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