Clac – A command-line, stack-based calculator with postfix notation(github.com)
github.com
Clac – A command-line, stack-based calculator with postfix notation
https://github.com/soveran/clac
28 comments
For those not familiar with "dc", try running dc and then running "7 100 ^ p". Arbitrary precision! One of the reasons dc doesn't print things out is so you can use it to do arithmetic in your shell scripts.
you can use $((5+5)) if you want to do arithmetic in a bash shell. Most of the time that's all that's needed.
But when it was written when there was no bash. Or even sh.
It was written in B before the C programming language was created.
It was written in B before the C programming language was created.
Nice. I did one in Perl a long while back. I added sum to sum the contents of the stack (good for bill paying day) and pt for Pythagorean theorem because I was doing a lot of cable distances.
Is there a command line Forth interpreter out there that can save words or load a startup dictionary each use?
Is there a command line Forth interpreter out there that can save words or load a startup dictionary each use?
That's interesting: commands that consume everything in the stack. About saving/loading words: I have plans to work on that, it could be a very useful feature.
When I saw the headline, I wondered what was so special about this. But actually reading the description, the UI sounds like a significant improvement in usability, such that I might actually consider using a postfix calculator like this.
On a side note, the description of arithmetic says that - and / swap the inputs, and it describes the operators as popping values a and b (and in the case of swap, then pushing a and b back on). This is a little confusing. It would be much simpler just to say that all binary operators pop values b and a. Now you don't have to swap anything, because - and / will still just perform a - b and a / b. And the fact that this swaps + and * is irrelevant, because those operators are commutative. And the swap operator makes more sense, because it "pops values b and a, and pushes values a and b", so you can now actually see the swapped values in the description.
On a side note, the description of arithmetic says that - and / swap the inputs, and it describes the operators as popping values a and b (and in the case of swap, then pushing a and b back on). This is a little confusing. It would be much simpler just to say that all binary operators pop values b and a. Now you don't have to swap anything, because - and / will still just perform a - b and a / b. And the fact that this swaps + and * is irrelevant, because those operators are commutative. And the swap operator makes more sense, because it "pops values b and a, and pushes values a and b", so you can now actually see the swapped values in the description.
orpie has been my main calculator-on-the-pc for a while now. Curses based interface, written in OCaml, extensive library of scientific functions, configurable hotkeys. It's quite neat.
Unfortunately, the homepage seems to be down at the moment, but here's a freecode link. It's also packaged for most popular distros.
http://freecode.com/projects/orpie
Unfortunately, the homepage seems to be down at the moment, but here's a freecode link. It's also packaged for most popular distros.
http://freecode.com/projects/orpie
Surprisingly, postfix calculators fell out of fashion. I still have an original HP-11C on my desk. Students are now introduced to calculators that meet the "educational standard", that is, the TI-84 and its approved clones. That seems to have set what people expect in a calculator.
There is a great HP-41C for iOS: i41Cx. I believe it's the most expensive app I have, and it's worth every penny. I still have my HP-41, but I never drag it out because of this.
There's an open-source clone of the HP-42s that has found it's way into different smartphone apps.
I use Free42 for iOS and it works beautifully. Plus I can leave my real 42s safe in a drawer at home.
https://itunes.apple.com/us/app/free42/id337692629
There are other builds for Android, Windows, Mac, and Linux:
http://thomasokken.com/free42/
I use Free42 for iOS and it works beautifully. Plus I can leave my real 42s safe in a drawer at home.
https://itunes.apple.com/us/app/free42/id337692629
There are other builds for Android, Windows, Mac, and Linux:
http://thomasokken.com/free42/
If I need to see what's going on, I've relied on orpie for quite a long time. I like soveran's video showing off what it does, but I'd rather manipulate a stack rather than have the output appended to the end of my cursor.
There is also bc [1] which isn't stack based and pretty standard. See [2] for a discussion on dc vs bc.
[1]: https://en.wikipedia.org/wiki/Bc_(programming_language)
[2]: https://unix.stackexchange.com/questions/124518/how-is-bc-di...
[1]: https://en.wikipedia.org/wiki/Bc_(programming_language)
[2]: https://unix.stackexchange.com/questions/124518/how-is-bc-di...
I did this as part of my data structures course at University! Good exercise, but obviously mine lacked the swap, dup features etc. Instead I had min and max implemented.
[deleted]
I don't understand why things like this get posted to HN so often. This kind of program is a very common exercise in university courses and appears in countless programming books. Heck, you implemented it in C, and this exact exercise appears in K&R C.
I hope this doesn't come off as snide... I just don't understand why the author would think this is something worth showing off.
I hope this doesn't come off as snide... I just don't understand why the author would think this is something worth showing off.
I'm not the author of the post, I just wrote the tool. I agree with you that it's not groundbreaking technology :-)
In fact, the code is extremely simple. As I mentioned in the README, I've used dc a lot and what I propose with clac is an improvement (for my taste) regarding the UI, as there's an always-visible representation of the stack that gets updated as you type. I recorded a short video to show it in action: http://files.soveran.com/misc/clac.mov
In fact, the code is extremely simple. As I mentioned in the README, I've used dc a lot and what I propose with clac is an improvement (for my taste) regarding the UI, as there's an always-visible representation of the stack that gets updated as you type. I recorded a short video to show it in action: http://files.soveran.com/misc/clac.mov
I just want to say great work. The parent comment here didn't realize that you drastically improved the user experience by showing the stack, which I think makes all the difference here.
To make this clear, I would upload your video to gfycat and link the gif in your readme.
To make this clear, I would upload your video to gfycat and link the gif in your readme.
I just updated the README with a gif of that video. Thanks for the idea!
Just looked at the video, the 'as you type' updated stack info is actually pretty cool. I thought based on the description that it only printed the contents of the stack after a command was executed. This is much nicer.
You guys are right, I didn't realise it was this cool, and I was too quick to be cynical.
You guys are right, I didn't realise it was this cool, and I was too quick to be cynical.
Actually, the prospect of maybe, randomly getting to the front page of HN with some small interesting program is a nice motivator for small side projects. Overall it's probably a good thing introducing people to new concepts and ideas along the way (in this case Forth, post-fix notation and stack-based evaluation strategies).
Sharing a side project, even a simple one, is hardly showing off. It would be showing off if he claimed it was some amazing earth-shattering new thing that's going to blow everyone's minds. On the other hand, being dismissive about its mundanity is clearly an attempt to show off.
> I don't understand why things like this get posted to HN so often.
Nostalgia.
Nostalgia.
Likewise, I don't see this replacing dc for me. The exciting thing is really the suggestion to consider what it would take to implement your own dc. Reimplementing dc was never an assignment I saw as a student, so I think it's valuable to point out that it's an interesting exercise.
> I just don't understand why the author would think this is something worth showing off.
There's a large class of people who can be relied on to see this as a noteworthy accomplishment -- students. More experienced hands will note the absence of range checking and error trapping.
There's a large class of people who can be relied on to see this as a noteworthy accomplishment -- students. More experienced hands will note the absence of range checking and error trapping.