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

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