5 ms·
Unix history and the `dc` calculator
- gladiatr72 2y agoha! Nice article! I've never encountered another dc user let alone a blog post. Bravo.
- thanatos519 2y agoHello fellow dc user! I use it as my default calculator and even in shell scripts I'm all "sum=$(echo $a $b + p | dc)" (though it is usually a much more complex expression of course) I still have my HP48SX RPN calculator too!
- sevensor 2y ago> Hello fellow dc user! What are the chances all four of us would find the same thread? It’s so fast, so reliable, so omnipresent. Between that and being able to turn multiplication into addition by working in powers of two, I get answers while my colleagues are still waiting for excel to start.
- Borg3 2y agoAhaha :) Nice on. Its kinda similar to my net docs system written in ruby. I just type blgrep ip=192.168.0.10,15 hosts.txt and get answer, before people start any IPAM :) CLI power... Unfortunately, it does not get right respect these days.
- alisonatwork 2y agoI'm not sure why I started using dc instead of bc, but I think it might have been because at one point in time bc output a bunch of extra noise that made it annoying to use in shell scripts, so I just learned dc is the standard way to calculate stuff in UNIX and kept on using it that way. I noticed some new Linuxes don't have it installed and I'm always stuck for how to calculate things in shell scripts. I guess people just use Bash nowadays. In any case, thanks to the Gavin Howard dc I still use it today, even on the Windows command line. dc is great!
- gavinhoward 2y agoI have a user on Windows? Gasp! Glad you like it!
- baudaux 2y agoGNU bc online: https://exaequos.com/?a=/usr/bin/bc https://exaequos.com/?a=/usr/bin/bc
- 082349872349872 2y agoAlso, unlike bc, dc can be used to write binary files. (and it sure beats a magnetised needle and a steady hand)
- quectophoton 2y agoYou're not the only one! I also use `dc`, together with `grpe` and `gits tatus`.
- zzo38computer 2y agoI also use dc
- gavinhoward 2y agoAs the author of a `dc` (https://git.gavinhoward.com/gavin/bc https://git.gavinhoward.com/gavin/bc), I appreciate this article! The author is correct on the parsing of "normal" vs. Reverse Polish notation. My `bc`, with normal expressions, needs 1555 LoC for parsing. My `dc` needs just 298. I have a couple posts ([1], [2]) about `dc` tricks and code on my blog. And yes, I have an HP 48G [3]. :) [1]: https://gavinhoward.com/2019/12/bc/dc-tips-and-tricks/ https://gavinhoward.com/2019/12/bc/dc-tips-and-tricks/ [2]: https://gavinhoward.com/2021/04/a-dc-script-for-easter/ https://gavinhoward.com/2021/04/a-dc-script-for-easter/ [3]: https://gavinhoward.com/uploads/easter-dc-script/hp-48g.jpg https://gavinhoward.com/uploads/easter-dc-script/hp-48g.jpg
- whartung 2y agoiHP48 on iOS is excellent. It’s my mostly go to calculator on the go. I find the lack of a backlight on the legacy HPs to make usage difficult (plus I always have my phone).
- fsckboy 2y agois your bc the bc or is it a bc? bc is a POSIX standard so "a" bc would be as good as "the" bc, i'm just curious. "man bc" on Fedora linux says the author is Phil Nelson
- gavinhoward 2y agoDepends on what platform you are using. Most Linux distros? Only a bc. FreeBSD and macOS? The bc. [1] [1]: https://gavinhoward.com/2023/02/my-code-conquered-another-os/ https://gavinhoward.com/2023/02/my-code-conquered-another-os...
- lolinder 2y agoAfter learning Forth years ago I became addicted to RPN and stack-based calculation, to the point where I wrote myself a calculator for Android that does just what I need it to (no existing calculators satisfied my Forth-like craving). The really nice thing about stack based calculators that keeps me coming back to mine is that I can get all the numbers I'm working with down in one shot and then figure out which computations I'm applying in which order as a second step. Until I tried RPN I never noticed how much mental bandwidth I was using shuffling numbers around in my head so I could input them into my calculator in the right order. With my stack calculator I just get all the numbers down and then swap and roll as needed to get the computation right. The number of times I've had to redo a computation because I started out wrong has been reduced to effectively zero, because I'm able to fully engage my math brain instead of juggling numbers at the same time.
- kragen 2y agoi'm running gforth under termux on android. i just ran a video game on it earlier tonight, this one i wrote last month do you have a screencast of your calculator's user interface? it sounds interesting!
- lolinder 2y agoI don't, but it's pretty simple: it looks like a regular calculator, but instead of having a single line for the number it has a line for building a number and then a scrolling text box that shows the stack [0]. There's an enter button that moves a number from the construction line to the stack, or you can hit any of the operation keys to implicitly use the under-construction number as the top of the stack. If no number is under construction it uses the top two numbers as the operands. Short tap on the stack swaps the top two stack items, long press rolls the top three. If I were doing it again I'd have long press roll the entire stack (top becomes bottom, the rest move up)—since it's all visible and I'm not programming it this would actually be more useful. [0] On my phone at the time I wrote it this box had room for three numbers before scrolling. Now it has 12...
- 2y ago
- usr1106 2y agoAs an HP mini calculator user since the late 1970s I use dc as my favorite calculator on every terminal since the late 1980s. Annoyingly it is not installed on every server or embedded system.
- bawolff 2y agoDC one liners always seem like the most arcane bit of magic (to the uninitiated like me). E.g. dc -e '_640320[0ksslk3^16lkd12+sk-lmlhd1+sh3^/smlxljsxll545140134+dsllmlxlnk/ls+dls!=P]sP3^sj7sn[6sk1ddshsxsm13591409dsllPx10005v426880*ls/K3-k1/pcln14+snlMx]dsMx'
- Diti 2y agoFor those who are on NixOS and can’t find `dc`, it’s present in BusyBox. nix run nixpkgs#busybox -- dc -e '_640320[0ksslk3^16lkd12+sk*-lm*lhd1+sh3^/smlxlj*sxll545140134+dsllm*lxlnk/ls+dls!=P]sP3^sj7sn[6sk1ddshsxsm13591409dsllPx10005v426880*ls/K3-k1/pcln14+snlMx]dsMx'
- kragen 2y ago
- whartung 2y agoAt the computer my level of math escalation is the generic Mac desktop calculator. From there I may shift to a handy emacs scratch buffer. After that it moves to firing up Slime into CL. The * * and ** variables (which in CL equate to the 3 most recent evaluation results) is actually quite handy. Plus I just have the Slime history and all that. None of those are RPN, but at this level I tend to not enter long equations anyway, and I have to do just as much precedence parsing cognitively converting algebraic to RPN or prefix anyway.
- koito17 2y agoReverse Polish notation, or RPN, removes the need for operator precedence. This is true of prefix or "Lisp notation" as well. I would show you an operator precedence table for Common Lisp or Clojure, but we don't need such a table. With that said, one does need to keep in mind how function evaluation and macro evaluation differ. e.g. Consider (f (g (h x))). If f, g, h are functions, then (h x) is evaluated first. If they are all macros, then f is macroexpanded first with the body (g (h x)). ... with postfix operators, the only data structure you need is a stack. This also applies to prefix. I wonder what the motivation was to introduce postfix notation and prefer it over prefix.
- kstrauser 2y agoI vastly prefer postfix for interactive use. I can enter a bunch of numbers and then decide what to do with them. With prefix, I start by typing operators when I haven’t even fully decided which numbers I’m going to be using with them. Basically, prefix emphasizes the operations and postfix emphasizes the numbers. Prefix is perfectly fine for entering formulas into code. In dc or any other scratchpad, I find postfix much, much more convenient.
- somat 2y agoThey both remove the need for screwball operator precedence, but the lisp s-expression do it by putting everything in parentheses(or perhaps everything in function notation is the better way to put it) and RPN does it by making the stack an integral part of the mental model needed to understand it.
- adrian_b 2y agoThe parentheses are needed in LISP only because many functions have a variable number of operands, so that parentheses are needed to show where is the end of the list of operands. In a language where all functions had a fixed number of operands, prefix notation (or function invocation in general) could be used without any parentheses. Parentheses are also required for postfix notation when functions with variable number of operands are accepted (e.g. the sum of an arbitrary number of operands).
- baudaux 2y agoYou can launch GNU bc online using exaequOS: https://exaequos.com/?a=/usr/bin/bc https://exaequos.com/?a=/usr/bin/bc (I am the creator of exaequOS). I will add dc as well since it is appreciated
- baudaux 2y agoGNU dc is now online in exaequOS: https://exaequos.com/?a=@Benoit/dc https://exaequos.com/?a=@Benoit/dc
- fafhnir 2y agoI love RPN! I got used to it while my Engineer-Studies in Germany, around 1992. I got an HP 48SX an it was a hard time to really understand this RPN. But when you checked it, it was boosting my Math-skills. Because to work properly with RPN you MUST understand what you are doing. it's the exact mimic of calculating with pen and paper. If you don't understand what you are doing, you are doomed. And so i have to understand what i was doing, which was very supportive to learn math the right way! By the way, the most funny part of using aa RPN machine was the fact thats Students which wantes to lean my Calculator gave it right back after trying to use the thing the algebraic way.
- Someone 2y agoFTA: “I'm especially proud of my little prompt macro“ I don’t understand why that has to be a macro. It asks for user input, so “because it has to be fast” can’t be the answer, can it? I also don’t think it avoids some allocations. It’s creating two String’s, one for the call to read_line, and one to convert its trimmed result back to a String, is it? What do I overlook?
- jmclnx 2y agoI am an avid bc(1) user, dc(1) I never could figure out. I think I will now spend time trying to get use to it. cool article
- rstuart4133 2y agoOnce upon a time, if you were an avid user of bc(1) you where an avid user of dc(1). This is an extract of the Unix V7 man page for bc(1) https://man.cat-v.org/unix_7th/1/bc https://man.cat-v.org/unix_7th/1/bc : Bc is actually a preprocessor for dc(1), which it invokes automatically, unless the -c (compile only) option is pre- sent. In this case the dc input is sent to the standard output instead.
- kkfx 2y agoIf I remember correctly I've discovered RPN via an HP 49g+ and was very enlightened for it's quickness, not counting the "natural" equation editor, natural meaning that selections works like RPN stack. I do honestly not remember if I ever encounterd dc (while I've used bc, very casually mostly for computing partition boundaries and alike) but I still use M-x calc, which is actually RPN as well. After this post I've looked a man and well, it's a bit limited, googled a bit I've seen it's powerful, with macros, registers, usable for interactive scripts etc, I think it's a remarkable piece of software from another era, not because of RPN witch is very valid today, but because it was designed to run on limited resources to a point of being long to learn, a thing that might be useful today for quickness, but I fail to se a case for dc, and in general is not needed. A good example of old glories, nice for inspiring some modern design which tend to be crapplily complex on average... We can do much with very little, if we find clever ways to do so.
- throwaway81523 2y ago"This is a very simple algorithm, and it needs only very simple tools. Perhaps this is why dc, the UNIX RPN calculator, was the first utility ported to UNIX, way back in the PDP-11 days. It even predates the C programming language; originally it was implemented in B" I'm pretty sure the version with v6 Unix was written in PDP-11 assembler. It's possible it was translated from a version written in B? It wasn't THAT simple, since it had to implement arbitrary precision arithmetic. I have a vague memory of it using a "buddy system" storage allocator, a scheme also described in Knuth volume 1. Maybe the code is still around somewhere.
- throwaway81523 2y agoSelf-followup: yes, the v6 source code is here: dc1.s, dc2.s, etc. https://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/s1 https://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/...