DC(1)

NAME
dc - arbitrary-precision desk calculator

SYNOPSIS
dc [-i] [file ...]

DESCRIPTION
dc is a reverse-polish notation arbitrary-precision desk calculator. It reads and executes commands from each file in sequence. After processing all files, dc reads from stdin.

Numbers are arbitrary-precision decimal values. Numbers may contain a decimal point and use ‘_’ as a negative sign prefix. When the input base is greater than 10, letters A-F represent digits 10-15.

OPTIONS

-i  Extended identifier mode. Register names can be enclosed in ‘<...>’ (numeric identifiers) or ‘"..."’ (string identifiers).

COMMANDS

  Printing

p   Print the value on top of the stack with a newline, without popping it.

n   Print the value on top of the stack without a newline, and pop it.

P   Print the value on top of the stack as a byte stream. For strings, print the characters directly. For numbers, print it in base 100 representing the internal representation. The value is popped.

f   Print all values on the stack, one per line, from top to bottom.

  Arithmetic

+   Pop two values, add them, and push the result.

-   Pop two values, subtract the top from the second, and push the result.

*   Pop two values, multiply them, and push the result.

/   Pop two values, divide the second by the top, and push the quotient. The scale of the result is determined by the scale parameter.

%   Pop two values, compute the remainder of dividing the second by the top, and push the result.

~   Pop two values, compute both quotient and remainder, pushing the quotient first then the remainder on top.

^   Pop two values, raise the second to the power of the top, and push the result. The exponent is truncated to an integer.

v   Pop the top value, compute its square root, and push the result. The precision is determined by the scale parameter.

  Stack

c   Clear the stack.

d   Duplicate the top of the stack.

r   Reverse the order of the top two values on the stack.

z   Push the current stack depth.

  Registers
Registers are named storage locations. In normal mode, register names are single characters. With the -i option, extended names are available. Each register also has an associated stack.

sx  Pop the top value and store it in register x.

lx  Push a copy of the value in register x onto the stack.

Sx  Pop the top value and push it onto register x's stack.

Lx  Pop the top value from register x's stack and push it onto the main stack.

  Arrays
Each register has an associated array.

:x  Pop an index, then pop a value, and store the value at that index in array x.

;x  Pop an index and push the value at that index in array x.

  Parameters

i   Pop the top value and use it as the input radix. The input base must be between 2 and 16.

o   Pop the top value and use it as the output radix. The output base must be at least 2.

k   Pop the top value and use it as the scale (number of decimal places) for arithmetic operations. The scale must be non-negative.

I   Push the current input radix.

O   Push the current output radix.

K   Push the current scale.

  Strings and Macros

[string]
    Push string onto the stack. Brackets inside the string must be balanced or escaped with a backslash.

x   Pop the top value. If it is a string, execute it as a macro. If it is a number, push it back.

  Conditionals
The conditional commands pop two values, compare them, and if the condition is true, execute the contents of register x as a macro.

>x  Execute register x if the second value is greater than the top.

<x  Execute register x if the second value is less than the top.

=x  Execute register x if the two values are equal.

!>x Execute register x if the second value is not greater than (less or equal to) the top.

!<x Execute register x if the second value is not less than (greater or equal to) the top.

!=x Execute register x if the two values are not equal.

  Control

q   Quit. If executing a macro, exit two macro levels.

Q   Pop a value and quit that many macro levels.

  Input

?   Read a line from stdin and execute it.

#   Comment. The rest of the line is ignored.

!command
    Execute command as a shell command.

  Number Information

Z   Pop a value and push its length. For numbers, push the number of significant digits. For strings, push the number of characters.

X   Pop a value and push its scale (number of fractional digits). For strings, push 0.

EXAMPLES
Compute 6 * 7:

    6 7*p

Compute 2^10:

    2 10^p

Compute square root of 2 with 20 decimal places:

    20k 2vp

Factorial using recursion (store in register f):

    [d1-d1<f*]sf 10lf xp

SEE ALSO
bc(1) R. H. Morris, L. L. Cherry, DC — An Interactive Desk Calculator.

STANDARDS
dc is compatible with traditional UNIX dc implementations. The -i flag, # comments and the ~ operator are extensions to the traditional dc specification.