1 .\" $OpenBSD: dc.1,v 1.27 2012/08/19 12:07:21 jmc Exp $
3 .\" Copyright (C) Caldera International Inc. 2001-2002.
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code and documentation must retain the above
10 .\" copyright notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed or owned by Caldera
17 .\" International, Inc.
18 .\" 4. Neither the name of Caldera International, Inc. nor the names of other
19 .\" contributors may be used to endorse or promote products derived from
20 .\" this software without specific prior written permission.
22 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 .\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 .\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
27 .\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 .\" POSSIBILITY OF SUCH DAMAGE.
35 .\" @(#)dc.1 8.1 (Berkeley) 6/6/93
46 .Op Fl e Ar expression
51 is an arbitrary precision arithmetic package.
52 The overall structure of
55 a stacking (reverse Polish) calculator i.e.\&
56 numbers are stored on a stack.
57 Adding a number pushes it onto the stack.
58 Arithmetic operations pop arguments off the stack
62 utility, which is a preprocessor for
64 providing infix notation and a C-like syntax
65 which implements functions and reasonable control
66 structures for programs.
67 The options are as follows:
69 .It Fl e Ar expr , Fl Fl expression Ar expr
74 options are specified, they will be processed in the order given.
75 .It Fl f Ar filename , Fl Fl file Ar filename
76 Process the content of the given file before further calculations are done.
79 options are specified, they will be processed in the order given.
81 Print short usage info.
82 .It Fl V , Fl Fl version
85 Enable extended register mode.
88 to allow more than 256 registers.
91 for a more detailed description.
98 are specified on the command line,
100 reads from the standard input.
111 operates on decimal integers,
112 but one may specify an input base, output base,
113 and a number of fractional digits (scale) to be maintained.
114 Whitespace is ignored, except where it signals the end of a number,
115 end of a line or when a register name is expected.
116 The following constructions are recognized:
117 .Bl -tag -width "number"
119 The value of the number is pushed on the stack.
120 A number is an unbroken string of the digits 0\-9 and letters A\-F.
121 It may be preceded by an underscore
123 to input a negative number.
124 A number may contain a single decimal point.
125 A number may also contain the characters A\-F, with the values 10\-15.
126 .It Cm "+ - / * % ~ ^"
128 top two values on the stack are added
135 divided and remaindered (~),
136 or exponentiated (^).
137 The two entries are popped off the stack;
138 the result is pushed on the stack in their place.
139 Any fractional part of an exponent is ignored.
141 For addition, subtraction, and remainder, the scale of the result is the
142 maximum of scales of the operands.
143 For division the scale of the result is defined
144 by the scale set by the
147 For multiplication, the scale is defined by the expression
148 .Sy min(a+b,max(a,b,scale)) ,
153 are the scales of the operands, and
155 is the scale defined by the
158 For exponentiation with a non-negative exponent, the scale of the result is
159 .Sy min(a*b,max(scale,a)) ,
162 is the scale of the base, and
167 If the exponent is negative, the scale of the result is the scale
172 In the case of the division and modulus operator (~),
173 the resultant quotient is pushed first followed by the remainder.
174 This is a shorthand for the sequence:
175 .Bd -literal -offset indent -compact
178 The division and modulus operator is a non-portable extension.
180 Pop the top value from the stack.
181 If that value is a number, compute the integer part of the number modulo 256.
182 If the result is zero, push an empty string.
183 Otherwise push a one character string by interpreting the computed value
188 If the top value is a string, push a string containing the first character
189 of the original string.
190 If the original string is empty, an empty string is pushed back.
193 operator is a non-portable extension.
195 All values on the stack are popped.
197 The top value on the stack is duplicated.
201 except that the output is written to the standard error stream.
203 All values on the stack are printed, separated by newlines.
205 The top two numbers are popped from the stack and compared.
206 A one is pushed if the top of the stack is equal to the second number
208 A zero is pushed otherwise.
209 This is a non-portable extension.
211 Pushes the input base on the top of the stack.
213 The top value on the stack is popped and used as the
214 base for further input.
215 The initial input base is 10.
217 Pop the top value from the stack.
218 The recursion level is popped by that value and, following that,
219 the input is skipped until the first occurrence of the
224 operator is a non-portable extension, used by the
228 The current scale factor is pushed onto the stack.
230 The top of the stack is popped, and that value is used as
231 a non-negative scale factor:
232 the appropriate number of places
233 are printed on output,
234 and maintained during multiplication, division, and exponentiation.
235 The interaction of scale factor,
236 input base, and output base will be reasonable if all are changed
241 is treated as a stack and its top value is popped onto the main stack.
246 is pushed on the stack.
250 Initially, all registers contain the value zero.
257 operator is a non-portable extensions, used by the
261 The top of the stack is replaced by one if the top of the stack
263 If the top of the stack is unequal to zero, it is replaced by zero.
264 This is a non-portable extension.
266 The top value on the stack is popped and printed without a newline.
267 This is a non-portable extension.
269 Pushes the output base on the top of the stack.
271 The top value on the stack is popped and used as the
272 base for further output.
273 The initial output base is 10.
275 The top of the stack is popped.
276 If the top of the stack is a string, it is printed without a trailing newline.
277 If the top of the stack is a number, it is interpreted as a
278 base 256 number, and each digit of this base 256 number is printed as
281 character, without a trailing newline.
283 The top value on the stack is printed with a trailing newline.
284 The top value remains unchanged.
286 The top value on the stack is popped and the string execution level is popped
290 If executing a string, the recursion level is
293 The top of the stack is removed (popped).
294 This is a non-portable extension.
296 The top two values on the stack are reversed (swapped).
297 This is a non-portable extension.
301 is treated as a stack.
302 The top value of the main stack is popped and pushed on it.
305 top of the stack is popped and stored into
309 Replaces the top element on the stack by its square root.
310 The scale of the result is the maximum of the scale of the argument
311 and the current value of scale.
313 Replaces the number on the top of the stack with its scale factor.
314 If the top of the stack is a string, replace it with the integer 0.
316 Treats the top element of the stack as a character string
317 and executes it as a string of
321 Replaces the number on the top of the stack with its length.
322 The length of a string is its number of characters.
323 The length of a number is its number of digits, not counting the minus sign
326 The stack level is pushed onto the stack.
327 .It Cm \&[ Ns ... Ns Cm \&]
330 string onto the top of the stack.
331 If the string includes brackets, these must be properly balanced.
332 The backslash character
334 may be used as an escape character, making it
335 possible to include unbalanced brackets in strings.
336 To include a backslash in a string, use a double backslash.
345 The top two elements of the stack are popped and compared.
348 is executed if they obey the stated
351 .Cm < Ns Va x Ns e Ns Va y
352 .Cm > Ns Va x Ns e Ns Va y
353 .Cm = Ns Va x Ns e Ns Va y
354 .Cm !< Ns Va x Ns e Ns Va y
355 .Cm !> Ns Va x Ns e Ns Va y
356 .Cm != Ns Va x Ns e Ns Va y
358 These operations are variants of the comparison operations above.
359 The first register name is followed by the letter
361 and another register name.
364 will be executed if the relation is true, and register
366 will be executed if the relation is false.
367 This is a non-portable extension.
369 The top two numbers are popped from the stack and compared.
370 A one is pushed if the top of the stack is less than the second number
372 A zero is pushed otherwise.
373 This is a non-portable extension.
375 The top two numbers are popped from the stack and compared.
376 A one is pushed if the top of stack is less than or equal to the
377 second number on the stack.
378 A zero is pushed otherwise.
379 This is a non-portable extension.
381 Interprets the rest of the line as a
385 A line of input is taken from the input source (usually the terminal)
388 Pop two values from the stack.
389 The second value on the stack is stored into the array
391 indexed by the top of stack.
393 Pop a value from the stack.
394 The value is used as an index into register
396 The value in this register is pushed onto the stack.
398 Array elements initially have the value zero.
399 Each level of a stacked register has its own array associated with
402 .Bd -literal -offset indent
403 [first] 0:a [dummy] Sa [second] 0:a 0;a p La 0;a p
407 .Bd -literal -offset indent
414 is written in an array that is later popped, to reveal the array that
418 Skip the rest of the line.
419 This is a non-portable extension.
422 Registers have a single character name
426 may be any character, including space, tab or any other special character.
427 If extended register mode is enabled using the
429 option and the register identifier
431 has the value 255, the next two characters are interpreted as a
432 two-byte register index.
433 The set of standard single character registers and the set of extended
434 registers do not overlap.
435 Extended register mode is a non-portable extension.
437 An example which prints the first ten values of
439 .Bd -literal -offset indent
445 Independent of the current input base, the command
446 .Bd -literal -offset indent
450 will reset the input base to decimal 10.
453 .It %c (0%o) is unimplemented
454 an undefined operation was called.
456 for not enough elements on the stack to do what was asked.
457 .It stack register '%c' (0%o) is empty
460 operation from a stack register that is empty.
461 .It Runtime warning: non-zero scale in exponent
462 for a fractional part of an exponent that is being ignored.
464 for trying to divide by zero.
465 .It remainder by zero
466 for trying to take a remainder by zero.
467 .It square root of negative number
468 for trying to take the square root of a negative number.
470 for an array index that is larger than 2048.
472 for a negative array index.
473 .It "input base must be a number between 2 and 16"
474 for trying to set an illegal input base.
475 .It output base must be a number greater than 1
476 for trying to set an illegal output base.
477 .It scale must be a nonnegative number
478 for trying to set a negative or zero scale.
480 for trying to set a scale that is too large.
481 A scale must be representable as a 32-bit unsigned number.
482 .It Q command argument exceeded string execution depth
483 for trying to pop the recursion level more than the current
485 .It Q command requires a number >= 1
486 for trying to pop an illegal number of recursion levels.
487 .It recursion too deep
488 for too many levels of nested execution.
490 The recursion level is increased by one if the
494 operation or one of the compare operations resulting in the execution
495 of register is executed.
496 As an exception, the recursion level is not increased if the operation
497 is executed as the last command of a string.
498 For example, the commands
499 .Bd -literal -offset indent
504 will execute an endless loop, while the commands
505 .Bd -literal -offset indent
510 will terminate because of a too deep recursion level.
511 .It J command argument exceeded string execution depth
512 for trying to pop the recursion level more than the current
515 for a failed scan for an occurrence of the
525 "DC \- An Interactive Desk Calculator"
526 .Pa /usr/share/doc/usd/05.dc/ .
528 The arithmetic operations of the
530 utility are expected to conform to the definition listed in the
538 command first appeared in
540 A complete rewrite of the
544 big number routines first appeared in
548 The original version of the
550 command was written by
554 The current version of the
556 utility was written by