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

Annotation of src/usr.bin/bc/bc.1, Revision 1.12

1.12    ! otto        1: .\"    $OpenBSD: bc.1,v 1.11 2003/11/11 09:16:53 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: .\"    @(#)bc.1        6.8 (Berkeley) 8/8/91
                     36: .\"
                     37: .Dd August 8, 1991
                     38: .Dt BC 1
                     39: .Sh NAME
1.4       jmc        40: .Nm bc
1.1       otto       41: .Nd arbitrary-precision arithmetic language and calculator
                     42: .Sh SYNOPSIS
1.4       jmc        43: .Nm bc
                     44: .Op Fl cl
1.5       otto       45: .Op Ar file ...
1.1       otto       46: .Sh DESCRIPTION
1.4       jmc        47: .Nm
1.1       otto       48: is an interactive processor for a language which resembles
                     49: C but provides unlimited precision arithmetic.
                     50: It takes input from any files given, then reads
                     51: the standard input.
                     52: .Pp
                     53: Options available:
1.4       jmc        54: .Bl -tag -width Ds
1.1       otto       55: .It Fl c
1.4       jmc        56: .Nm
1.1       otto       57: is actually a preprocessor for
1.4       jmc        58: .Xr dc 1 ,
1.1       otto       59: which it invokes automatically, unless the
                     60: .Fl c
1.4       jmc        61: .Pq compile only
1.1       otto       62: option is present.
1.4       jmc        63: In this case the generated
                     64: .Xr dc 1
                     65: instructions are sent to the standard output,
                     66: instead of being interpreted by a running
                     67: .Xr dc 1
                     68: process.
                     69: .It Fl l
                     70: Allow specification
                     71: of an arbitrary precision math library.
1.2       deraadt    72: .El
1.1       otto       73: .Pp
                     74: The syntax for
1.4       jmc        75: .Nm
                     76: programs is as follows:
                     77: .Sq L
                     78: means letter a-z;
                     79: .Sq E
                     80: means expression;
                     81: .Sq S
                     82: means statement.
1.1       otto       83: .Pp
                     84: Comments
                     85: .Bd -unfilled -offset indent -compact
1.4       jmc        86: are enclosed in /* and */
1.12    ! otto       87: are enclosed in # and the next newline
1.1       otto       88: .Ed
                     89: .Pp
1.12    ! otto       90: The newline is not part of the line comment,
        !            91: which in itself is a non-portable extension.
        !            92: .Pp
1.1       otto       93: Names
                     94: .Bd -unfilled -offset indent -compact
                     95: simple variables: L
                     96: array elements: L [ E ]
                     97: The words `ibase', `obase', and `scale'
1.10      otto       98: The word `last' or a single dot
1.1       otto       99: .Ed
                    100: .Pp
                    101: Other operands
                    102: .Bd -unfilled -offset indent -compact
1.4       jmc       103: arbitrarily long numbers with optional sign and decimal point
                    104: ( E )
1.1       otto      105: sqrt ( E )
                    106: length ( E )   number of significant decimal digits
                    107: scale ( E )    number of digits right of decimal point
                    108: L ( E , ... , E )
                    109: .Ed
                    110: .Pp
1.11      otto      111: The sequence
                    112: .Sq \e<newline><whitespace>
                    113: is ignored within numbers.
                    114: .Pp
1.1       otto      115: Operators
                    116: .Bd -unfilled -offset indent -compact
1.4       jmc       117: +  \-  *  /  %  ^ (`%' is remainder; `^' is power)
                    118: ++   \-\-         (prefix and postfix; apply to names)
                    119: ==  <=  >=  !=  <  >
                    120: =  +=  \-=  *=  /=  %=  ^=
1.1       otto      121: .Ed
                    122: .Pp
                    123: Statements
                    124: .Bd -unfilled -offset indent -compact
                    125: E
                    126: { S ; ... ; S }
                    127: if ( E ) S
1.9       otto      128: if ( E ) S else S
1.1       otto      129: while ( E ) S
                    130: for ( E ; E ; E ) S
                    131: null statement
                    132: break
1.6       otto      133: continue
1.1       otto      134: quit
1.6       otto      135: a string of characters, enclosed in double quotes
1.11      otto      136: print E ,..., E
1.1       otto      137: .Ed
                    138: .Pp
1.11      otto      139: A string may contain any character, except double quote.
1.9       otto      140: The if statement with an else branch is a non-portable extension.
1.8       jmc       141: All three E's in a for statement may be empty.
1.7       otto      142: This is a non-portable extension.
1.11      otto      143: The continue and print statements are also non-portable extensions.
                    144: .Pp
                    145: The print statement takes a list of comma-separated expressions.
                    146: Each expression in the list is evaluated and the computed
                    147: value is printed and assigned to the variable `last'.
                    148: No trailing newline is printed.
                    149: The expression may also be a string enclosed in double quotes.
                    150: Within these strings the following escape sequences may be used:
                    151: .Sq \ea
                    152: for bell (alert),
                    153: .Sq \eb
                    154: for backspace,
                    155: .Sq \ef
                    156: for formfeed,
                    157: .Sq \en
                    158: for newline,
                    159: .Sq \er
                    160: for carriage return,
                    161: .Sq \et
                    162: for tab,
                    163: .Sq \eq
                    164: for double quote and
                    165: .Sq \e\e
                    166: for backslash.
                    167: Any other character following a backslash will be ignored.
                    168: Strings will not be assigned to `last'.
