it's 2am, you've been up all night hacking on your latest crappy project, when
you feel the need to use a calculator for something.
how fucked are you? oh idk, depends what you need the calculator for. if you're
just adding integers together you can do that in python, really. but what if
you're interested in doing arbitrary precision calculations? or what if you
really hate python?
dc is there for you my friend. it's a stack-based/RPN desk calculator (hence
the name) but all that really means is your pals will think you're a 1337
hacker for using it. basically everything works on a stack, a FILO (first-in,
last-out) data structure. When you type a number, it gets pushed onto the
top of the stack:
12 13 14
____
| 14 |
'----'
____
| 13 |
'----'
____
| 12 |
'----'
And when you type an operation (like +) it pops the numbers it needs off the
stack, and pushes its result onto the stack:
12 13 14 +
____
| 27 |
'----'
____
| 12 |
'----'
Neat, right? Turns out you can do just about any calculation with this method,
including super annoying ones that usually need a fuckton of brackets. (The
usual mathematical operators do what you expect; p prints the top of the stack)
12 9 + 14 * 73 -
instead of
((12 + 9) * 14) - 73
Pro tip: `echo 5 k > ~/.dcrc`. What does that do? It sets dc to 5 point
precision (I think that's the technical term) so now you get better results
when real numbers are involved:
1 3 / p
0
5 k
1 3 / p
0.33333
Wicked sick.
Read the man page for dc for more, or just have a play. it's good and I
recommend using it.
Or if you're a coward, you can just use bc instead. Dangit.