TrumanWong

DC

arbitrary precision calculator

illustrate

dc is a reverse Polish expression calculator that supports unlimited precision arithmetic operations. It also allows you to define and call macros. Normally, dc reads from standard input, but can also be evaluated by passing in a file as an argument.

grammar

dc [options] [file...]

Options

-e, --expression=EXPR # Evaluate expression
-f, --file=FILE # Evaluate file contents
-h, --help # Show this help and exit
-V, --version # Output version information and exit
p prints the value at the top of the stack and ends the statement with a newline character.
n Prints the value at the top of the stack and ends the line with an empty statement.
f prints the entire stack without any changes.
P pops the value from the top of the stack.
c Clear the stack.
d copies the top value and pushes it onto the main stack.
r Reverse the order of the top two elements in the stack.
Z pops a value from the stack, counts the number of digits in it, and pushes that number.
X pops a value from the stack, counts the number of decimal places in it, and pushes that number.
z Push the stack length onto the stack.
i pops a value from the stack and uses it as the input base.
o Pop a value from the stack and use it as the output base.
Pop the k value from the stack and use it to set the precision.
I Push the value of the input base onto the stack.
O Push the value of the output base onto the stack
K pushes the precision value onto the stack.

Example

The following is the process of calculating 10 * 10 completed by the dc command on the command line to obtain the result 100 and deriving it.

$dc

10 # 1. Enter the number 10
10 # 2. Enter the number 10
* # 3. Input operation type * means multiplication
p # 4. Enter p to get the calculation result
100
q # 5. Enter q to exit dc

Example showing command line result 509

$ dc --expression="50 10 * 9 + p"
509

Supported operations

+ Pops two values from the stack, adds them, and pushes the result.

- Pops two values, subtracts the first value popped from the second value popped, and pushes the result onto the stack.

* Pops two values, multiplies them, and pushes the result. The number of fractions in the result depends on the current precision value and the number of fractions in the two parameters.

/ Pop two values, divide the second value popped by the first value popped, and push the result. The number of fractional digits is specified by the precision value.

% pops two values, computes the remainder of the division that the command/command will perform, and pushes the value. The calculated value is the same as the value calculated for the sequence Sd dld/Ld*-.

~ Pops two values, dividing the second value popped by the first value popped. Push the quotient first, then the remainder. The number of decimal places used in division is specified by the precision value.

(The sequence SdSn lnld/lnld% also accomplishes this function, but the error checking is slightly different.)

^ Pops two values and powers them using the first value popped as the exponent and the second value as the base. Ignore the fractional part of the exponent.

| Pops three values and computes the modular exponentiation. The first value popped is used as the reduction modulus; this value must be a non-zero number and should be an integer. The second one popped is used as the exponent; the value must be nonnegative, and any fractional part of the exponent is ignored. The third value that pops up is the base for exponentiation, which should be an integer. For small integers, this is similar to the sequence Sm^Lm%, but unlike ^ this command works for arbitrarily large exponents.

v pops a value, computes its square root, and pushes it. The maximum value of the precision value and the precision of the argument are used to determine the number of decimal places in the result.