[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.15

1.15    ! otto        1: .\"    $OpenBSD: dc.1,v 1.14 2003/11/04 08:10:06 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.15    ! otto      137: .It Ic a
        !           138: Pop the top value from the stack.
        !           139: If that value is a number, compute the integer part of the number modulo 256.
        !           140: If the result is zero, push an empty string.
        !           141: Otherwise push a one character string by interpreting the computed value
        !           142: as an
        !           143: .Tn ASCII
        !           144: character.
        !           145: .Pp
        !           146: If the top value is a string, push a string containing the first character
        !           147: of the original string.
        !           148: If the original string is empty, an empty string is pushed back.
        !           149: The
        !           150: .Ic a
        !           151: operator is a non-portable extension.
1.10      otto      152: .It Ic c
                    153: All values on the stack are popped.
                    154: .It Ic d
                    155: The top value on the stack is duplicated.
                    156: .It Ic f
                    157: All values on the stack are printed, separated by newlines.
                    158: .It Ic i
                    159: The top value on the stack is popped and used as the
                    160: base for further input.
                    161: The initial input base is 10.
                    162: .It Ic I
                    163: Pushes the input base on the top of the stack.
                    164: .It Ic J
1.15    ! otto      165: Pop the top value from the stack.
1.10      otto      166: The recursion level is popped by that value and, following that,
                    167: the input is skipped until the first occurrence of the
                    168: .Ic M
                    169: operator.
1.1       otto      170: The
1.10      otto      171: .Ic J
1.15    ! otto      172: operator is a non-portable extension, used by the
1.10      otto      173: .Xr bc 1
                    174: command.
                    175: .It Ic K
                    176: The current scale factor is pushed onto the stack.
                    177: .It Ic k
                    178: The top of the stack is popped, and that value is used as
                    179: a non-negative scale factor:
                    180: the appropriate number of places
                    181: are printed on output,
                    182: and maintained during multiplication, division, and exponentiation.
                    183: The interaction of scale factor,
                    184: input base, and output base will be reasonable if all are changed
                    185: together.
                    186: .It Ic L Ns Ar x
                    187: Register
1.1       otto      188: .Ar x
1.10      otto      189: is treated as a stack and its top value is popped onto the main stack.
1.1       otto      190: .It Ic l Ns Ar x
                    191: The
                    192: value in register
                    193: .Ar x
                    194: is pushed on the stack.
                    195: The register
                    196: .Ar x
                    197: is not altered.
1.4       otto      198: Initially, all registers contain the value zero.
1.10      otto      199: .It Ic M
                    200: Mark used by the
                    201: .Ic J
                    202: operator.
                    203: The
                    204: .Ic M
                    205: operator is a non-portable extensions, used by the
                    206: .Xr bc 1
                    207: command.
1.15    ! otto      208: .It Ic n
        !           209: The top value on the stack is popped and printed without a newline.
        !           210: This is a non-portable extension.
1.10      otto      211: .It Ic O
                    212: Pushes the output base on the top of the stack.
                    213: .It Ic o
                    214: The top value on the stack is popped and used as the
                    215: base for further output.
                    216: The initial output base is 10.
1.1       otto      217: .It Ic P
                    218: The top of the stack is popped.
1.2       jmc       219: If the top of the stack is a string, it is printed without a trailing newline.
1.1       otto      220: If the top of the stack is a number, it is interpreted as a
                    221: base 256 number, and each digit of this base 256 number is printed as
                    222: an
                    223: .Tn ASCII
                    224: character, without a trailing newline.
1.10      otto      225: .It Ic p
                    226: The top value on the stack is printed with a trailing newline.
                    227: The top value remains unchanged.
                    228: .It Ic Q
                    229: The top value on the stack is popped and the string execution level is popped
                    230: by that value.
1.1       otto      231: .It Ic q
                    232: Exits the program.
                    233: If executing a string, the recursion level is
                    234: popped by two.
1.14      otto      235: .It Ic r
                    236: The top two values on the stack are reversed (swapped).
                    237: This is a non-portable extension.
1.10      otto      238: .It Ic S Ns Ar x
                    239: Register
                    240: .Ar x
                    241: is treated as a stack.
                    242: The top value of the main stack is popped and pushed on it.
                    243: .It Ic s Ns Ar x
                    244: The
                    245: top of the stack is popped and stored into
                    246: a register named
                    247: .Ar x ,
                    248: where
                    249: .Ar x
                    250: may be any character, including space, tab or any other special character.
                    251: .It Ic v
                    252: Replaces the top element on the stack by its square root.
                    253: The scale of the result is the maximum of the scale of the argument
                    254: and the current value of scale.
                    255: .It Ic X
                    256: Replaces the number on the top of the stack with its scale factor.
                    257: If the top of the stack is a string, replace it with the integer 0.
1.1       otto      258: .It Ic x
                    259: Treats the top element of the stack as a character string
                    260: and executes it as a string of
                    261: .Nm
                    262: commands.
1.10      otto      263: .It Ic Z
                    264: Replaces the number on the top of the stack with its length.
                    265: The length of a string is its number of characters.
                    266: The length of a number is its number of digits, not counting the minus sign
                    267: and decimal point.
                    268: .It Ic z
                    269: The stack level is pushed onto the stack.
1.1       otto      270: .It Cm [ Ns ... Ns Cm ]
                    271: Puts the bracketed
                    272: .Tn ASCII
                    273: string onto the top of the stack.
1.5       otto      274: If the string includes brackets, these must be properly balanced.
1.6       jmc       275: The backslash character
                    276: .Pq Sq \e
                    277: may be used as an escape character, making it
1.5       otto      278: possible to include unbalanced brackets in strings.
1.6       jmc       279: To include a backslash in a string, use a double backslash.
1.1       otto      280: .It Xo
                    281: .Cm < Ns Va x
                    282: .Cm > Ns Va x
                    283: .Cm = Ns Va x
                    284: .Cm !< Ns Va x
                    285: .Cm !> Ns Va x
                    286: .Cm != Ns Va x
                    287: .Xc
                    288: The top two elements of the stack are popped and compared.
                    289: Register
                    290: .Ar x
                    291: is executed if they obey the stated
                    292: relation.
1.12      otto      293: .It Xo
                    294: .Cm < Ns Va x Ns e Ns Va y
                    295: .Cm > Ns Va x Ns e Ns Va y
                    296: .Cm = Ns Va x Ns e Ns Va y
                    297: .Cm !< Ns Va x Ns e Ns Va y
                    298: .Cm !> Ns Va x Ns e Ns Va y
                    299: .Cm != Ns Va x Ns e Ns Va y
                    300: .Xc
                    301: These operations are variants of the comparison operations above.
                    302: The first register name is followed by the letter
                    303: .Sq e
                    304: and another register name.
                    305: Register
                    306: .Ar x
                    307: will be executed if the relation is true, and register
                    308: .Ar y
                    309: will be executed if the relation is false.
                    310: This is a non-portable extension.
1.2       jmc       311: .It Ic \&!
1.1       otto      312: Interprets the rest of the line as a
                    313: .Ux
                    314: command.
1.2       jmc       315: .It Ic \&?
1.1       otto      316: A line of input is taken from the input source (usually the terminal)
                    317: and executed.
                    318: .It Ic : Ns Ar r
1.2       jmc       319: Pop two values from the stack.
                    320: The second value on the stack is stored into the array
1.1       otto      321: .Ar r
                    322: indexed by the top of stack.
                    323: .It Ic ; Ns Ar r
1.2       jmc       324: Pop a value from the stack.
                    325: The value is used as an index into register
1.1       otto      326: .Ar r .
                    327: The value in this register is pushed onto the stack.
                    328: .Pp
1.2       jmc       329: Array elements initially have the value zero.
1.1       otto      330: Each level of a stacked register has its own array associated with
                    331: it.
                    332: The command sequence
1.2       jmc       333: .Bd -literal -offset indent
1.1       otto      334: [first] 0:a [dummy] Sa [second] 0:a 0;a p La 0;a p
                    335: .Ed
                    336: .Pp
                    337: will print
1.2       jmc       338: .Bd -literal -offset indent
1.1       otto      339: second
                    340: first
                    341: .Ed
                    342: .Pp
                    343: since the string
                    344: .Ql second
                    345: is written in an array that is later popped, to reveal the array that
                    346: stored
                    347: .Ql first .
1.15    ! otto      348: .It Ic #
        !           349: Skip the rest of the line.
        !           350: This is a non-portable extension.
1.1       otto      351: .El
                    352: .Sh EXAMPLES
                    353: An example which prints the first ten values of
1.2       jmc       354: .Ic n! :
                    355: .Bd -literal -offset indent
1.1       otto      356: [la1+dsa*pla10>y]sy
                    357: 0sa1
                    358: lyx
                    359: .Ed
                    360: .Pp
                    361: Independent of the current input base, the command
1.2       jmc       362: .Bd -literal -offset indent
1.1       otto      363: Ai
                    364: .Ed
                    365: .Pp
                    366: will reset the input base to decimal 10.
                    367: .Sh DIAGNOSTICS
1.2       jmc       368: .Bl -diag
                    369: .It %c (0%o) is unimplemented
1.1       otto      370: an undefined operation was called.
1.2       jmc       371: .It stack empty
1.1       otto      372: for not enough elements on the stack to do what was asked.
1.2       jmc       373: .It stack register '%c' (0%o) is empty
                    374: for an
1.1       otto      375: .Ar L
                    376: operation from a stack register that is empty.
1.2       jmc       377: .It Runtime warning: non-zero scale in exponent
1.1       otto      378: for a fractional part of an exponent that is being ignored.
1.2       jmc       379: .It divide by zero
1.1       otto      380: for trying to divide by zero.
1.2       jmc       381: .It remainder by zero
1.1       otto      382: for trying to take a remainder by zero.
1.2       jmc       383: .It square root of negative number
1.1       otto      384: for trying to take the square root of a negative number.
1.2       jmc       385: .It index too big
1.1       otto      386: for an array index that is larger than 2048.
1.2       jmc       387: .It negative index
1.1       otto      388: for a negative array index.
1.13      jmc       389: .It "input base must be a number between 2 and 16"
1.1       otto      390: for trying to set an illegal input base.
1.2       jmc       391: .It output base must be a number greater than 1
1.1       otto      392: for trying to set an illegal input base.
1.2       jmc       393: .It scale must be a nonnegative number
1.1       otto      394: for trying to set a negative or zero scale.
1.2       jmc       395: .It scale too large
1.1       otto      396: for trying to set a scale that is too large.
1.2       jmc       397: A scale must be representable as a 32-bit unsigned number.
                    398: .It Q command argument exceeded string execution depth
1.1       otto      399: for trying to pop the recursion level more than the current
                    400: recursion level.
1.2       jmc       401: .It Q command requires a number >= 1
1.1       otto      402: for trying to pop an illegal number of recursion levels.
1.2       jmc       403: .It recursion too deep
1.1       otto      404: for too many levels of nested execution.
                    405: .Pp
                    406: The recursion level is increased by one if the
                    407: .Ar x
                    408: or
1.2       jmc       409: .Ar ?\&
1.1       otto      410: operation or one of the compare operations resulting in the execution
                    411: of register is executed.
                    412: As an exception, the recursion level is not increased if the operation
                    413: is executed as the last command of a string.
1.2       jmc       414: For example, the commands
                    415: .Bd -literal -offset indent
1.1       otto      416: [lax]sa
                    417: 1 lax
                    418: .Ed
                    419: .Pp
                    420: will execute an endless loop, while the commands
1.2       jmc       421: .Bd -literal -offset indent
1.1       otto      422: [laxp]sa
                    423: 1 lax
                    424: .Ed
                    425: .Pp
                    426: will terminate because of a too deep recursion level.
1.8       otto      427: .It J command argument exceeded string execution depth
                    428: for trying to pop the recursion level more than the current
                    429: recursion level.
                    430: .It mark not found
1.9       jmc       431: for a failed scan for an occurrence of the
1.8       otto      432: .Ic M
                    433: operator.
1.1       otto      434: .El
                    435: .Sh SEE ALSO
1.2       jmc       436: .Xr bc 1
1.1       otto      437: .Pp
                    438: USD:05
                    439: .Em "DC \- An Interactive Desk Calculator"
                    440: .Sh STANDARDS
                    441: The arithmetic operations of the
                    442: .Nm
                    443: utility are expected to conform to the definition listed in the
                    444: .Xr bc 1
                    445: section of the
                    446: .St -p1003.2
                    447: specification.
                    448: .Sh HISTORY
                    449: The
                    450: .Nm
                    451: command first appeared in
                    452: .At v6 .
                    453: A complete rewrite of the
                    454: .Nm
                    455: command using the
                    456: .Xr bn 3
                    457: big number routines first appeared in
                    458: .Ox 3.5 .
                    459: .Sh AUTHORS
                    460: The original version of the
                    461: .Nm
                    462: command was written by
                    463: .An Robert Morris
                    464: and
                    465: .An Lorinda Cherry .
                    466: The current version of the
                    467: .Nm
                    468: utility was written by
                    469: .An Otto Moerbeek .