| manual | gnu bsd |
| source | unix |
| wiki | english spanish |
- 1971
- Authors:
- Robert H. Morris (39)
- Lorinda Cherry (27)
- "Desk Calculator"
- a reverse-polish (RPN) calculator
- a stack based language
- up to 1M digits of decimal precision
- ~/.dcrc
cli
dc [OPTIONS] [FILES]
| -f | –file= | FILE | executes script file |
| -e | –expression= | EXPR | runs commands in script on input |
$ export DC_LINE_LENGTH=0 # disable line break $ dc script.dc $ dc -e '[Enter time: ]P?st' -e '[Distance: ]Plt2^0.5*9.8*p' $ echo '2 3 +' | dc $ echo '2 3' | dc -f - -e 'f' $ echo '2 3' | dc -e '? ? +'
language
- supports recursion
- # for comments
strings
- can only be printed or executed as macros
- delimited by []
numbers
- hex numbers MUST be upcased (A-F)
- use _ for negative numbers
| T | P | T | P | ||||
|---|---|---|---|---|---|---|---|
| 1 | i | set input radix | I | 1 | push input radix | ||
| 1 | o | set output radix | O | 1 | push output radix | ||
| 1 | k | set decimal precision | K | 1 | push decimal precision | ||
| 1 | Z | 1 | number of decimals | 1 | X | 1 | number of fraction digits |
| 2 | + | 1 | fst + snd | 2 | - | 1 | fst - snd |
| 2 | * | 1 | fst * snd | 2 | / | 1 | fst / snd |
| 2 | % | 1 | remainder of / | 2 | ~ | 2 | divmod(fst,snd) |
| 2 | ^ | 1 | fst ^ snd | 3 | ¦ | 1 | mod(fst, snd^trd) |
| 1 | v | 1 | sqrt |
arrays
| 2 | :A | A[snd] = fst | |
| 1 | ;A | 1 | A[fst] |
registers (256)
- each register has his own stack
- valid names: a A 0 . " …
- one char length
- you CAN use a register name that overlaps with a command
- and the command will stil work
| 1 | sR | R.push(fst) | 1 | SR | R=[fst] | ||
| LR | 1 | R.pop() | lR | 1 | R[R.size]¦¦0 | ||
| 2 | >R | x R if > | 2 | <R | x R if < | ||
| 2 | !<R | x R if >= | 2 | !>R | x R if <= | ||
| 2 | =R | x R if = | 2 | !=R | x R if != |
other commands
| 1 | a | 1 | 1st char/byte |
| ! | system command | ||
| # | comment line | ||
| ? | 1 | ask user input, and x it | |
| 1 | x | executes as a macro | |
| q | exits 2 level macro | ||
| 1 | Q | exits Nth level macro |
| f | print full stack | ||
| p | print top of stack, with \n | ||
| 1 | n | print top of stack, no \n | |
| 1 | P | 1 | print top of stack, no \n, as char if number |
stack manipulation
| z | 1 | stack size | |
| * | c | clear all stack | |
| 1 | d | 2 | dup |
| 2 | r | 2 | swap |
| 1 | R | rotates top Nth items once |
snippets
- https://literateprograms.org/category_programming_language_dc.html
- https://rosettacode.org/wiki/Category:Dc
- brunk23/dc-programs
sum of first 36 integers
36[d1-d1<F+]dsFxp
factorial
[d1<g]Sf[d1-lfx*]Sg 13lfx
codebases
- https://x.com/nixcraft/status/1493822005262491651
- Alhadis/language-etc/dirsize.dc
- brunk23/code-challenges/trend.error
-
[91aPP93aP[dx]p]dx
- grondilu/bitcoin-bash-tools