dc(1) dc(1)
NAME
dc - desk calculator
SYNOPSIS
dc [file]
DESCRIPTION
dc provides you with the functions of a desk calculator for interac-
tive use in Reliant UNIX.
file Name of the file containing the calculation operations to be exe-
cuted. dc reads file to its end, and then reads from standard
input.
file not specified:
dc reads from standard input.
MODE OF OPERATION OF DC
dc works on the stacking principle. A stack is a linear memory area
that can only be accessed on a LIFO (Last In First Out) basis, i.e.
the last value stored in the stack is located at the top, and is the
only one accessible for operations. If values further down the stack
need to be processed, the values stacked above them must either be
deleted or temporarily stored in some other memory area. This memory
area could be another stack or a register.
A register is an area of rapid-access memory in the CPU.
The memory area with which dc operates is generally referred to as a
"stack" in the following discussion. The term "main stack" is used
only if this memory needs to be differentiated from other memory areas
(other stacks or registers).
dc accepts input in reverse Polish notation (postfix notation). Thus,
to add 3 and 4, you would enter:
3 4 +
dc operates on decimal integers by default. You may define another
base for input and output (see SYNOPSIS OF DC PROGRAMS, i or o), how-
ever, and change the number of fractional digits (see SYNOPSIS OF DC
PROGRAMS, k).
dc is called by bc (see bc). bc is a preprocessor for dc; it allows
you to enter operators and operands in normal arithmetic notation and
provides a C-like syntax that implements functions. Control structures
can also be included in bc programs.
Page 1 Reliant UNIX 5.44 Printed 11/98
dc(1) dc(1)
SYNOPSIS OF DC PROGRAMS
c The entire contents of the stack are popped (deleted).
d The top value on the stack is duplicated (loaded onto the stack a
second time).
f All values on the stack are printed, but retained in the stack.
i The top value on the stack is used as the base for further input;
the value itself is popped from the stack. The default value for
the base is 10, i.e. inputs are interpreted with base 10.
I The current value of the input base is pushed onto the stack.
k The top value on the stack is popped and used as a non-negative
scale factor. In other words, a corresponding number of decimal
places are printed for subsequent output and maintained during
multiplication, division and exponentiation.
lx The contents of register x are pushed onto the stack. Register x
remains unchanged. The initial value of all registers is 0.
Lx The top value is popped, i.e. deleted, from stack x and pushed
onto the main stack.
o The top value on the stack is popped and used as the base for
further output. Numbers are output to the base 10 by default.
O The currently valid base for output is pushed onto the top of the
stack.
p The top value on the stack is printed but not removed from the
stack.
P The top value on the stack is interpreted as an ASCII string and
printed. In this case, the value is removed from the stack.
q Terminates the program. If a loop is being executed, the recur-
sion level is popped by two.
Q Terminates the program. The top value is popped from the stack
and the execution level of the loop is popped by that value.
sx The top value of the stack is popped and loaded into a register
named x, i.e. stored as the top element in register x. x may be
any character.
Sx The top value of the main stack is popped and pushed onto a stack
named x, i.e. stored as the top element in stack x. x may be any
character.
Page 2 Reliant UNIX 5.44 Printed 11/98
dc(1) dc(1)
v The top value on the stack is replaced by its square root, taking
all the fractional part into account (even if the applicable
scale factor is less than the number of existing fractional
digits, see k).
x The top value on the stack is interpreted as a series of state-
ments to the dc command and executed.
X The last loaded value is replaced by the currently valid number
of fractional digits.
z The stack level (the number of elements it contains is pushed
onto the stack.
Z The top number on the stack is replaced by its length.
number
The value of number is pushed onto the stack. number is a string
comprising the digits 0-9 and periods and may be preceded by an
underscore. The first period is interpreted as a decimal point;
all others are ignored.
A preceding underscore is interpreted as a negative sign.
! The remainder of the line from the exclamation point onward is
interpreted as a Reliant UNIX command.
+
-
/
*
%
^ The top two values on the stack have one of the specified opera-
tions applied to them. The meanings are:
+ addition
- subtraction
/ division
* multiplication
% remaindering
^ exponentiation
Both entries are popped from the stack, and the result of the
executed operation is pushed onto the stack instead.
;: bc uses the characters ; and : for array operations.
Page 3 Reliant UNIX 5.44 Printed 11/98
dc(1) dc(1)
<x
>x
=x The top two elements of the stack are popped and compared. If the
specified relation is satisfied, i.e. the topmost value is
respectively less than, greater than, or equal to the second
value, register x is evaluated.
? A line of input is taken from the input device (usually the
display terminal) and executed.
[...]
The bracketed ASCII string is placed on top of the stack.
ERROR MESSAGES
x is unimplemented
x is an octal number that is not a valid statement for dc.
stack empty
Not enough elements on the stack to do what was asked.
Out of space
No more space in the stack (too many digits).
Out of headers
Too many numbers.
Out of pushdown
Too many items on the stack.
Nesting Depth
Too many levels of nested execution.
exp not an integer
The exponent is not an integer.
EXAMPLES
Example 1
In this example, the numbers 0-9 will be printed:
[lip1+sili10>a]sa
0si lax
0
1
2
3
4
5
6
7
8
Page 4 Reliant UNIX 5.44 Printed 11/98
dc(1) dc(1)
9
dc processes this entry as follows: The string lip1+sili10>a is first
pushed on the top of the stack ([]) and then popped from it and stored
in register a (sa).
0si causes the number 0 to be stored in register i. The string
lip1+sili10>a is then pushed onto the stack, but remains stored in
register a as well. The command x causes the string lip1+sili10>a to
be interpreted as a series of dc commands and to be evaluated accord-
ingly.
The resulting sequence of operations is as follows: the number 0 (con-
tents of register i) is pushed on the stack and printed, etc. Example
2
This example prints the first ten values of n!:
[la1+dsa*pla10>y]sy
0sa1
lyx
1
2
6
24
120
720
5040
40320
362880
3628800
SEE ALSO
bc(1).
Page 5 Reliant UNIX 5.44 Printed 11/98