1 .\" $OpenBSD: dc.1,v 1.19 2004/10/18 07:49:00 otto 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
50 is an arbitrary precision arithmetic package.
51 The overall structure of
54 a stacking (reverse Polish) calculator i.e.\&
55 numbers are stored on a stack.
56 Adding a number pushes it onto the stack.
57 Arithmetic operations pop arguments off the stack
61 utility, which is a preprocessor for
63 providing infix notation and a C-like syntax
64 which implements functions and reasonable control
65 structures for programs.
66 The options are as follows:
68 .It Fl e Ar expression
73 options are specified, they will be processed in the order given.
76 argument is given, execution will stop after processing the expressions
77 given on the command line,
78 otherwise processing will continue with the contents of
81 Enable extended register mode.
84 to allow more than 256 registers.
87 for a more detailed description.
92 operates on decimal integers,
93 but one may specify an input base, output base,
94 and a number of fractional digits (scale) to be maintained.
95 If an argument is given,
96 input is taken from that file until its end,
97 then from the standard input.
98 Whitespace is ignored, expect where it signals the end of a number,
99 end of a line or when a register name is expected.
100 The following constructions are recognized:
101 .Bl -tag -width "number"
103 The value of the number is pushed on the stack.
104 A number is an unbroken string of the digits 0\-9 and letters A\-F.
105 It may be preceded by an underscore
107 to input a negative number.
108 A number may contain a single decimal point.
109 A number may also contain the characters A\-F, with the values 10\-15.
110 .It Cm "+ - / * % ~ ^"
112 top two values on the stack are added
119 divided and remaindered (~),
120 or exponentiated (^).
121 The two entries are popped off the stack;
122 the result is pushed on the stack in their place.
123 Any fractional part of an exponent is ignored.
125 For addition and subtraction, the scale of the result is the maximum
126 of scales of the operands.
127 For division the scale of the result is defined
128 by the scale set by the
131 For multiplication, the scale is defined by the expression
132 .Sy min(a+b,max(a,b,scale)) ,
137 are the scales of the operands, and
139 is the scale defined by the
142 For exponentiation with a non-negative exponent, the scale of the result is
143 .Sy min(a*b,max(scale,a)) ,
146 is the scale of the base, and
151 If the exponent is negative, the scale of the result is the scale
156 In the case of the division and modulus operator (~),
157 the resultant quotient is pushed first followed by the remainder.
158 This is a shorthand for the sequence:
159 .Bd -literal -offset indent -compact
162 The division and modulus operator is a non-portable extension.
164 Pop the top value from the stack.
165 If that value is a number, compute the integer part of the number modulo 256.
166 If the result is zero, push an empty string.
167 Otherwise push a one character string by interpreting the computed value
172 If the top value is a string, push a string containing the first character
173 of the original string.
174 If the original string is empty, an empty string is pushed back.
177 operator is a non-portable extension.
179 All values on the stack are popped.
181 The top value on the stack is duplicated.
183 All values on the stack are printed, separated by newlines.
185 The top two numbers are popped from the stack and compared.
186 A one is pushed if the top of the stack is equal to the second number
188 A zero is pushed otherwise.
189 This is a non-portable extension.
191 The top value on the stack is popped and used as the
192 base for further input.
193 The initial input base is 10.
195 Pushes the input base on the top of the stack.
197 Pop the top value from the stack.
198 The recursion level is popped by that value and, following that,
199 the input is skipped until the first occurrence of the
204 operator is a non-portable extension, used by the
208 The current scale factor is pushed onto the stack.
210 The top of the stack is popped, and that value is used as
211 a non-negative scale factor:
212 the appropriate number of places
213 are printed on output,
214 and maintained during multiplication, division, and exponentiation.
215 The interaction of scale factor,
216 input base, and output base will be reasonable if all are changed
221 is treated as a stack and its top value is popped onto the main stack.
226 is pushed on the stack.
230 Initially, all registers contain the value zero.
237 operator is a non-portable extensions, used by the
241 The top of the stack is replaced by one if the top of the stack
243 If the top of the stack is unequal to zero, it is replaced by zero.
244 This is a non-portable extension.
246 The top value on the stack is popped and printed without a newline.
247 This is a non-portable extension.
249 Pushes the output base on the top of the stack.
251 The top value on the stack is popped and used as the
252 base for further output.
253 The initial output base is 10.
255 The top of the stack is popped.
256 If the top of the stack is a string, it is printed without a trailing newline.
257 If the top of the stack is a number, it is interpreted as a
258 base 256 number, and each digit of this base 256 number is printed as
261 character, without a trailing newline.
263 The top value on the stack is printed with a trailing newline.
264 The top value remains unchanged.
266 The top value on the stack is popped and the string execution level is popped
270 If executing a string, the recursion level is
273 The top of the stack is removed (popped).
274 This is a non-portable extension.
276 The top two values on the stack are reversed (swapped).
277 This is a non-portable extension.
281 is treated as a stack.
282 The top value of the main stack is popped and pushed on it.
285 top of the stack is popped and stored into
289 Replaces the top element on the stack by its square root.
290 The scale of the result is the maximum of the scale of the argument
291 and the current value of scale.
293 Replaces the number on the top of the stack with its scale factor.
294 If the top of the stack is a string, replace it with the integer 0.
296 Treats the top element of the stack as a character string
297 and executes it as a string of
301 Replaces the number on the top of the stack with its length.
302 The length of a string is its number of characters.
303 The length of a number is its number of digits, not counting the minus sign
306 The stack level is pushed onto the stack.
307 .It Cm \&[ Ns ... Ns Cm \&]
310 string onto the top of the stack.
311 If the string includes brackets, these must be properly balanced.
312 The backslash character
314 may be used as an escape character, making it
315 possible to include unbalanced brackets in strings.
316 To include a backslash in a string, use a double backslash.
325 The top two elements of the stack are popped and compared.
328 is executed if they obey the stated
331 .Cm < Ns Va x Ns e Ns Va y
332 .Cm > Ns Va x Ns e Ns Va y
333 .Cm = Ns Va x Ns e Ns Va y
334 .Cm !< Ns Va x Ns e Ns Va y
335 .Cm !> Ns Va x Ns e Ns Va y
336 .Cm != Ns Va x Ns e Ns Va y
338 These operations are variants of the comparison operations above.
339 The first register name is followed by the letter
341 and another register name.
344 will be executed if the relation is true, and register
346 will be executed if the relation is false.
347 This is a non-portable extension.
349 The top two numbers are popped from the stack and compared.
350 A one is pushed if the top of the stack is less than the second number
352 A zero is pushed otherwise.
353 This is a non-portable extension.
355 The top two numbers are popped from the stack and compared.
356 A one is pushed if the top of stack is less than or equal to the
357 second number on the stack.
358 A zero is pushed otherwise.
359 This is a non-portable extension.
361 Interprets the rest of the line as a
365 A line of input is taken from the input source (usually the terminal)
368 Pop two values from the stack.
369 The second value on the stack is stored into the array
371 indexed by the top of stack.
373 Pop a value from the stack.
374 The value is used as an index into register
376 The value in this register is pushed onto the stack.
378 Array elements initially have the value zero.
379 Each level of a stacked register has its own array associated with
382 .Bd -literal -offset indent
383 [first] 0:a [dummy] Sa [second] 0:a 0;a p La 0;a p
387 .Bd -literal -offset indent
394 is written in an array that is later popped, to reveal the array that
398 Skip the rest of the line.
399 This is a non-portable extension.
402 Registers have a single character name
406 may be any character, including space, tab or any other special character.
407 If extended register mode is enabled using the
409 option and the register identifier
411 has the value 255, the next two characters are interpreted as a
412 two-byte register index.
413 The set of standard single character registers and the set of extended
414 registers do not overlap.
415 Extended register mode is a non-portable extension.
417 An example which prints the first ten values of
419 .Bd -literal -offset indent
425 Independent of the current input base, the command
426 .Bd -literal -offset indent
430 will reset the input base to decimal 10.
433 .It %c (0%o) is unimplemented
434 an undefined operation was called.
436 for not enough elements on the stack to do what was asked.
437 .It stack register '%c' (0%o) is empty
440 operation from a stack register that is empty.
441 .It Runtime warning: non-zero scale in exponent
442 for a fractional part of an exponent that is being ignored.
444 for trying to divide by zero.
445 .It remainder by zero
446 for trying to take a remainder by zero.
447 .It square root of negative number
448 for trying to take the square root of a negative number.
450 for an array index that is larger than 2048.
452 for a negative array index.
453 .It "input base must be a number between 2 and 16"
454 for trying to set an illegal input base.
455 .It output base must be a number greater than 1
456 for trying to set an illegal output base.
457 .It scale must be a nonnegative number
458 for trying to set a negative or zero scale.
460 for trying to set a scale that is too large.
461 A scale must be representable as a 32-bit unsigned number.
462 .It Q command argument exceeded string execution depth
463 for trying to pop the recursion level more than the current
465 .It Q command requires a number >= 1
466 for trying to pop an illegal number of recursion levels.
467 .It recursion too deep
468 for too many levels of nested execution.
470 The recursion level is increased by one if the
474 operation or one of the compare operations resulting in the execution
475 of register is executed.
476 As an exception, the recursion level is not increased if the operation
477 is executed as the last command of a string.
478 For example, the commands
479 .Bd -literal -offset indent
484 will execute an endless loop, while the commands
485 .Bd -literal -offset indent
490 will terminate because of a too deep recursion level.
491 .It J command argument exceeded string execution depth
492 for trying to pop the recursion level more than the current
495 for a failed scan for an occurrence of the
505 .%T "DC \- An Interactive Desk Calculator"
508 The arithmetic operations of the
510 utility are expected to conform to the definition listed in the
518 command first appeared in
520 A complete rewrite of the
524 big number routines first appeared in
528 The original version of the
530 command was written by
534 The current version of the
536 utility was written by