Category:Dc: Difference between revisions

Content added Content deleted
No edit summary
(Expand the quick reference to cover OpenBSD dc.)
Line 4: Line 4:
}}dc is the unix desktop calculator. It uses a reverse polish notation and is turing complete.
}}dc is the unix desktop calculator. It uses a reverse polish notation and is turing complete.


== Quick reference ==
Commands in POSIX dc
Commands in [[AT&T dc]]
number : push a number
number : push a number (0-9A-F digits, _ negative, . radix point)
+ - / * % ^ : arithmetic operations: add sub div mul mod exp
sx Sx : pop the stack and save or push it to register x
[...] : push a string
+ - * / % ^ : arithmetic operations: add sub mul div mod pow
lx Lx : copy | pop the register x to main stack.
v : square root
c : clear stack
d : duplicate top of the stack
d : duplicate top of the stack
p : print top of the stack
P : pop and print String
f : show stack
f : show stack
q : exit (drop execution stack by 2)
p : print value with a newline, leave it on the stack
Q : drop execution stack by top of the stack number
P : print string, or print number as ASCII string
x : execute dc commands on top of the stack
sx : store to register x
X : replace top of stack by its scale factor.
lx : load from register x
[...] : string notation
Sx : push to register x
<x >x =x : compare 2 elements at top of the stack, and execute register x
Lx : pop register x
v : square root
a b :x : store x[b] = a (x is register)
a ;x : retrieve x[b]
<x >x =x : compare top 2 values: if true, then execute register x
!>x !<x !=x : (example: 5 2 <A executes register A if 2 < 5)
! : shell command
! : shell command
c : clear
q : quit, or break 2 execution levels
n Q : break n execution levels
i : set input radix from top of stack
i : set input radix from top of stack
I : push input base on the top of the stack.
I : get current input base, push it on stack
o : set output radix from top of stack
o : set output radix from top of stack
O : push output base on the top of the stack.
O : get current output base, push it on stack
k : set scale factor
k : set scale factor
K : get current scale factor
K : get current scale factor
z : get the current stack level.
x : execute a string of dc commands
Z : get the character length of element at top of stack
X : count decimal digits after decimal point
? : read input and execute.
z : count elements on the stack
Z : count all decimal digits in number, or characters in string
? : read and execute one line of input
Y : debug information
Y : debug information
a b :r : store r[b] = a (r is register)
a ;r : retrieve r[b]


Extensions in both [[GNU dc]] and [[OpenBSD dc]]
GNU Extensions
a : print ASCII character
n : print value without a newline
~ : divmod: division and remainder
r : reverse (swap) the top two elements
# : comment


Extension in [[GNU dc]]
n : print value without a new line
~ : arithmetic - divmod
| : arithmetic - modular exponentiation
| : arithmetic - modular exponentiation

r : swap the top two elements
Extensions in [[OpenBSD dc]]
a : asciify
n J : break n execution levels, then jump to next M
!>r !<r !=r : execute contents of register x based on comparison
X : pushes the number of fractional digits
M : mark for the J command
# : comment
a N : boolean not: push 1 if a == 0, else push 0
R : pop and discard top of the stack
<xey >xey : comparisons with an else branch: if true,
=xey !<xey : then execute register x
!>xey !=xey : else execute register y
a b G : equality: push 1 if b == a, else push 0
a b ( : less than: push 1 if b < a, else push 0