.TH dc "" "" Command
.PC "Desk calculator"
\fBdc [\fIfile\^\fB]\fR
.PP
.HS
Arbitrary precision desk calculator with registers,
using reverse-Polish notation.
Reads input from \fIfile\fR if given, then from stdin.
.HE
.B dc
is an arbitrary precision desk calculator. It simulates a
stacking calculator with ancillary registers.
Input must be entered in reverse Polish notation.
.B dc
maintains the expected number of decimal places
during addition, subtraction, and multiplication, but the
user must make an explicit request to maintain any places at all
during division.
.PP
.B dc
reads input from
.I file
if specified, and then from the standard input.
.B dc
accepts an arbitrary number of commands per line;
moreover, spaces need not be left between them.
.PP
The
.I "scale factor"
of a number is the number of places to the right of its decimal point.
The
.I "scale factor register"
controls
decimal places in calculations.
The scale factor does not affect addition or subtraction.
It affects multiplication only if
the sum of the scale factors of the two operands is greater than it.
The result of every division command has as many decimal places as
it specifies.
It affects exponentiation in that multiplication
is performed as many times as the integer part of the
exponent indicates; any fractional part of the exponent is ignored.
.PP
.B dc
recognizes the following commands and constructions:
.RS
.IP \fInumber\fR
.br
Stack the value of
.IR number .
A number is a string of symbols taken from the digits \*(Ql0\*(Qr through
\*(Ql9\*(Qr, and the capital letters \*(QlA\*(Qr through \*(QlF\*(Qr
(usual hexadecimal notation), with an optional
decimal point.
An underscore \*(Ql_\*(Qr as a prefix indicates a negative
number.
The letters retain values ten through 15, respectively,
regardless of the base chosen by the user.
.IP "\fB+ - / * % ^\fR"
.br
The arithmetic operations:
addition(+), subtraction(-), division(/),
multiplication(*), remainder(%), and exponentiation(^).
.B dc
pops the two top stack elements,
performs the desired operation by calling the multiprecision
routine desired (see
.BR "multiprecision arithmetic" ),
and stacks the result.
.IP \fBc\fR
Clear the stack.
.IP \fBd\fR
Duplicate the top of the stack (so that it occupies
the top two positions of the stack).
.IP \fBf\fR
Print the contents of the stack and the values of all registers.
.IP \fBi\fR
Remove the top of the stack and use its integer part as the
assumed input base (default, ten).
The new input base must be greater than one and less than 17.
.IP \fBI\fR
Stack the current assumed input base.
.IP \fBk\fR
Remove the top of the stack and put it in the internal scale factor register.
.IP \fBK\fR
Put the value of the internal scale register (which the
.B k
command sets) on the top of the stack.
.IP "\fBl \fIx\fR"
Load the value of register
.I x
to the top of the stack.
The value of register
.I x
is unaltered.
.I x
may be any character.
.IP \fBo\fR
Remove the top of the stack and use its integer part as the
assumed output base (default, ten).
The specified base may be any positive integer.
.IP \fBO\fR
Stack the current assumed output base.
.IP \fBp\fR
Print the top of the stack.
The value remains on the stack.
.IP \fBq\fR
Quit the program; control returns to the
shell
.BR sh .
.IP "\fBs \fIx\fR"
Remove the top of the stack and store it in register
.I x.
The previous contents of
.I x
are overwritten.
.I x
may be any character.
.IP \fBv\fR
Replace the top of the stack by its square root.
.IP \fBx\fR
Remove the top of the stack,
interpret it as a string containing a sequence of
.B dc
commands,
and execute it.
.IP \fBX\fR
Replace the top of the stack by its scale
factor (i.e., the number of decimal places it has).
.IP \fBz\fR
Place the number of occupied levels of the stack on top of the
stack.
.IP \fB[...]\fR
Place the bracketed character string on top of the stack.
The string may be executed subsequently with the
.B x
command.
.IP "\fB<\fIx \fB>\fIx \fB=\fIx \fB!<\fIx \fB!>\fIx \fB!=\fIx\fR"
.br
Remove the top two elements of the stack and compare them.
If there is no \*(Ql!\*(Qr sign
before the relation, execute register
.I x
if the two elements obey the relation.
If a \*(Ql!\*(Qr sign is
present, execute register
.I x
if the elements do not obey the relation.
.IP \fB!\fR
Interpret the rest of the line as a command to the shell
.BR sh .
Control returns to
.B dc
after command execution terminates.
.SH Example
.PP
The following example
program prints the first 20 Fibbonacci numbers.
The character \fBl\fR is printed in boldface to help you
tell from a numeric one.
.DM
	1sa1sb1sc
	[\fBl\fPa\fBl\fPbdsa+psb\fBl\fPc1+dsc21<y]sy
	\fBl\fPyx
.DE
.SH "See Also"
.Xr "bc," bc
.Xr "commands" commands
.SH Notes
For most purposes, the in-fix notation of
.B bc
is more convenient than the Polish notation of
.BR dc .
