]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/bc/doc/dc.1
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / bc / doc / dc.1
1 .\"
2 .\" dc.1 - the *roff document processor source for the dc manual
3 .\"
4 .\" This file is part of GNU dc.
5 .\" Copyright (C) 1994, 1997, 1998, 2000 Free Software Foundation, Inc.
6 .\"
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.
11 .\"
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.
16 .\"
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
22 .\"
23 .\" $FreeBSD$
24 .\"
25 .TH DC 1 "1997-03-25" "GNU Project"
26 .ds dc \fIdc\fP
27 .ds Dc \fIDc\fP
28 .SH NAME
29 dc \- an arbitrary precision calculator
30 .SH SYNOPSIS
31 dc [-V] [--version] [-h] [--help]
32    [-e scriptexpression] [--expression=scriptexpression]
33    [-f scriptfile] [--file=scriptfile]
34    [file ...]
35 .SH DESCRIPTION
36 .PP
37 \*(Dc is a reverse-polish desk calculator which supports
38 unlimited precision arithmetic.
39 It also allows you to define and call macros.
40 Normally \*(dc reads from the standard input;
41 if any command arguments are given to it, they are filenames,
42 and \*(dc reads and executes the contents of the files before reading
43 from standard input.
44 All normal output is to standard output;
45 all error output is to standard error.
46 .PP
47 A reverse-polish calculator stores numbers on a stack.
48 Entering a number pushes it on the stack.
49 Arithmetic operations pop arguments off the stack and push the results.
50 .PP
51 To enter a number in
52 .IR dc ,
53 type the digits with an optional decimal point.
54 Exponential notation is not supported.
55 To enter a negative number,
56 begin the number with ``_''.
57 ``-'' cannot be used for this,
58 as it is a binary operator for subtraction instead.
59 To enter two numbers in succession,
60 separate them with spaces or newlines.
61 These have no meaning as commands.
62 .SH OPTIONS
63 \*(Dc may be invoked with the following command-line options:
64 .TP
65 .B -V
66 .TP
67 .B --version
68 Print out the version of \*(dc that is being run and a copyright notice,
69 then exit.
70 .TP
71 .B -h
72 .TP
73 .B --help
74 Print a usage message briefly summarizing these command-line options
75 and the bug-reporting address,
76 then exit.
77 .TP
78 .B -e \fIscript\fP
79 .TP
80 .BI --expression= script
81 Add the commands in
82 .I script
83 to the set of commands to be run while processing the input.
84 .TP
85 .B -f \fIscript-file\fP
86 .TP
87 .BI --file= script-file
88 Add the commands contained in the file
89 .I script-file
90 to the set of commands to be run while processing the input.
91 .PP
92 If any command-line parameters remain after processing the above,
93 these parameters are interpreted as the names of input files to
94 be processed.
95 A file name of
96 .B -
97 refers to the standard input stream.
98 The standard input will processed if no file names are specified.
99 .PD
100 .SH
101 Printing Commands
102 .TP
103 .B p
104 Prints the value on the top of the stack,
105 without altering the stack.
106 A newline is printed after the value.
107 .TP
108 .B n
109 Prints the value on the top of the stack, popping it off,
110 and does not print a newline after.
111 .TP
112 .B P
113 Pops off the value on top of the stack.
114 If it it a string, it is simply printed without a trailing newline.
115 Otherwise it is a number, and the integer portion of its absolute
116 value is printed out as a "base (UCHAR_MAX+1)" byte stream.
117 Assuming that (UCHAR_MAX+1) is 256
118 (as it is on most machines with 8-bit bytes),
119 the sequence \fBKSK 0k1/ [_1*]sx d0>x [256~aPd0<x]dsxx sxLKk\fP
120 could also accomplish this function,
121 except for the side-effect of clobbering the x register.
122 .TP
123 .B f
124 Prints the entire contents of the stack
125 .ig
126 and the contents of all of the registers,
127 ..
128 without altering anything.
129 This is a good command to use if you are lost or want
130 to figure out what the effect of some command has been.
131 .PD
132 .SH
133 Arithmetic
134 .TP
135 .B +
136 Pops two values off the stack, adds them,
137 and pushes the result.
138 The precision of the result is determined only
139 by the values of the arguments,
140 and is enough to be exact.
141 .TP
142 .B -
143 Pops two values,
144 subtracts the first one popped from the second one popped,
145 and pushes the result.
146 .TP
147 .B *
148 Pops two values, multiplies them, and pushes the result.
149 The number of fraction digits in the result depends on
150 the current precision value and the number of fraction
151 digits in the two arguments.
152 .TP
153 .B /
154 Pops two values,
155 divides the second one popped from the first one popped,
156 and pushes the result.
157 The number of fraction digits is specified by the precision value.
158 .TP
159 .B %
160 Pops two values,
161 computes the remainder of the division that the
162 .B /
163 command would do,
164 and pushes that.
165 The value computed is the same as that computed by
166 the sequence \fBSd dld/ Ld*-\fP .
167 .TP
168 .B ~
169 Pops two values,
170 divides the second one popped from the first one popped.
171 The quotient is pushed first, and the remainder is pushed next.
172 The number of fraction digits used in the division
173 is specified by the precision value.
174 (The sequence \fBSdSn lnld/ LnLd%\fP could also accomplish
175 this function, with slightly different error checking.)
176 .TP
177 .B ^
178 Pops two values and exponentiates,
179 using the first value popped as the exponent
180 and the second popped as the base.
181 The fraction part of the exponent is ignored.
182 The precision value specifies the number of fraction
183 digits in the result.
184 .TP
185 .B |
186 Pops three values and computes a modular exponentiation.
187 The first value popped is used as the reduction modulus;
188 this value must be a non-zero number,
189 and should be an integer.
190 The second popped is used as the exponent;
191 this value must be a non-negative number,
192 and any fractional part of this exponent will be ignored.
193 The third value popped is the base which gets exponentiated,
194 which should be an integer.
195 For small integers this is like the sequence \fBSm^Lm%\fP,
196 but, unlike \fB^\fP, this command will work with arbitrarily large exponents.
197 .TP
198 .B v
199 Pops one value,
200 computes its square root,
201 and pushes that.
202 The precision value specifies the number of fraction digits in the result.
203 .PP
204 Most arithmetic operations are affected by the ``precision value'',
205 which you can set with the
206 .B k
207 command.
208 The default precision value is zero,
209 which means that all arithmetic except for
210 addition and subtraction produces integer results.
211 .SH
212 Stack Control
213 .TP
214 .B c
215 Clears the stack, rendering it empty.
216 .TP
217 .B d
218 Duplicates the value on the top of the stack,
219 pushing another copy of it.
220 Thus, ``4d*p'' computes 4 squared and prints it.
221 .TP
222 .B r
223 Reverses the order of (swaps) the top two values on the stack.
224 .SH
225 Registers
226 .PP
227 \*(Dc provides at least 256 memory registers,
228 each named by a single character.
229 You can store a number or a string in a register and retrieve it later.
230 .TP
231 .BI s r
232 Pop the value off the top of the stack and store
233 it into register
234 .IR r .
235 .TP
236 .BI l r
237 Copy the value in register
238 .I r
239 and push it onto the stack.
240 This does not alter the contents of
241 .IR r .
242 .PP
243 Each register also contains its own stack.
244 The current register value is the top of the register's stack.
245 .TP
246 .BI S r
247 Pop the value off the top of the (main) stack and
248 push it onto the stack of register
249 .IR r .
250 The previous value of the register becomes inaccessible.
251 .TP
252 .BI L r
253 Pop the value off the top of register
254 .IR r 's
255 stack and push it onto the main stack.
256 The previous value
257 in register
258 .IR r 's
259 stack, if any,
260 is now accessible via the
261 .BI l r
262 command.
263 .ig
264 .PP
265 The
266 .B f
267 command prints a list of all registers that have contents stored in them,
268 together with their contents.
269 Only the current contents of each register
270 (the top of its stack)
271 is printed.
272 ..
273 .SH
274 Parameters
275 .PP
276 \*(Dc has three parameters that control its operation:
277 the precision, the input radix, and the output radix.
278 The precision specifies the number
279 of fraction digits to keep in the result of most arithmetic operations.
280 The input radix controls the interpretation of numbers typed in;
281 all numbers typed in use this radix.
282 The output radix is used for printing numbers.
283 .PP
284 The input and output radices are separate parameters;
285 you can make them unequal,
286 which can be useful or confusing.
287 The input radix must be between 2 and 16 inclusive.
288 The output radix must be at least 2.
289 The precision must be zero or greater.
290 The precision is always measured in decimal digits,
291 regardless of the current input or output radix.
292 .TP
293 .B i
294 Pops the value off the top of the stack
295 and uses it to set the input radix.
296 .TP
297 .B o
298 Pops the value off the top of the stack
299 and uses it to set the output radix.
300 .TP
301 .B k
302 Pops the value off the top of the stack
303 and uses it to set the precision.
304 .TP
305 .B I
306 Pushes the current input radix on the stack.
307 .TP
308 .B O
309 Pushes the current output radix on the stack.
310 .TP
311 .B K
312 Pushes the current precision on the stack.
313 .SH
314 Strings
315 .PP
316 \*(Dc can operate on strings as well as on numbers.
317 The only things you can do with strings are
318 print them and execute them as macros
319 (which means that the contents of the string are processed as
320 \*(dc commands).
321 All registers and the stack can hold strings,
322 and \*(dc always knows whether any given object is a string or a number.
323 Some commands such as arithmetic operations demand numbers
324 as arguments and print errors if given strings.
325 Other commands can accept either a number or a string;
326 for example, the
327 .B p
328 command can accept either and prints the object
329 according to its type.
330 .TP
331 .BI [ characters ]
332 Makes a string containing
333 .I characters
334 (contained between balanced
335 .B [
336 and
337 .B ]
338 characters),
339 and pushes it on the stack.
340 For example,
341 .B [foo]P
342 prints the characters
343 .B foo
344 (with no newline).
345 .TP
346 .B a
347 The top-of-stack is popped.
348 If it was a number, then the low-order byte of this number
349 is converted into a string and pushed onto the stack.
350 Otherwise the top-of-stack was a string,
351 and the first character of that string is pushed back.
352 .TP
353 .B x
354 Pops a value off the stack and executes it as a macro.
355 Normally it should be a string;
356 if it is a number,
357 it is simply pushed back onto the stack.
358 For example,
359 .B [1p]x
360 executes the macro
361 .B 1p
362 which pushes
363 .B 1
364 on the stack and prints
365 .B 1
366 on a separate line.
367 .PP
368 Macros are most often stored in registers;
369 .B [1p]sa
370 stores a macro to print
371 .B 1
372 into register
373 .BR a ,
374 and
375 .B lax
376 invokes this macro.
377 .TP
378 .BI > r
379 Pops two values off the stack and compares them
380 assuming they are numbers,
381 executing the contents of register
382 .I r
383 as a macro if the original top-of-stack
384 is greater.
385 Thus,
386 .B 1 2>a
387 will invoke register
388 .BR a 's
389 contents and
390 .B 2 1>a
391 will not.
392 .TP
393 .BI !> r
394 Similar but invokes the macro if the original top-of-stack is
395 not greater than (less than or equal to) what was the second-to-top.
396 .TP
397 .BI < r
398 Similar but invokes the macro if the original top-of-stack is less.
399 .TP
400 .BI !< r
401 Similar but invokes the macro if the original top-of-stack is
402 not less than (greater than or equal to) what was the second-to-top.
403 .TP
404 .BI = r
405 Similar but invokes the macro if the two numbers popped are equal.
406 .TP
407 .BI != r
408 Similar but invokes the macro if the two numbers popped are not equal.
409 .ig
410 This can also be validly used to compare two strings for equality.
411 ..
412 .TP
413 .B ?
414 Reads a line from the terminal and executes it.
415 This command allows a macro to request input from the user.
416 .TP
417 .B q
418 exits from a macro and also from the macro which invoked it.
419 If called from the top level,
420 or from a macro which was called directly from the top level,
421 the
422 .B q
423 command will cause \*(dc to exit.
424 .TP
425 .B Q
426 Pops a value off the stack and uses it as a count
427 of levels of macro execution to be exited.
428 Thus,
429 .B 3Q
430 exits three levels.
431 The
432 .B Q
433 command will never cause \*(dc to exit.
434 .SH
435 Status Inquiry
436 .TP
437 .B Z
438 Pops a value off the stack,
439 calculates the number of digits it has
440 (or number of characters, if it is a string)
441 and pushes that number.
442 .TP
443 .B X
444 Pops a value off the stack,
445 calculates the number of fraction digits it has,
446 and pushes that number.
447 For a string,
448 the value pushed is
449 .\" -1.
450 0.
451 .TP
452 .B z
453 Pushes the current stack depth:
454 the number of objects on the stack before the execution of the
455 .B z
456 command.
457 .SH
458 Miscellaneous
459 .TP
460 .B !
461 Will run the rest of the line as a system command.
462 Note that parsing of the !<, !=, and !> commands take precedence,
463 so if you want to run a command starting with <, =, or > you will
464 need to add a space after the !.
465 .TP
466 .B #
467 Will interpret the rest of the line as a comment.
468 .TP
469 .BI : r
470 Will pop the top two values off of the stack.
471 The old second-to-top value will be stored in the array
472 .IR r ,
473 indexed by the old top-of-stack value.
474 .TP
475 .BI ; r
476 Pops the top-of-stack and uses it as an index into
477 the array
478 .IR r .
479 The selected value is then pushed onto the stack.
480 .P
481 Note that each stacked instance of a register has its own
482 array associated with it.
483 Thus \fB1 0:a 0Sa 2 0:a La 0;ap\fP will print 1,
484 because the 2 was stored in an instance of 0:a that
485 was later popped.
486 .SH
487 BUGS
488 .PP
489 Email bug reports to
490 .BR bug-dc@gnu.org .