'\" te
.\"  Copyright 1989 AT&T  Copyright (c) 2003, Sun Microsystems, Inc.  All Rights Reserved
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH DC 1 "Aug 29, 2003"
.SH NAME
dc \- desk calculator
.SH SYNOPSIS
.LP
.nf
\fB/usr/bin/dc\fR [\fIfilename\fR]
.fi

.LP
.nf
\fB/usr/xpg6/bin/dc\fR [\fIfilename\fR]
.fi

.SH DESCRIPTION
.sp
.LP
\fBdc\fR is an arbitrary precision arithmetic package. Ordinarily it operates
on decimal integers, but one may specify an input base, output base, and a
number of fractional digits to be maintained. The overall structure of \fBdc\fR
is a stacking (reverse Polish) calculator. If an argument is given, input is
taken from that file until its end, then from the standard input.
.sp
.LP
\fBbc\fR is a preprocessor for \fBdc\fR that provides infix notation and a
C-like syntax that implements functions. \fBbc\fR also provides reasonable
control structures for programs. See \fBbc\fR(1).
.SH USAGE
.SS "/usr/bin/dc, /usr/xpg6/bin/dc"
.sp
.LP
The following constructions are recognized under both \fB/usr/bin/dc\fR and
\fB/usr/xpg6/bin/dc\fR:
.sp
.ne 2
.na
\fB\fInumber\fR\fR
.ad
.RS 15n
The value of the number is pushed on the stack. A number is an  unbroken string
of the digits 0\(mi9. It may be preceded by an underscore (\fB_\fR) to input a
negative number. Numbers may  contain decimal points.
.RE

.sp
.ne 2
.na
\fB\fBs\fR\fIx\fR\fR
.ad
.RS 15n
The top of the stack is popped and stored into a register named \fIx\fR, where
\fIx\fR may be any character.  If the \fBs\fR is capitalized, \fIx\fR is
treated as a stack and the value is pushed on it.
.RE

.sp
.ne 2
.na
\fB\fBl\fR\fIx\fR\fR
.ad
.RS 15n
The value in register \fIx\fR is pushed on the stack. The register \fIx\fR is
not altered. All registers start with zero value.  If the \fBl\fR is
capitalized, register \fIx\fR is treated as a stack and its top value is popped
onto the main stack.
.RE

.sp
.ne 2
.na
\fB\fBd\fR\fR
.ad
.RS 15n
The top value on the stack is duplicated.
.RE

.sp
.ne 2
.na
\fB\fBp\fR\fR
.ad
.RS 15n
The top value on the stack is printed. The top value remains  unchanged.
.RE

.sp
.ne 2
.na
\fB\fBP\fR\fR
.ad
.RS 15n
Interprets the top of the stack as an ASCII string, removes it,  and prints it.
.RE

.sp
.ne 2
.na
\fB\fBf\fR\fR
.ad
.RS 15n
All values on the stack are printed.
.RE

.sp
.ne 2
.na
\fB\fBq\fR\fR
.ad
.RS 15n
Exits the program. If executing a string, the recursion level is popped by two.
.RE

.sp
.ne 2
.na
\fB\fBQ\fR\fR
.ad
.RS 15n
Exits the program.  The top value on the stack is popped and the string
execution level is popped by that value.
.RE

.sp
.ne 2
.na
\fB\fBx\fR\fR
.ad
.RS 15n
Treats the top element of the stack as a character string and executes it as a
string of \fBdc\fR commands.
.RE

.sp
.ne 2
.na
\fB\fBX\fR\fR
.ad
.RS 15n
Replaces the number on the top of the stack with its scale factor.
.RE

.sp
.ne 2
.na
\fB\fB[ ... ]\fR\fR
.ad
.RS 15n
Puts the bracketed ASCII string onto the top of the stack.
.RE

.sp
.ne 2
.na
\fB\fB<\fR\fIx \fR \fB>\fR\fIx \fR \fB=\fR\fIx \fR\fR
.ad
.RS 15n
The top two elements of the stack are popped and compared. Register \fIx\fR is
evaluated if they obey the stated relation.
.RE

.sp
.ne 2
.na
\fB\fBv\fR\fR
.ad
.RS 15n
Replaces the top element on the stack by its square root.  Any  existing
fractional part of the argument is taken into account, but  otherwise the scale
factor is ignored.
.RE

.sp
.ne 2
.na
\fB\fB!\fR\fR
.ad
.RS 15n
Interprets the rest of the line as a shell command.
.RE

.sp
.ne 2
.na
\fB\fBc\fR\fR
.ad
.RS 15n
All values on the stack are popped.
.RE

.sp
.ne 2
.na
\fB\fBi\fR\fR
.ad
.RS 15n
The top value on the stack is popped and used as the number radix  for further
input.
.RE

.sp
.ne 2
.na
\fB\fBI\fR\fR
.ad
.RS 15n
Pushes the input base on the top of the stack.
.RE

.sp
.ne 2
.na
\fB\fBo\fR\fR
.ad
.RS 15n
The top value on the stack is popped and used as the number radix for  further
output.
.RE

.sp
.ne 2
.na
\fB\fBO\fR\fR
.ad
.RS 15n
Pushes the output base on the top of the stack.
.RE

.sp
.ne 2
.na
\fB\fBk\fR\fR
.ad
.RS 15n
The top of the stack is popped, and that value is used as a  non-negative scale
factor: the appropriate number of places are  printed on output, and maintained
during multiplication, division,  and exponentiation.  The interaction of scale
factor, input base, and  output base will be reasonable if all are changed
together.
.RE

.sp
.ne 2
.na
\fB\fBK\fR\fR
.ad
.RS 15n
Pushes the current scale factor on the top of the stack.
.RE

.sp
.ne 2
.na
\fB\fBz\fR\fR
.ad
.RS 15n
The stack level is pushed onto the stack.
.RE

.sp
.ne 2
.na
\fB\fBZ\fR\fR
.ad
.RS 15n
Replaces the number on the top of the stack with its length.
.RE

.sp
.ne 2
.na
\fB\fB?\fR\fR
.ad
.RS 15n
A line of input is taken from the input source (usually the terminal) and
executed.
.RE

.sp
.ne 2
.na
\fB\fBY\fR\fR
.ad
.RS 15n
Displays \fBdc\fR debugging information.
.RE

.sp
.ne 2
.na
\fB\fB; :\fR\fR
.ad
.RS 15n
Used by  \fBbc\fR(1) for array operations.
.RE

.SS "/usr/bin/dc"
.sp
.LP
The following construction is recognized under \fB/usr/bin/dc\fR, using the
scale of whatever the result is.
.sp
.ne 2
.na
\fB\fB+ \(mi / * % ^\fR\fR
.ad
.RS 18n
The top two values on the stack are added (\fB+\fR), subtracted (\fB\(mi\fR),
multiplied (\fB*\fR), divided (\fB/\fR), remaindered (\fB%\fR), or
exponentiated (\fB^\fR). The two entries are popped off the stack; the result
is pushed on the stack in their place. Any fractional part of an exponent is
ignored.
.RE

.SS "/usr/xpg6/bin/dc"
.sp
.LP
The following construction is recognized under \fB/usr/xpg6/bin/dc\fR. The
results of division are forced to be a scale of 20.
.sp
.ne 2
.na
\fB\fB+ \(mi / * % ^\fR\fR
.ad
.RS 18n
The top two values on the stack are added (\fB+\fR), subtracted (\fB\(mi\fR),
multiplied (\fB*\fR), divided (\fB/\fR), remaindered (\fB%\fR), or
exponentiated (\fB^\fR). The two entries are popped off the stack. The result
is pushed on the stack in their place. Any fractional part of an exponent is
ignored.
.sp
Ensures that the scale set prior to division is the scale of the result.
.RE

.SH EXAMPLES
.LP
\fBExample 1 \fRPrinting the first ten values of n!
.sp
.LP
This example prints the first ten values of n!:

.sp
.in +2
.nf
[la1+dsa*pla10>y]sy
0sa1
lyx
.fi
.in -2
.sp

.SH SEE ALSO
.sp
.LP
\fBbc\fR(1), \fBattributes\fR(5)
.SH DIAGNOSTICS
.sp
.ne 2
.na
\fB\fIx\fR \fBis unimplemented\fR\fR
.ad
.RS 29n
\fIx\fR is an octal number.
.RE

.sp
.ne 2
.na
\fB\fBout of space\fR\fR
.ad
.RS 29n
The free list is exhausted (too many digits).
.RE

.sp
.ne 2
.na
\fB\fBout of stack space\fR\fR
.ad
.RS 29n
Too many pushes onto the stack (stack overflow).
.RE

.sp
.ne 2
.na
\fB\fBempty stack\fR\fR
.ad
.RS 29n
Too many pops from the stack (stack underflow).
.RE

.sp
.ne 2
.na
\fB\fBnesting depth\fR\fR
.ad
.RS 29n
Too many levels of nested execution.
.RE

.sp
.ne 2
.na
\fB\fBdivide by 0\fR\fR
.ad
.RS 29n
Division by zero.
.RE

.sp
.ne 2
.na
\fB\fBsqrt of neg number\fR\fR
.ad
.RS 29n
Square root of a negative number is not defined (no imaginary numbers).
.RE

.sp
.ne 2
.na
\fB\fBexp not an integer\fR\fR
.ad
.RS 29n
\fBdc\fR only processes integer exponentiation.
.RE

.sp
.ne 2
.na
\fB\fBexp too big\fR\fR
.ad
.RS 29n
The largest exponent allowed is 999.
.RE

.sp
.ne 2
.na
\fB\fBinput base is too large\fR\fR
.ad
.RS 29n
The input base x: 2<= x <= 16.
.RE

.sp
.ne 2
.na
\fB\fBinput base is too small\fR\fR
.ad
.RS 29n
The input base x: 2<= x <= 16.
.RE

.sp
.ne 2
.na
\fB\fBoutput base is too large\fR\fR
.ad
.RS 29n
The output base must be no larger than  \fBBC_BASE_MAX\fR.
.RE

.sp
.ne 2
.na
\fB\fBinvalid scale factor\fR\fR
.ad
.RS 29n
Scale factor cannot be less than 1.
.RE

.sp
.ne 2
.na
\fB\fBscale factor is too large\fR\fR
.ad
.RS 29n
A scale factor cannot be larger than  \fBBC_SCALE_MAX\fR.
.RE

.sp
.ne 2
.na
\fB\fBsymbol table overflow\fR\fR
.ad
.RS 29n
Too many variables have been specified.
.RE

.sp
.ne 2
.na
\fB\fBinvalid index\fR\fR
.ad
.RS 29n
Index cannot be less than 1.
.RE

.sp
.ne 2
.na
\fB\fBindex is too large\fR\fR
.ad
.RS 29n
An index cannot be larger than  \fBBC_DIM_MAX\fR.
.RE