- 1971
- Authors:
- Robert H. Morris Sr.
- Lorinda Cherry
- "Desk Calculator"
- a reverse-polish (RPN) calculator, with a stack based language
- up to 1M digits of decimal precision
cli
dc [OPTIONS] [FILES]
-f FILE | executes script file |
–file=FILE | " |
-e SCRIPT | runs commands in script on input |
–expression=SCRIPT | " |
DC_LINE_LENGTH=0 # disable line break DC_LINE_LENGTH=999 # < 2^16 -1
language
$ 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 '? ? +'
- types
- numbers
- hex numbers must be upcased (A-F)
- use _ for negative numbers
- strings
- can only be printed or execute them as macros
- delimited by []
- arrays
- numbers
- registers (256)
- valid names: a A 0 . " …
- one char length
- you CAN use a register name that overlaps with a command
- and it will stil work
- each register has his own stack
- valid names: a A 0 . " …
commands
T | P | ||
---|---|---|---|
f | print full stack | ||
p | print top of stack, new line | ||
1 | n | print top of stack, no \n | |
1 | P | 1 | print top of stack, no \n, as char if number |
2 | + | 1 | |
2 | - | 1 | fst - snd |
2 | * | 1 | |
2 | / | 1 | fst / snd |
2 | % | 1 | remainder of / |
2 | ~ | 2 | quotient and remainder of / |
2 | ^ | 1 | fst ^ snd |
3 | ¦ | 1 | mod(fst, snd ^ trd) |
1 | v | 1 | square root |
* | c | clear stack | |
1 | d | dup top of stack | |
r | swap top 2 values | ||
R | rotates the top Nth items | ||
1 | sR | stores in register R | |
1 | SR | stores in register R, nuke prev content on R | |
lR | 1 | load from register R, 0 if empty | |
LR | 1 | " and pops it from register R | |
1 | i | set input radix | |
1 | o | set output radix | |
1 | k | set decimal precision | |
I | 1 | push input radix | |
O | 1 | push output radix | |
K | 1 | push decimal precision | |
1 | x | executes as a macro | |
1 | a | 1 | pop top, and push back 1st char/byte |
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 != | |
? | 1 | ask user input, and execute it as a macro | |
q | exits 2 level macro | ||
1 | Q | exits Nth level macro | |
1 | Z | number of decimal digits | |
1 | X | number of fraction digits | |
z | 1 | number of element in stack | |
! | system command | ||
# | comment | ||
2 | :R | store fst in R array , at idx snd |
|
1 | ;r | get given array element in register |
snippets
- Alhadis/language-etc/dirsize.dc
- brunk23/code-challenges/trend.error
- brunk23/dc-programs
sum of first 36 integers
36[d1-d1<F+]dsFxp
factorial
[d1<g]Sf[d1-lfx*]Sg 13lfx