xref: /386bsd/usr/src/usr.bin/dc/dc.1 (revision a2142627)
1.\'
2.\'    This file documents DC, an arbitrary precision calculator.
3.\'
4.\'    Published by the Free Software Foundation, 675 Massachusetts Avenue,
5.\' Cambridge, MA 02139 USA
6.\'
7.\'    Copyright (C) 1984 Free Software Foundation, Inc.
8.\'
9.\'    Permission is granted to make and distribute verbatim copies of this
10.\' manual provided the copyright notice and this permission notice are
11.\' preserved on all copies.
12.\'
13.\'    Permission is granted to copy and distribute modified versions of
14.\' this manual under the conditions for verbatim copying, provided that
15.\' the entire resulting derived work is distributed under the terms of a
16.\' permission notice identical to this one.
17.\'
18.\'    Permission is granted to copy and distribute translations of this
19.\' manual into another language, under the above conditions for modified
20.\' versions, except that this permission notice may be stated in a
21.\' translation approved by the Foundation.
22.\'
23.Dd March 1, 1993
24.Dt DC 1
25.Os BSD 4
26.Sh NAME
27.Nm dc
28.Nd desk calculator
29.Sh SYNOPSIS
30.Nm dc
31.Sh DESCRIPTION
32DC is a reverse-polish desk calculator which supports unlimited
33precision arithmetic.  It also allows you to define and call macros.
34Normally DC reads from the standard input; if any command arguments are
35given to it, they are filenames, and DC reads and executes the contents
36of the files before reading from standard input.  All output is to
37standard output.
38.Pp
39To exit, use `q'.  `C-c' does not exit; it is used to abort macros
40that are looping, etc.  (Currently this is not true; `C-c' does exit.)
41.Pp
42A reverse-polish calculator stores numbers on a stack.  Entering a
43number pushes it on the stack.  Arithmetic operations pop arguments off
44the stack and push the results.
45.Pp
46To enter a number in DC, type the digits, with an optional decimal
47point.  Exponential notation is not supported.  To enter a negative
48number, begin the number with `_'.  `-' cannot be used for this, as it
49is a binary operator for subtraction instead.  To enter two numbers in
50succession, separate them with spaces or newlines.  These have no
51meaning as commands.
52.Sh Printing Commands
53.Bl -tag -width Ds
54`p'
55     Prints the value on the top of the stack, without altering the
56     stack.  A newline is printed after the value.
57
58`P'
59     Prints the value on the top of the stack, popping it off, and does
60     not print a newline after.
61
62`f'
63     Prints the entire contents of the stack and the contents of all of
64     the registers, without altering anything.  This is a good command
65     to use if you are lost or want to figure out what the effect of
66     some command has been.
67.El
68.Sh Arithmetic
69.Bl -tag -width Ds
70`+'
71     Pops two values off the stack, adds them, and pushes the result.
72     The precision of the result is determined only by the values of
73     the arguments, and is enough to be exact.
74
75`-'
76     Pops two values, subtracts the first one popped from the second
77     one popped, and pushes the result.
78
79`*'
80     Pops two values, multiplies them, and pushes the result.  The
81     number of fraction digits in the result is controlled by the
82     current precision flag (see below) and does not depend on the
83     values being multiplied.
84
85`/'
86     Pops two values, divides the second one popped from the first one
87     popped, and pushes the result.  The number of fraction digits is
88     specified by the precision flag.
89
90`%'
91     Pops two values, computes the remainder of the division that the
92     `/' command would do, and pushes that.  The division is done with
93     as many fraction digits as the precision flag specifies, and the
94     remainder is also computed with that many fraction digits.
95
96`^'
97     Pops two values and exponentiates, using the first value popped as
98     the exponent and the second popped as the base.  The fraction part
99     of the exponent is ignored.  The precision flag specifies the
100     number of fraction digits in the result.
101
102`v'
103     Pops one value, computes its square root, and pushes that.  The
104     precision flag specifies the number of fraction digits in the
105     result.
106.El
107.Pp
108Most arithmetic operations are affected by the "precision flag",
109which you can set with the `k' command.  The default precision value is
110zero, which means that all arithmetic except for addition and
111subtraction produces integer results.
112.Pp
113The remainder operation (`%') requires some explanation: applied to
114arguments `a' and `b' it produces `a - (b * (a / b))', where `a / b' is
115computed in the current precision.
116.Sh Stack Control
117.Bl -tag -width Ds
118`c'
119     Clears the stack, rendering it empty.
120
121`d'
122     Duplicates the value on the top of the stack, pushing another copy
123     of it.  Thus, `4d*p' computes 4 squared and prints it.
124
125.El
126.Sh Registers
127.Pp
128DC provides 128 memory registers, each named by a single ASCII
129character.  You can store a number in a register and retrieve it later.
130.Pp
131.Bl -tag -width Ds
132`sR'
133     Pop the value off the top of the stack and store it into register
134     R.
135
136`lR'
137     Copy the value in register R, and push it onto the stack.  This
138     does not alter the contents of R.
139
140     Each register also contains its own stack.  The current register
141     value is the top of the register's stack.
142
143`SR'
144     Pop the value off the top of the (main) stack and push it onto the
145     stack of register R.  The previous value of the register becomes
146     inaccessible.
147
148`LR'
149     Pop the value off the top of register R's stack and push it onto
150     the main stack.  The previous value in register R's stack, if any,
151     is now accessible via the `lR' command.
152.El
153.Pp
154The `f' command prints a list of all registers that have contents
155stored in them, together with their contents.  Only the current
156contents of each register (the top of its stack) is printed.
157.Sh Parameters
158.Pp
159DC has three parameters that control its operation: the precision,
160the input radix, and the output radix.  The precision specifies the
161number of fraction digits to keep in the result of most arithmetic
162operations.  The input radix controls the interpretation of numbers
163typed in; *all* numbers typed in use this radix.  The output radix is
164used for printing numbers.
165.Pp
166The input and output radices are separate parameters; you can make
167them unequal, which can be useful or confusing.  Each radix must be
168between 2 and 36 inclusive.  The precision must be zero or greater.
169The precision is always measured in decimal digits, regardless of the
170current input or output radix.
171.Pp
172.Bl -tag -width Ds
173`i'
174     Pops the value off the top of the stack and uses it to set the
175     input radix.
176
177`o'
178`k'
179     Similarly set the output radix and the precision.
180
181`I'
182     Pushes the current input radix on the stack.
183
184`O'
185`K'
186     Similarly push the current output radix and the current precision.
187.El
188.Sh Strings
189.Pp
190DC can operate on strings as well as on numbers.  The only things you
191can do with strings are print them and execute them as macros (which
192means that the contents of the string are processed as DC commands).
193Both registers and the stack can hold strings, and DC always knows
194whether any given object is a string or a number.  Some commands such as
195arithmetic operations demand numbers as arguments and print errors if
196given strings.  Other commands can accept either a number or a string;
197for example, the `p' command can accept either and prints the object
198according to its type.
199.Bl -tag -width Ds
200`[CHARACTERS]'
201     Makes a string containing CHARACTERS and pushes it on the stack.
202     For example, `[foo]P' prints the characters `foo' (with no
203     newline).
204
205`x'
206     Pops a value off the stack and executes it as a macro.  Normally
207     it should be a string; if it is a number, it is simply pushed back
208     onto the stack.  For example, `[1p]x' executes the macro `1p',
209     which pushes 1 on the stack and prints `1' on a separate line.
210
211     Macros are most often stored in registers; `[1p]sa' stores a macro
212     to print `1' into register `a', and `lax' invokes the macro.
213
214`>R'
215     Pops two values off the stack and compares them assuming they are
216     numbers, executing the contents of register R as a macro if the
217     original top-of-stack is greater.  Thus, `1 2>a' will invoke
218     register `a''s contents and `2 1>a' will not.
219
220`<R'
221     Similar but invokes the macro if the original top-of-stack is less.
222
223`=R'
224     Similar but invokes the macro if the two numbers popped are equal.
225     This can also be validly used to compare two strings for equality.
226
227`?'
228     Reads a line from the terminal and executes it.  This command
229     allows a macro to request input from the user.
230
231`q'
232     During the execution of a macro, this comand does not exit DC.
233     Instead, it exits from that macro and also from the macro which
234     invoked it (if any).
235
236`Q'
237     Pops a value off the stack and uses it as a count of levels of
238     macro execution to be exited.  Thus, `3Q' exits three levels.
239.El
240.Sh Status Inquiry
241.Bl -tag -width Ds
242`Z'
243     Pops a value off the stack, calculates the number of digits it has
244     (or number of characters, if it is a string) and pushes that
245     number.
246
247`X'
248     Pops a value off the stack, calculates the number of fraction
249     digits it has, and pushes that number.  For a string, the value
250     pushed is -1.
251
252`z'
253     Pushes the current stack depth; the number of objects on the stack
254     before the execution of the `z' command.
255
256`I'
257     Pushes the current value of the input radix.
258
259`O'
260     Pushes the current value of the output radix.
261
262`K'
263     Pushes the current value of the precision.
264.El
265.Sh Bugs
266.Pp
267The `:' and `;' commands of the Unix DC program are not supported,
268as the documentation does not say what they do.  The `!' command is not
269supported, but will be supported as soon as a library for executing a
270line as a command exists.
271.Sh SEE ALSO
272.Xr bc 1
273