[BACK]Return to dc.1 CVS log [TXT][DIR] Up to [local] / src / usr.bin / dc

Annotation of src/usr.bin/dc/dc.1, Revision 1.13

1.13    ! jmc         1: .\"    $OpenBSD: dc.1,v 1.12 2003/10/22 12:16:25 otto 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.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.12      otto      272: .It Xo
                    273: .Cm < Ns Va x Ns e Ns Va y
                    274: .Cm > Ns Va x Ns e Ns Va y
                    275: .Cm = Ns Va x Ns e Ns Va y
                    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: .Xc
                    280: These operations are variants of the comparison operations above.
                    281: The first register name is followed by the letter
                    282: .Sq e
                    283: and another register name.
                    284: Register
                    285: .Ar x
                    286: will be executed if the relation is true, and register
                    287: .Ar y
                    288: will be executed if the relation is false.
                    289: This is a non-portable extension.
1.2       jmc       290: .It Ic \&!
1.1       otto      291: Interprets the rest of the line as a
                    292: .Ux
                    293: command.
1.2       jmc       294: .It Ic \&?
1.1       otto      295: A line of input is taken from the input source (usually the terminal)
                    296: and executed.
                    297: .It Ic : Ns Ar r
1.2       jmc       298: Pop two values from the stack.
                    299: The second value on the stack is stored into the array
1.1       otto      300: .Ar r
                    301: indexed by the top of stack.
                    302: .It Ic ; Ns Ar r
1.2       jmc       303: Pop a value from the stack.
                    304: The value is used as an index into register
1.1       otto      305: .Ar r .
                    306: The value in this register is pushed onto the stack.
                    307: .Pp
1.2       jmc       308: Array elements initially have the value zero.
1.1       otto      309: Each level of a stacked register has its own array associated with
                    310: it.
                    311: The command sequence
1.2       jmc       312: .Bd -literal -offset indent
1.1       otto      313: [first] 0:a [dummy] Sa [second] 0:a 0;a p La 0;a p
                    314: .Ed
                    315: .Pp
                    316: will print
1.2       jmc       317: .Bd -literal -offset indent
1.1       otto      318: second
                    319: first
                    320: .Ed
                    321: .Pp
                    322: since the string
                    323: .Ql second
                    324: is written in an array that is later popped, to reveal the array that
                    325: stored
                    326: .Ql first .
                    327: .El
                    328: .Sh EXAMPLES
                    329: An example which prints the first ten values of
1.2       jmc       330: .Ic n! :
                    331: .Bd -literal -offset indent
1.1       otto      332: [la1+dsa*pla10>y]sy
                    333: 0sa1
                    334: lyx
                    335: .Ed
                    336: .Pp
                    337: Independent of the current input base, the command
1.2       jmc       338: .Bd -literal -offset indent
1.1       otto      339: Ai
                    340: .Ed
                    341: .Pp
                    342: will reset the input base to decimal 10.
                    343: .Sh DIAGNOSTICS
1.2       jmc       344: .Bl -diag
                    345: .It %c (0%o) is unimplemented
1.1       otto      346: an undefined operation was called.
1.2       jmc       347: .It stack empty
1.1       otto      348: for not enough elements on the stack to do what was asked.
1.2       jmc       349: .It stack register '%c' (0%o) is empty
                    350: for an
1.1       otto      351: .Ar L
                    352: operation from a stack register that is empty.
1.2       jmc       353: .It Runtime warning: non-zero scale in exponent
1.1       otto      354: for a fractional part of an exponent that is being ignored.
1.2       jmc       355: .It divide by zero
1.1       otto      356: for trying to divide by zero.
1.2       jmc       357: .It remainder by zero
1.1       otto      358: for trying to take a remainder by zero.
1.2       jmc       359: .It square root of negative number
1.1       otto      360: for trying to take the square root of a negative number.
1.2       jmc       361: .It index too big
1.1       otto      362: for an array index that is larger than 2048.
1.2       jmc       363: .It negative index
1.1       otto      364: for a negative array index.
1.13    ! jmc       365: .It "input base must be a number between 2 and 16"
1.1       otto      366: for trying to set an illegal input base.
1.2       jmc       367: .It output base must be a number greater than 1
1.1       otto      368: for trying to set an illegal input base.
1.2       jmc       369: .It scale must be a nonnegative number
1.1       otto      370: for trying to set a negative or zero scale.
1.2       jmc       371: .It scale too large
1.1       otto      372: for trying to set a scale that is too large.
1.2       jmc       373: A scale must be representable as a 32-bit unsigned number.
                    374: .It Q command argument exceeded string execution depth
1.1       otto      375: for trying to pop the recursion level more than the current
                    376: recursion level.
1.2       jmc       377: .It Q command requires a number >= 1
1.1       otto      378: for trying to pop an illegal number of recursion levels.
1.2       jmc       379: .It recursion too deep
1.1       otto      380: for too many levels of nested execution.
                    381: .Pp
                    382: The recursion level is increased by one if the
                    383: .Ar x
                    384: or
1.2       jmc       385: .Ar ?\&
1.1       otto      386: operation or one of the compare operations resulting in the execution
                    387: of register is executed.
                    388: As an exception, the recursion level is not increased if the operation
                    389: is executed as the last command of a string.
1.2       jmc       390: For example, the commands
                    391: .Bd -literal -offset indent
1.1       otto      392: [lax]sa
                    393: 1 lax
                    394: .Ed
                    395: .Pp
                    396: will execute an endless loop, while the commands
1.2       jmc       397: .Bd -literal -offset indent
1.1       otto      398: [laxp]sa
                    399: 1 lax
                    400: .Ed
                    401: .Pp
                    402: will terminate because of a too deep recursion level.
1.8       otto      403: .It J command argument exceeded string execution depth
                    404: for trying to pop the recursion level more than the current
                    405: recursion level.
                    406: .It mark not found
1.9       jmc       407: for a failed scan for an occurrence of the
1.8       otto      408: .Ic M
                    409: operator.
1.1       otto      410: .El
                    411: .Sh SEE ALSO
1.2       jmc       412: .Xr bc 1
1.1       otto      413: .Pp
                    414: USD:05
                    415: .Em "DC \- An Interactive Desk Calculator"
                    416: .Sh STANDARDS
                    417: The arithmetic operations of the
                    418: .Nm
                    419: utility are expected to conform to the definition listed in the
                    420: .Xr bc 1
                    421: section of the
                    422: .St -p1003.2
                    423: specification.
                    424: .Sh HISTORY
                    425: The
                    426: .Nm
                    427: command first appeared in
                    428: .At v6 .
                    429: A complete rewrite of the
                    430: .Nm
                    431: command using the
                    432: .Xr bn 3
                    433: big number routines first appeared in
                    434: .Ox 3.5 .
                    435: .Sh AUTHORS
                    436: The original version of the
                    437: .Nm
                    438: command was written by
                    439: .An Robert Morris
                    440: and
                    441: .An Lorinda Cherry .
                    442: The current version of the
                    443: .Nm
                    444: utility was written by
                    445: .An Otto Moerbeek .