
Clac – A command-line, stack-based calculator with postfix notation - djanowski
https://github.com/soveran/clac
======
klodolph
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.

~~~
chocolatebunny
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.

~~~
smhenderson
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.

------
protomyth
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?

~~~
soveran
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.

~~~
protomyth
Postscript had the concept of dropping a mark on the stack that allowed
commands that would consume the whole stack to also stop at a mark if
encountered.

    
    
      5 4 mark 3 2 1 sum -> 5 4 6

~~~
soveran
I love the idea, thanks for the tip!

------
eridius
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.

------
auxym
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](http://freecode.com/projects/orpie)

------
Animats
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.

~~~
wrycoder
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.

~~~
joezydeco
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](https://itunes.apple.com/us/app/free42/id337692629)

There are other builds for Android, Windows, Mac, and Linux:

[http://thomasokken.com/free42/](http://thomasokken.com/free42/)

------
GilbertErik
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.

------
Kametrixom
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)](https://en.wikipedia.org/wiki/Bc_\(programming_language\))

[2]: [https://unix.stackexchange.com/questions/124518/how-is-bc-
di...](https://unix.stackexchange.com/questions/124518/how-is-bc-different-
from-dc)

------
pkd
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.

------
lexpar
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.

~~~
soveran
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](http://files.soveran.com/misc/clac.mov)

~~~
undershirt
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.

~~~
soveran
I just updated the README with a gif of that video. Thanks for the idea!

~~~
lexpar
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.

