Annotation of src/usr.bin/dc/dc.1, Revision 1.10
1.10 ! otto 1: .\" $OpenBSD: dc.1,v 1.9 2003/10/18 21:00:17 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.
116: For exponentation 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.10 ! otto 217: .It Ic S Ns Ar x
! 218: Register
! 219: .Ar x
! 220: is treated as a stack.
! 221: The top value of the main stack is popped and pushed on it.
! 222: .It Ic s Ns Ar x
! 223: The
! 224: top of the stack is popped and stored into
! 225: a register named
! 226: .Ar x ,
! 227: where
! 228: .Ar x
! 229: may be any character, including space, tab or any other special character.
! 230: .It Ic v
! 231: Replaces the top element on the stack by its square root.
! 232: The scale of the result is the maximum of the scale of the argument
! 233: and the current value of scale.
! 234: .It Ic X
! 235: Replaces the number on the top of the stack with its scale factor.
! 236: If the top of the stack is a string, replace it with the integer 0.
1.1 otto 237: .It Ic x
238: Treats the top element of the stack as a character string
239: and executes it as a string of
240: .Nm
241: commands.
1.10 ! otto 242: .It Ic Z
! 243: Replaces the number on the top of the stack with its length.
! 244: The length of a string is its number of characters.
! 245: The length of a number is its number of digits, not counting the minus sign
! 246: and decimal point.
! 247: .It Ic z
! 248: The stack level is pushed onto the stack.
1.1 otto 249: .It Cm [ Ns ... Ns Cm ]
250: Puts the bracketed
251: .Tn ASCII
252: string onto the top of the stack.
1.5 otto 253: If the string includes brackets, these must be properly balanced.
1.6 jmc 254: The backslash character
255: .Pq Sq \e
256: may be used as an escape character, making it
1.5 otto 257: possible to include unbalanced brackets in strings.
1.6 jmc 258: To include a backslash in a string, use a double backslash.
1.1 otto 259: .It Xo
260: .Cm < Ns Va x
261: .Cm > Ns Va x
262: .Cm = Ns Va x
263: .Cm !< Ns Va x
264: .Cm !> Ns Va x
265: .Cm != Ns Va x
266: .Xc
267: The top two elements of the stack are popped and compared.
268: Register
269: .Ar x
270: is executed if they obey the stated
271: relation.
1.2 jmc 272: .It Ic \&!
1.1 otto 273: Interprets the rest of the line as a
274: .Ux
275: command.
1.2 jmc 276: .It Ic \&?
1.1 otto 277: A line of input is taken from the input source (usually the terminal)
278: and executed.
279: .It Ic : Ns Ar r
1.2 jmc 280: Pop two values from the stack.
281: The second value on the stack is stored into the array
1.1 otto 282: .Ar r
283: indexed by the top of stack.
284: .It Ic ; Ns Ar r
1.2 jmc 285: Pop a value from the stack.
286: The value is used as an index into register
1.1 otto 287: .Ar r .
288: The value in this register is pushed onto the stack.
289: .Pp
1.2 jmc 290: Array elements initially have the value zero.
1.1 otto 291: Each level of a stacked register has its own array associated with
292: it.
293: The command sequence
1.2 jmc 294: .Bd -literal -offset indent
1.1 otto 295: [first] 0:a [dummy] Sa [second] 0:a 0;a p La 0;a p
296: .Ed
297: .Pp
298: will print
1.2 jmc 299: .Bd -literal -offset indent
1.1 otto 300: second
301: first
302: .Ed
303: .Pp
304: since the string
305: .Ql second
306: is written in an array that is later popped, to reveal the array that
307: stored
308: .Ql first .
309: .El
310: .Sh EXAMPLES
311: An example which prints the first ten values of
1.2 jmc 312: .Ic n! :
313: .Bd -literal -offset indent
1.1 otto 314: [la1+dsa*pla10>y]sy
315: 0sa1
316: lyx
317: .Ed
318: .Pp
319: Independent of the current input base, the command
1.2 jmc 320: .Bd -literal -offset indent
1.1 otto 321: Ai
322: .Ed
323: .Pp
324: will reset the input base to decimal 10.
325: .Sh DIAGNOSTICS
1.2 jmc 326: .Bl -diag
327: .It %c (0%o) is unimplemented
1.1 otto 328: an undefined operation was called.
1.2 jmc 329: .It stack empty
1.1 otto 330: for not enough elements on the stack to do what was asked.
1.2 jmc 331: .It stack register '%c' (0%o) is empty
332: for an
1.1 otto 333: .Ar L
334: operation from a stack register that is empty.
1.2 jmc 335: .It Runtime warning: non-zero scale in exponent
1.1 otto 336: for a fractional part of an exponent that is being ignored.
1.2 jmc 337: .It divide by zero
1.1 otto 338: for trying to divide by zero.
1.2 jmc 339: .It remainder by zero
1.1 otto 340: for trying to take a remainder by zero.
1.2 jmc 341: .It square root of negative number
1.1 otto 342: for trying to take the square root of a negative number.
1.2 jmc 343: .It index too big
1.1 otto 344: for an array index that is larger than 2048.
1.2 jmc 345: .It negative index
1.1 otto 346: for a negative array index.
1.2 jmc 347: .It input base must be a number between 2 and 16
1.1 otto 348: for trying to set an illegal input base.
1.2 jmc 349: .It output base must be a number greater than 1
1.1 otto 350: for trying to set an illegal input base.
1.2 jmc 351: .It scale must be a nonnegative number
1.1 otto 352: for trying to set a negative or zero scale.
1.2 jmc 353: .It scale too large
1.1 otto 354: for trying to set a scale that is too large.
1.2 jmc 355: A scale must be representable as a 32-bit unsigned number.
356: .It Q command argument exceeded string execution depth
1.1 otto 357: for trying to pop the recursion level more than the current
358: recursion level.
1.2 jmc 359: .It Q command requires a number >= 1
1.1 otto 360: for trying to pop an illegal number of recursion levels.
1.2 jmc 361: .It recursion too deep
1.1 otto 362: for too many levels of nested execution.
363: .Pp
364: The recursion level is increased by one if the
365: .Ar x
366: or
1.2 jmc 367: .Ar ?\&
1.1 otto 368: operation or one of the compare operations resulting in the execution
369: of register is executed.
370: As an exception, the recursion level is not increased if the operation
371: is executed as the last command of a string.
1.2 jmc 372: For example, the commands
373: .Bd -literal -offset indent
1.1 otto 374: [lax]sa
375: 1 lax
376: .Ed
377: .Pp
378: will execute an endless loop, while the commands
1.2 jmc 379: .Bd -literal -offset indent
1.1 otto 380: [laxp]sa
381: 1 lax
382: .Ed
383: .Pp
384: will terminate because of a too deep recursion level.
1.8 otto 385: .It J command argument exceeded string execution depth
386: for trying to pop the recursion level more than the current
387: recursion level.
388: .It mark not found
1.9 jmc 389: for a failed scan for an occurrence of the
1.8 otto 390: .Ic M
391: operator.
1.1 otto 392: .El
393: .Sh SEE ALSO
1.2 jmc 394: .Xr bc 1
1.1 otto 395: .Pp
396: USD:05
397: .Em "DC \- An Interactive Desk Calculator"
398: .Sh STANDARDS
399: The arithmetic operations of the
400: .Nm
401: utility are expected to conform to the definition listed in the
402: .Xr bc 1
403: section of the
404: .St -p1003.2
405: specification.
406: .Sh HISTORY
407: The
408: .Nm
409: command first appeared in
410: .At v6 .
411: A complete rewrite of the
412: .Nm
413: command using the
414: .Xr bn 3
415: big number routines first appeared in
416: .Ox 3.5 .
417: .Sh AUTHORS
418: The original version of the
419: .Nm
420: command was written by
421: .An Robert Morris
422: and
423: .An Lorinda Cherry .
424: The current version of the
425: .Nm
426: utility was written by
427: .An Otto Moerbeek .