2 .\" dc.1 - the *roff document processor source for the dc manual
4 .\" This file is part of GNU dc.
5 .\" Copyright (C) 1994, 1997, 1998, 2000 Free Software Foundation, Inc.
7 .\" This program is free software; you can redistribute it and/or modify
8 .\" it under the terms of the GNU General Public License as published by
9 .\" the Free Software Foundation; either version 2 of the License , or
10 .\" (at your option) any later version.
12 .\" This program is distributed in the hope that it will be useful,
13 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
14 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 .\" GNU General Public License for more details.
17 .\" You should have received a copy of the GNU General Public License
18 .\" along with this program; see the file COPYING. If not, write to:
19 .\" The Free Software Foundation, Inc.
20 .\" 59 Temple Place, Suite 330
21 .\" Boston, MA 02111 USA
23 .TH DC 1 "1997-03-25" "GNU Project"
27 dc \- an arbitrary precision calculator
29 dc [-V] [--version] [-h] [--help]
30 [-e scriptexpression] [--expression=scriptexpression]
31 [-f scriptfile] [--file=scriptfile]
35 \*(Dc is a reverse-polish desk calculator which supports
36 unlimited precision arithmetic.
37 It also allows you to define and call macros.
38 Normally \*(dc reads from the standard input;
39 if any command arguments are given to it, they are filenames,
40 and \*(dc reads and executes the contents of the files before reading
42 All normal output is to standard output;
43 all error output is to standard error.
45 A reverse-polish calculator stores numbers on a stack.
46 Entering a number pushes it on the stack.
47 Arithmetic operations pop arguments off the stack and push the results.
51 type the digits with an optional decimal point.
52 Exponential notation is not supported.
53 To enter a negative number,
54 begin the number with ``_''.
55 ``-'' cannot be used for this,
56 as it is a binary operator for subtraction instead.
57 To enter two numbers in succession,
58 separate them with spaces or newlines.
59 These have no meaning as commands.
61 \*(Dc may be invoked with the following command-line options:
66 Print out the version of \*(dc that is being run and a copyright notice,
72 Print a usage message briefly summarizing these command-line options
73 and the bug-reporting address,
78 .BI --expression= script
81 to the set of commands to be run while processing the input.
83 .B -f \fIscript-file\fP
85 .BI --file= script-file
86 Add the commands contained in the file
88 to the set of commands to be run while processing the input.
90 If any command-line parameters remain after processing the above,
91 these parameters are interpreted as the names of input files to
95 refers to the standard input stream.
96 The standard input will processed if no file names are specified.
102 Prints the value on the top of the stack,
103 without altering the stack.
104 A newline is printed after the value.
107 Prints the value on the top of the stack, popping it off,
108 and does not print a newline after.
111 Pops off the value on top of the stack.
112 If it it a string, it is simply printed without a trailing newline.
113 Otherwise it is a number, and the integer portion of its absolute
114 value is printed out as a "base (UCHAR_MAX+1)" byte stream.
115 Assuming that (UCHAR_MAX+1) is 256
116 (as it is on most machines with 8-bit bytes),
117 the sequence \fBKSK 0k1/ [_1*]sx d0>x [256~aPd0<x]dsxx sxLKk\fP
118 could also accomplish this function,
119 except for the side-effect of clobbering the x register.
122 Prints the entire contents of the stack
124 and the contents of all of the registers,
126 without altering anything.
127 This is a good command to use if you are lost or want
128 to figure out what the effect of some command has been.
134 Pops two values off the stack, adds them,
135 and pushes the result.
136 The precision of the result is determined only
137 by the values of the arguments,
138 and is enough to be exact.
142 subtracts the first one popped from the second one popped,
143 and pushes the result.
146 Pops two values, multiplies them, and pushes the result.
147 The number of fraction digits in the result depends on
148 the current precision value and the number of fraction
149 digits in the two arguments.
153 divides the second one popped from the first one popped,
154 and pushes the result.
155 The number of fraction digits is specified by the precision value.
159 computes the remainder of the division that the
163 The value computed is the same as that computed by
164 the sequence \fBSd dld/ Ld*-\fP .
168 divides the second one popped from the first one popped.
169 The quotient is pushed first, and the remainder is pushed next.
170 The number of fraction digits used in the division
171 is specified by the precision value.
172 (The sequence \fBSdSn lnld/ LnLd%\fP could also accomplish
173 this function, with slightly different error checking.)
176 Pops two values and exponentiates,
177 using the first value popped as the exponent
178 and the second popped as the base.
179 The fraction part of the exponent is ignored.
180 The precision value specifies the number of fraction
181 digits in the result.
184 Pops three values and computes a modular exponentiation.
185 The first value popped is used as the reduction modulus;
186 this value must be a non-zero number,
187 and should be an integer.
188 The second popped is used as the exponent;
189 this value must be a non-negative number,
190 and any fractional part of this exponent will be ignored.
191 The third value popped is the base which gets exponentiated,
192 which should be an integer.
193 For small integers this is like the sequence \fBSm^Lm%\fP,
194 but, unlike \fB^\fP, this command will work with arbitrarily large exponents.
198 computes its square root,
200 The precision value specifies the number of fraction digits in the result.
202 Most arithmetic operations are affected by the ``precision value'',
203 which you can set with the
206 The default precision value is zero,
207 which means that all arithmetic except for
208 addition and subtraction produces integer results.
213 Clears the stack, rendering it empty.
216 Duplicates the value on the top of the stack,
217 pushing another copy of it.
218 Thus, ``4d*p'' computes 4 squared and prints it.
221 Reverses the order of (swaps) the top two values on the stack.
225 \*(Dc provides at least 256 memory registers,
226 each named by a single character.
227 You can store a number or a string in a register and retrieve it later.
230 Pop the value off the top of the stack and store
235 Copy the value in register
237 and push it onto the stack.
238 This does not alter the contents of
241 Each register also contains its own stack.
242 The current register value is the top of the register's stack.
245 Pop the value off the top of the (main) stack and
246 push it onto the stack of register
248 The previous value of the register becomes inaccessible.
251 Pop the value off the top of register
253 stack and push it onto the main stack.
258 is now accessible via the
265 command prints a list of all registers that have contents stored in them,
266 together with their contents.
267 Only the current contents of each register
268 (the top of its stack)
274 \*(Dc has three parameters that control its operation:
275 the precision, the input radix, and the output radix.
276 The precision specifies the number
277 of fraction digits to keep in the result of most arithmetic operations.
278 The input radix controls the interpretation of numbers typed in;
279 all numbers typed in use this radix.
280 The output radix is used for printing numbers.
282 The input and output radices are separate parameters;
283 you can make them unequal,
284 which can be useful or confusing.
285 The input radix must be between 2 and 16 inclusive.
286 The output radix must be at least 2.
287 The precision must be zero or greater.
288 The precision is always measured in decimal digits,
289 regardless of the current input or output radix.
292 Pops the value off the top of the stack
293 and uses it to set the input radix.
296 Pops the value off the top of the stack
297 and uses it to set the output radix.
300 Pops the value off the top of the stack
301 and uses it to set the precision.
304 Pushes the current input radix on the stack.
307 Pushes the current output radix on the stack.
310 Pushes the current precision on the stack.
314 \*(Dc can operate on strings as well as on numbers.
315 The only things you can do with strings are
316 print them and execute them as macros
317 (which means that the contents of the string are processed as
319 All registers and the stack can hold strings,
320 and \*(dc always knows whether any given object is a string or a number.
321 Some commands such as arithmetic operations demand numbers
322 as arguments and print errors if given strings.
323 Other commands can accept either a number or a string;
326 command can accept either and prints the object
327 according to its type.
330 Makes a string containing
332 (contained between balanced
337 and pushes it on the stack.
340 prints the characters
345 The top-of-stack is popped.
346 If it was a number, then the low-order byte of this number
347 is converted into a string and pushed onto the stack.
348 Otherwise the top-of-stack was a string,
349 and the first character of that string is pushed back.
352 Pops a value off the stack and executes it as a macro.
353 Normally it should be a string;
355 it is simply pushed back onto the stack.
362 on the stack and prints
366 Macros are most often stored in registers;
368 stores a macro to print
377 Pops two values off the stack and compares them
378 assuming they are numbers,
379 executing the contents of register
381 as a macro if the original top-of-stack
392 Similar but invokes the macro if the original top-of-stack is
393 not greater than (less than or equal to) what was the second-to-top.
396 Similar but invokes the macro if the original top-of-stack is less.
399 Similar but invokes the macro if the original top-of-stack is
400 not less than (greater than or equal to) what was the second-to-top.
403 Similar but invokes the macro if the two numbers popped are equal.
406 Similar but invokes the macro if the two numbers popped are not equal.
408 This can also be validly used to compare two strings for equality.
412 Reads a line from the terminal and executes it.
413 This command allows a macro to request input from the user.
416 exits from a macro and also from the macro which invoked it.
417 If called from the top level,
418 or from a macro which was called directly from the top level,
421 command will cause \*(dc to exit.
424 Pops a value off the stack and uses it as a count
425 of levels of macro execution to be exited.
431 command will never cause \*(dc to exit.
436 Pops a value off the stack,
437 calculates the number of digits it has
438 (or number of characters, if it is a string)
439 and pushes that number.
442 Pops a value off the stack,
443 calculates the number of fraction digits it has,
444 and pushes that number.
451 Pushes the current stack depth:
452 the number of objects on the stack before the execution of the
459 Will run the rest of the line as a system command.
460 Note that parsing of the !<, !=, and !> commands take precedence,
461 so if you want to run a command starting with <, =, or > you will
462 need to add a space after the !.
465 Will interpret the rest of the line as a comment.
468 Will pop the top two values off of the stack.
469 The old second-to-top value will be stored in the array
471 indexed by the old top-of-stack value.
474 Pops the top-of-stack and uses it as an index into
477 The selected value is then pushed onto the stack.
479 Note that each stacked instance of a register has its own
480 array associated with it.
481 Thus \fB1 0:a 0Sa 2 0:a La 0;ap\fP will print 1,
482 because the 2 was stored in an instance of 0:a that