1.7       otto      169: .Pp
1.1       otto      170: Function definitions
1.11      otto      171: .Pp
1.1       otto      172: .Bd -unfilled -offset indent -compact
                    173: define L ( L ,..., L ) {
                    174:        auto L, ... , L
                    175:        S; ... S
                    176:        return ( E )
                    177: }
                    178: .Ed
1.12    ! otto      179: .Pp
        !           180: As a non-portable extension, the opening brace of the define statement
        !           181: may appear on the next line.
        !           182: The return statement may also appear in the following forms:
        !           183: .Pp
        !           184: .Bd -unfilled -offset indent
        !           185: return
        !           186: return ()
        !           187: return E
        !           188: .Ed
        !           189: .Pp
        !           190: The first two are equivalent to the statement
        !           191: .Dq return 0 .
        !           192: The last form is a non-portable extension.
        !           193: Not specifying a return statement is equivalent to writing
        !           194: .Dq return (0) .
1.5       otto      195: .Pp
1.11      otto      196: Functions available in the math library, which is loaded by specifying the
                    197: .Fl l
                    198: flag on the command line
1.1       otto      199: .Pp
                    200: .Bl -tag -width j(n,x) -offset indent -compact
                    201: .It s(x)
                    202: sine
                    203: .It c(x)
                    204: cosine
                    205: .It e(x)
                    206: exponential
                    207: .It l(x)
                    208: log
                    209: .It a(x)
                    210: arctangent
                    211: .It j(n,x)
                    212: Bessel function
                    213: .El
                    214: .Pp
                    215: All function arguments are passed by value.
                    216: .Pp
                    217: The value of a statement that is an expression is printed
                    218: unless the main operator is an assignment.
1.10      otto      219: The value printed is assigned to the special variable `last'.
                    220: This is a non-portable extension.
                    221: A single dot may be used as a synonym for `last'.
1.1       otto      222: Either semicolons or newlines may separate statements.
                    223: Assignment to
                    224: .Ar scale
                    225: influences the number of digits to be retained on arithmetic
                    226: operations in the manner of
1.4       jmc       227: .Xr dc 1 .
1.1       otto      228: Assignments to
                    229: .Ar ibase
                    230: or
                    231: .Ar obase
                    232: set the input and output number radix respectively.
                    233: .Pp
                    234: The same letter may be used as an array, a function,
                    235: and a simple variable simultaneously.
                    236: All variables are global to the program.
                    237: `Auto' variables are pushed down during function calls.
                    238: When using arrays as function arguments
1.4       jmc       239: or defining them as automatic variables,
1.1       otto      240: empty square brackets must follow the array name.
                    241: .Pp
                    242: For example
                    243: .Bd -literal -offset indent
                    244: scale = 20
                    245: define e(x){
                    246:        auto a, b, c, i, s
                    247:        a = 1
                    248:        b = 1
                    249:        s = 1
                    250:        for(i=1; 1==1; i++){
                    251:                a = a*x
                    252:                b = b*i
                    253:                c = a/b
                    254:                if(c == 0) return(s)
                    255:                s = s+c
                    256:        }
                    257: }
                    258: .Ed
                    259: .Pp
                    260: defines a function to compute an approximate value of
                    261: the exponential function and
                    262: .Pp
                    263: .Dl for(i=1; i<=10; i++) e(i)
                    264: .Pp
                    265: prints approximate values of the exponential function of
                    266: the first ten integers.
1.11      otto      267: .Sh FILES
                    268: .Bl -tag -width /usr/share/misc/bc.library -compact
                    269: .It Pa /usr/share/misc/bc.library
                    270: math library, read when the
                    271: .Fl l
                    272: option is specified on the command line.
                    273: .El
1.1       otto      274: .Sh SEE ALSO
1.4       jmc       275: .Xr dc 1
1.1       otto      276: .Rs
1.11      otto      277: .%B USD
                    278: .%V 06
1.1       otto      279: .%A L. L. Cherry
                    280: .%A R. Morris
                    281: .%T "BC \- An arbitrary precision desk-calculator language"
                    282: .Re
1.3       otto      283: .Sh STANDARDS
                    284: The
                    285: .Nm
                    286: utility is expected to conform to the
                    287: .St -p1003.2
1.4       jmc       288: specification.
1.1       otto      289: .Sh HISTORY
                    290: The
1.4       jmc       291: .Nm
1.11      otto      292: first command appeared in
1.1       otto      293: .At v6 .
1.11      otto      294: A complete rewrite of the
                    295: .Nm
                    296: command first appeared in
                    297: .Ox 3.5 .
                    298: .Sh AUTHORS
                    299: The original version of the
                    300: .Nm
                    301: command was written by
                    302: .An Robert Morris
                    303: and
                    304: .An Lorinda Cherry .
                    305: The current version of the
                    306: .Nm
                    307: utility was written by
                    308: .An Otto Moerbeek .
1.1       otto      309: .Sh BUGS
1.4       jmc       310: No
                    311: .Sq && ,
                    312: .Sq \(or\(or ,
                    313: or
                    314: .Sq \&!
                    315: operators.
1.1       otto      316: .Pp
                    317: .Ql Quit
                    318: is interpreted when read, not when executed.
1.3       otto      319: .Pp
                    320: Some non-portable extensions, as found in the GNU version of the
                    321: .Nm
                    322: utility are not implemented (yet).