Annotation of src/usr.bin/dc/dc.1, Revision 1.14
1.14 ! otto 1: .\" $OpenBSD: dc.1,v 1.13 2003/10/23 09:01:42 jmc Exp $
1.1 otto 2: .\"
3: .\" Copyright (C) Caldera International Inc. 2001-2002.
4: .\" All rights reserved.
5: .\"
6: .\" Redistribution and use in source and binary forms, with or without
7: .\" modification, are permitted provided that the following conditions
8: .\" are met:
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.
21: .\"
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.
34: .\"
35: .\" @(#)dc.1 8.1 (Berkeley) 6/6/93
36: .\"
37: .Dd June 6, 1993
38: .Dt DC 1
39: .Sh NAME
40: .Nm dc
41: .Nd desk calculator
42: .Sh SYNOPSIS
43: .Nm
1.2 jmc 44: .Op Ar file
1.1 otto 45: .Sh DESCRIPTION
46: .Nm
47: is an arbitrary precision arithmetic package.
48: The overall structure of
49: .Nm
50: is
1.2 jmc 51: a stacking (reverse Polish) calculator i.e.\&
52: numbers are stored on a stack.
53: Adding a number pushes it onto the stack.
54: Arithmetic operations pop arguments off the stack
55: and push the results.
56: See also the
57: .Xr bc 1
58: utility, which is a preprocessor for
59: .Nm
60: providing infix notation and a C-like syntax
61: which implements functions and reasonable control
62: structures for programs.
63: .Pp
64: Ordinarily,
65: .Nm
66: operates on decimal integers,
67: but one may specify an input base, output base,
68: and a number of fractional digits (scale) to be maintained.
1.1 otto 69: If an argument is given,
70: input is taken from that file until its end,
71: then from the standard input.
1.2 jmc 72: Whitespace is ignored, expect where it signals the end of a number,
1.1 otto 73: end of a line or when a register name is expected.
74: The following constructions are recognized:
1.2 jmc 75: .Bl -tag -width "number"
1.1 otto 76: .It Va number
77: The value of the number is pushed on the stack.
78: A number is an unbroken string of the digits 0\-9 and letters A\-F.
1.2 jmc 79: It may be preceded by an underscore
80: .Pq Sq _
81: to input a negative number.
82: A number may contain a single decimal point.
1.1 otto 83: A number may also contain the characters A\-F, with the values 10\-15.
1.7 otto 84: .It Cm "+ - / * % ~ ^"
1.1 otto 85: The
86: top two values on the stack are added
87: (+),
88: subtracted
89: (\-),
90: multiplied (*),
91: divided (/),
92: remaindered (%),
1.7 otto 93: divided and remaindered (~),
1.1 otto 94: or exponentiated (^).
95: The two entries are popped off the stack;
96: the result is pushed on the stack in their place.
97: Any fractional part of an exponent is ignored.
98: .Pp
99: For addition and subtraction, the scale of the result is the maximum
100: of scales of the operands.
101: For division the scale of the result is defined
102: by the scale set by the
1.8 otto 103: .Ic k
1.1 otto 104: operation.
1.2 jmc 105: For multiplication, the scale is defined by the expression
106: .Sy min(a+b,max(a,b,scale)) ,
1.1 otto 107: where
108: .Sy a
109: and
110: .Sy b
111: are the scales of the operands, and
112: .Sy scale
1.2 jmc 113: is the scale defined by the
1.8 otto 114: .Ic k
1.1 otto 115: operation.
1.11 jmc 116: For exponentiation with a non-negative exponent, the scale of the result is
1.2 jmc 117: .Sy min(a*b,max(scale,a)) ,
1.1 otto 118: where
119: .Sy a
120: is the scale of the base, and
121: .Sy b
122: is the
123: .Em value
124: of the exponent.
125: If the exponent is negative, the scale of the result is the scale
126: defined by the
1.8 otto 127: .Ic k
1.1 otto 128: operation.
1.7 otto 129: .Pp
130: In the case of the division and modulus operator (~),
131: the resultant quotient is pushed first followed by the remainder.
132: This is a shorthand for the sequence:
133: .Bd -literal -offset indent -compact
134: x y / x y %
135: .Ed
136: The division and modulus operator is a non-portable extension.
1.10 otto 137: .It Ic c
138: All values on the stack are popped.
139: .It Ic d
140: The top value on the stack is duplicated.
141: .It Ic f
142: All values on the stack are printed, separated by newlines.
143: .It Ic i
144: The top value on the stack is popped and used as the
145: base for further input.
146: The initial input base is 10.
147: .It Ic I
148: Pushes the input base on the top of the stack.
149: .It Ic J
150: Pop the top value of the stack.
151: The recursion level is popped by that value and, following that,
152: the input is skipped until the first occurrence of the
153: .Ic M
154: operator.
1.1 otto 155: The
1.10 otto 156: .Ic J
157: operator is a non-portable extensions, used by the
158: .Xr bc 1
159: command.
160: .It Ic K
161: The current scale factor is pushed onto the stack.
162: .It Ic k
163: The top of the stack is popped, and that value is used as
164: a non-negative scale factor:
165: the appropriate number of places
166: are printed on output,
167: and maintained during multiplication, division, and exponentiation.
168: The interaction of scale factor,
169: input base, and output base will be reasonable if all are changed
170: together.
171: .It Ic L Ns Ar x
172: Register
1.1 otto 173: .Ar x
1.10 otto 174: is treated as a stack and its top value is popped onto the main stack.
1.1 otto 175: .It Ic l Ns Ar x
176: The
177: value in register
178: .Ar x
179: is pushed on the stack.
180: The register
181: .Ar x
182: is not altered.
1.4 otto 183: Initially, all registers contain the value zero.
1.10 otto 184: .It Ic M
185: Mark used by the
186: .Ic J
187: operator.
188: The
189: .Ic M
190: operator is a non-portable extensions, used by the
191: .Xr bc 1
192: command.
193: .It Ic O
194: Pushes the output base on the top of the stack.
195: .It Ic o
196: The top value on the stack is popped and used as the
197: base for further output.
198: The initial output base is 10.
1.1 otto 199: .It Ic P
200: The top of the stack is popped.
1.2 jmc 201: If the top of the stack is a string, it is printed without a trailing newline.
1.1 otto 202: If the top of the stack is a number, it is interpreted as a
203: base 256 number, and each digit of this base 256 number is printed as
204: an
205: .Tn ASCII
206: character, without a trailing newline.
1.10 otto 207: .It Ic p
208: The top value on the stack is printed with a trailing newline.
209: The top value remains unchanged.
210: .It Ic Q
211: The top value on the stack is popped and the string execution level is popped
212: by that value.
1.1 otto 213: .It Ic q
214: Exits the program.
215: If executing a string, the recursion level is
216: popped by two.
1.14 ! otto 217: .It Ic r
! 218: The top two values on the stack are reversed (swapped).
! 219: This is a non-portable extension.
1.10 otto 220: .It Ic S Ns Ar x
221: Register
222: .Ar x
223: is treated as a stack.
224: The top value of the main stack is popped and pushed on it.
225: .It Ic s Ns Ar x
226: The
227: top of the stack is popped and stored into
228: a register named
229: .Ar x ,
230: where
231: .Ar x
232: may be any character, including space, tab or any other special character.
233: .It Ic v
234: Replaces the top element on the stack by its square root.
235: The scale of the result is the maximum of the scale of the argument
236: and the current value of scale.
237: .It Ic X
238: Replaces the number on the top of the stack with its scale factor.
239: If the top of the stack is a string, replace it with the integer 0.
1.1 otto 240: .It Ic x
241: Treats the top element of the stack as a character string
242: and executes it as a string of
243: .Nm
244: commands.
1.10 otto 245: .It Ic Z
246: Replaces the number on the top of the stack with its length.
247: The length of a string is its number of characters.
248: The length of a number is its number of digits, not counting the minus sign
249: and decimal point.
250: .It Ic z
251: The stack level is pushed onto the stack.
1.1 otto 252: .It Cm [ Ns ... Ns Cm ]
253: Puts the bracketed
254: .Tn ASCII
255: string onto the top of the stack.
1.5 otto 256: If the string includes brackets, these must be properly balanced.
1.6 jmc 257: The backslash character
258: .Pq Sq \e
259: may be used as an escape character, making it
1.5 otto 260: possible to include unbalanced brackets in strings.
1.6 jmc 261: To include a backslash in a string, use a double backslash.
1.1 otto 262: .It Xo
263: .Cm < Ns Va x
264: .Cm > Ns Va x
265: .Cm = Ns Va x
266: .Cm !< Ns Va x
267: .Cm !> Ns Va x
268: .Cm != Ns Va x
269: .Xc
270: The top two elements of the stack are popped and compared.
271: Register
272: .Ar x
273: is executed if they obey the stated
274: relation.
1.12 otto 275: .It Xo
276: .Cm < Ns Va x Ns e Ns Va y
277: .Cm > Ns Va x Ns e Ns Va y
278: .Cm = Ns Va x Ns e Ns Va y
279: .Cm !< Ns Va x Ns e Ns Va y
280: .Cm !> Ns Va x Ns e Ns Va y
281: .Cm != Ns Va x Ns e Ns Va y
282: .Xc
283: These operations are variants of the comparison operations above.
284: The first register name is followed by the letter
285: .Sq e
286: and another register name.
287: Register
288: .Ar x
289: will be executed if the relation is true, and register
290: .Ar y
291: will be executed if the relation is false.
292: This is a non-portable extension.
1.2 jmc 293: .It Ic \&!
1.1 otto 294: Interprets the rest of the line as a
295: .Ux
296: command.
1.2 jmc 297: .It Ic \&?
1.1 otto 298: A line of input is taken from the input source (usually the terminal)
299: and executed.
300: .It Ic : Ns Ar r
1.2 jmc 301: Pop two values from the stack.
302: The second value on the stack is stored into the array
1.1 otto 303: .Ar r
304: indexed by the top of stack.
305: .It Ic ; Ns Ar r
1.2 jmc 306: Pop a value from the stack.
307: The value is used as an index into register
1.1 otto 308: .Ar r .
309: The value in this register is pushed onto the stack.
310: .Pp
1.2 jmc 311: Array elements initially have the value zero.
1.1 otto 312: Each level of a stacked register has its own array associated with
313: it.
314: The command sequence
1.2 jmc 315: .Bd -literal -offset indent
1.1 otto 316: [first] 0:a [dummy] Sa [second] 0:a 0;a p La 0;a p
317: .Ed
318: .Pp
319: will print
1.2 jmc 320: .Bd -literal -offset indent
1.1 otto 321: second
322: first
323: .Ed
324: .Pp
325: since the string
326: .Ql second
327: is written in an array that is later popped, to reveal the array that
328: stored
329: .Ql first .
330: .El
331: .Sh EXAMPLES
332: An example which prints the first ten values of
1.2 jmc 333: .Ic n! :
334: .Bd -literal -offset indent
1.1 otto 335: [la1+dsa*pla10>y]sy
336: 0sa1
337: lyx
338: .Ed
339: .Pp
340: Independent of the current input base, the command
1.2 jmc 341: .Bd -literal -offset indent
1.1 otto 342: Ai
343: .Ed
344: .Pp
345: will reset the input base to decimal 10.
346: .Sh DIAGNOSTICS
1.2 jmc 347: .Bl -diag
348: .It %c (0%o) is unimplemented
1.1 otto 349: an undefined operation was called.
1.2 jmc 350: .It stack empty
1.1 otto 351: for not enough elements on the stack to do what was asked.
1.2 jmc 352: .It stack register '%c' (0%o) is empty
353: for an
1.1 otto 354: .Ar L
355: operation from a stack register that is empty.
1.2 jmc 356: .It Runtime warning: non-zero scale in exponent
1.1 otto 357: for a fractional part of an exponent that is being ignored.
1.2 jmc 358: .It divide by zero
1.1 otto 359: for trying to divide by zero.
1.2 jmc 360: .It remainder by zero
1.1 otto 361: for trying to take a remainder by zero.
1.2 jmc 362: .It square root of negative number
1.1 otto 363: for trying to take the square root of a negative number.
1.2 jmc 364: .It index too big
1.1 otto 365: for an array index that is larger than 2048.
1.2 jmc 366: .It negative index
1.1 otto 367: for a negative array index.
1.13 jmc 368: .It "input base must be a number between 2 and 16"
1.1 otto 369: for trying to set an illegal input base.
1.2 jmc 370: .It output base must be a number greater than 1
1.1 otto 371: for trying to set an illegal input base.
1.2 jmc 372: .It scale must be a nonnegative number
1.1 otto 373: for trying to set a negative or zero scale.
1.2 jmc 374: .It scale too large
1.1 otto 375: for trying to set a scale that is too large.
1.2 jmc 376: A scale must be representable as a 32-bit unsigned number.
377: .It Q command argument exceeded string execution depth
1.1 otto 378: for trying to pop the recursion level more than the current
379: recursion level.
1.2 jmc 380: .It Q command requires a number >= 1
1.1 otto 381: for trying to pop an illegal number of recursion levels.
1.2 jmc 382: .It recursion too deep
1.1 otto 383: for too many levels of nested execution.
384: .Pp
385: The recursion level is increased by one if the
386: .Ar x
387: or
1.2 jmc 388: .Ar ?\&
1.1 otto 389: operation or one of the compare operations resulting in the execution
390: of register is executed.
391: As an exception, the recursion level is not increased if the operation
392: is executed as the last command of a string.
1.2 jmc 393: For example, the commands
394: .Bd -literal -offset indent
1.1 otto 395: [lax]sa
396: 1 lax
397: .Ed
398: .Pp
399: will execute an endless loop, while the commands
1.2 jmc 400: .Bd -literal -offset indent
1.1 otto 401: [laxp]sa
402: 1 lax
403: .Ed
404: .Pp
405: will terminate because of a too deep recursion level.
1.8 otto 406: .It J command argument exceeded string execution depth
407: for trying to pop the recursion level more than the current
408: recursion level.
409: .It mark not found
1.9 jmc 410: for a failed scan for an occurrence of the
1.8 otto 411: .Ic M
412: operator.
1.1 otto 413: .El
414: .Sh SEE ALSO
1.2 jmc 415: .Xr bc 1
1.1 otto 416: .Pp
417: USD:05
418: .Em "DC \- An Interactive Desk Calculator"
419: .Sh STANDARDS
420: The arithmetic operations of the
421: .Nm
422: utility are expected to conform to the definition listed in the
423: .Xr bc 1
424: section of the
425: .St -p1003.2
426: specification.
427: .Sh HISTORY
428: The
429: .Nm
430: command first appeared in
431: .At v6 .
432: A complete rewrite of the
433: .Nm
434: command using the
435: .Xr bn 3
436: big number routines first appeared in
437: .Ox 3.5 .
438: .Sh AUTHORS
439: The original version of the
440: .Nm
441: command was written by
442: .An Robert Morris
443: and
444: .An Lorinda Cherry .
445: The current version of the
446: .Nm
447: utility was written by
448: .An Otto Moerbeek .