dc
Reverse-Polish calculator and one of the oldest Unix utilities.
Commands
| Command | Use | ||
|---|---|---|---|
| Printing | |||
| p | print the stack top without altering the stack | ||
| n | pops the stack top and prints it without a newline | ||
| P | pops the stack top; if a string, print it without a newline; otherwise print the integer value as a byte stream | ||
| f | print the entire stack top to bottom without altering anything | ||
| Arithmetic | |||
| + | pops two values, adds them, and pushes the result | ||
| - | pops two values, subtracts the first popped from the second, and pushes the result | ||
| * | pops two values, multiplies them, and pushes the result | ||
| / | pops two values, divides the second popped by the first, and pushes the result | ||
| % | pops two values, calculates the remainder of the division that / would perform, and pushes that | ||
| ~ | pops two values, divides the second popped by the first popped, then pushes the quotient followed by the remainder | ||
| \ | pops two values, raises the second popped to the power of the first popped, and pushes the result | ||
| \ | pops three values; the first popped is a reduction modulus, the third is the base, and the second is the exponent | ||
| v | pops one value and computes its square root | ||
| Stack control | |||
| c | clears the stack | ||
| d | duplicates the value on top of the stack | ||
| r | swaps the top two values on the stack | ||
| R | pops the top value as an integer $n$, then rotates the top $n$ elements of the stack; if the stack has fewer than $n$ elements, the entire stack is rotated | ||
| Registers | |||
| sr | pop the stack top and store it in register r | ||
| lr | copy the value from register r and push it to the stack | ||
| Sr | pop the top of the main stack and push it onto the stack of register r | ||
| Lr | pop the top of register r's stack and push it onto the main stack | ||
| Parameters | |||
| i | pops a value off the stack and uses it as the input radix | ||
| o | pops a value off the stack and uses it as the output radix | ||
| k | pops a value off the stack and uses it to set the precision | ||
| I | pushes the current input radix onto the stack | ||
| O | pushes the current output radix onto the stack | ||
| K | pushes the current precision onto the stack | ||
| Strings | |||
| [text] | pushes text to the stack as a string | ||
| a | pops the top of stack; if it was a number, the low-order byte is converted to a string and pushed onto the stack; otherwise, the first character of the string is pushed back | ||
| x | pops a value off the stack and executes it as a macro; if the stack top was a number, it is simply pushed back | ||
| >r | pops two values and compares them as numbers, then executes the contents of r as a macro if the first popped is greater than the second | ||
| !>r | similar, but invokes the macro if the first popped is not greater than the second | ||
| <r | similar, but invokes the macro if the first popped is less than the second | ||
| !<r | similar, but invokes the macro if the first popped is not less than the second | ||
| =r | similar, but invokes the macro if the two numbers are equal | ||
| !=r | similar, but invokes the macro if the two numbers are not equal | ||
| ? | reads a line from the terminal and executes it, allowing for user input | ||
| q | exits from a macro and from the macro which invoked it; if called from the top level, exits dc | ||
| Q | pops a value off the stack and uses it as a count of macro levels to exit; will never cause dc to exit | ||
| Status inquiry | |||
| Z | pops a value off the stack, calculates the number of decimal digits (or number of characters if a string) it has, and pushes that number; this does not include any leading zeros | ||
| X | pops a value off the stack, calculates the number of fraction digits it has, and pushes that number; if a string, the value pushed is 0 | ||
| z | pushes the current stack depth | ||
| Miscellaneous | |||
| ! | executes the rest of the line as a system command; if the command begins with <, =, or >, then a space must be present after the ! | ||
| # | interprets the rest of the line as a comment | ||
| :r | pops the top two values off the stack, storing the second popped in the array r, indexed by the first popped | ||
| ;r | pops the top of stack and uses it as an index into array r; the selected value is then pushed onto the stack |
Samples
Break these examples out with comments
-
sum the entire stack:
0d[+z1<a]dsaxp -
calculate the GCD of the top two stack values:
[dSarLa%d0<a]dsax+p -
compute the factorial of the stack top:
[q]sQ[d1=Qd1-lFx*]dsFxp