- Run calculator in interactive mode:dc- Execute dc script in file:dc -f [file]- Calculate 4 times 5 [4 5 *], subtract 17 [17 -], and [p]rint the output (using echo):echo "4 5 * 17 - p"| dc- Set number of decimal places to 7 [7 k], calculate 5 divided by -3 [5 _3 /] and [p]rint (using dc -e):dc -e "7 k 5 _3 / p"- Calculate the golden ratio, phi: Set number of decimal places to 100 [100 k], square root of 5 [5 v] plus 1 [1 +], divided by 2 [2 /], and [p]rint result:dc -e "100 k 5 v 1 + 2 / p"
dc [-V] [--version] [-h] [--help] [-e scriptexpression] [--expression=scriptexpression] [-f scriptfile] [--file=scriptfile] [file ...]
dc is a reverse-polish desk calculator which supports unlimited precision arithmetic. It also allows you to define and call macros. Normally dc reads from the standard input; if any command arguments are given to it, they are filenames, and dc reads and executes the contents of the files before reading from standard input. All normal output is to standard output; all error output is to standard error.
A reverse-polish calculator stores numbers on a stack. Entering a number pushes it on the stack. Arithmetic operations pop arguments off the stack and push the results.
To enter a number in dc, type the digits (using upper case letters A through F as "digits" when working with input bases greater than ten), with an optional decimal point. Exponential notation is not supported. To enter a negative number, begin the number with ``_''. ``-'' cannot be used for this, as it is a binary operator for subtraction instead. To enter two numbers in succession, separate them with spaces or newlines. These have no meaning as commands.
dc may be invoked with the following command-line options:
If any command-line parameters remain after processing the above, these parameters are interpreted as the names of input files to be processed. A file name of - refers to the standard input stream. The standard input will processed if no script files or expressions are specified.
Most arithmetic operations are affected by the ``precision value'', which you can set with the k command. The default precision value is zero, which means that all arithmetic except for addition and subtraction produces integer results.
dc provides at least 256 memory registers, each named by a single character. You can store a number or a string in a register and retrieve it later.
Each register also contains its own stack. The current register value is the top of the register's stack.
dc has three parameters that control its operation: the precision, the input radix, and the output radix. The precision specifies the number of fraction digits to keep in the result of most arithmetic operations. The input radix controls the interpretation of numbers typed in; all numbers typed in use this radix. The output radix is used for printing numbers.
The input and output radices are separate parameters; you can make them unequal, which can be useful or confusing. The input radix must be between 2 and 16 inclusive. The output radix must be at least 2. The precision must be zero or greater. The precision is always measured in decimal digits, regardless of the current input or output radix.
dc has a limited ability to operate on strings as well as on numbers; the only things you can do with strings are print them and execute them as macros (which means that the contents of the string are processed as dc commands). All registers and the stack can hold strings, and dc always knows whether any given object is a string or a number. Some commands such as arithmetic operations demand numbers as arguments and print errors if given strings. Other commands can accept either a number or a string; for example, the p command can accept either and prints the object according to its type.
Macros are most often stored in registers; [1p]sa stores a macro to print 1 into register a, and lax invokes this macro.
Note that each stacked instance of a register has its own array associated with it. Thus 1 0:a 0Sa 2 0:a La 0;ap will print 1, because the 2 was stored in an instance of 0:a that was later popped.
Email bug reports to firstname.lastname@example.org.