Annotation of src/usr.bin/bc/bc.1, Revision 1.11
1.11 ! otto 1: .\" $OpenBSD: bc.1,v 1.10 2003/11/09 19:27:27 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.1 otto 87: .Ed
88: .Pp
89: Names
90: .Bd -unfilled -offset indent -compact
91: simple variables: L
92: array elements: L [ E ]
93: The words `ibase', `obase', and `scale'
1.10 otto 94: The word `last' or a single dot
1.1 otto 95: .Ed
96: .Pp
97: Other operands
98: .Bd -unfilled -offset indent -compact
1.4 jmc 99: arbitrarily long numbers with optional sign and decimal point
100: ( E )
1.1 otto 101: sqrt ( E )
102: length ( E ) number of significant decimal digits
103: scale ( E ) number of digits right of decimal point
104: L ( E , ... , E )
105: .Ed
106: .Pp
1.11 ! otto 107: The sequence
! 108: .Sq \e<newline><whitespace>
! 109: is ignored within numbers.
! 110: .Pp
1.1 otto 111: Operators
112: .Bd -unfilled -offset indent -compact
1.4 jmc 113: + \- * / % ^ (`%' is remainder; `^' is power)
114: ++ \-\- (prefix and postfix; apply to names)
115: == <= >= != < >
116: = += \-= *= /= %= ^=
1.1 otto 117: .Ed
118: .Pp
119: Statements
120: .Bd -unfilled -offset indent -compact
121: E
122: { S ; ... ; S }
123: if ( E ) S
1.9 otto 124: if ( E ) S else S
1.1 otto 125: while ( E ) S
126: for ( E ; E ; E ) S
127: null statement
128: break
1.6 otto 129: continue
1.1 otto 130: quit
1.6 otto 131: a string of characters, enclosed in double quotes
1.11 ! otto 132: print E ,..., E
1.1 otto 133: .Ed
134: .Pp
1.11 ! otto 135: A string may contain any character, except double quote.
1.9 otto 136: The if statement with an else branch is a non-portable extension.
1.8 jmc 137: All three E's in a for statement may be empty.
1.7 otto 138: This is a non-portable extension.
1.11 ! otto 139: The continue and print statements are also non-portable extensions.
! 140: .Pp
! 141: The print statement takes a list of comma-separated expressions.
! 142: Each expression in the list is evaluated and the computed
! 143: value is printed and assigned to the variable `last'.
! 144: No trailing newline is printed.
! 145: The expression may also be a string enclosed in double quotes.
! 146: Within these strings the following escape sequences may be used:
! 147: .Sq \ea
! 148: for bell (alert),
! 149: .Sq \eb
! 150: for backspace,
! 151: .Sq \ef
! 152: for formfeed,
! 153: .Sq \en
! 154: for newline,
! 155: .Sq \er
! 156: for carriage return,
! 157: .Sq \et
! 158: for tab,
! 159: .Sq \eq
! 160: for double quote and
! 161: .Sq \e\e
! 162: for backslash.
! 163: Any other character following a backslash will be ignored.
! 164: Strings will not be assigned to `last'.
1.7 otto 165: .Pp
1.1 otto 166: Function definitions
1.11 ! otto 167: .Pp
1.1 otto 168: .Bd -unfilled -offset indent -compact
169: define L ( L ,..., L ) {
170: auto L, ... , L
171: S; ... S
172: return ( E )
173: }
174: .Ed
1.5 otto 175: .Pp
1.11 ! otto 176: Functions available in the math library, which is loaded by specifying the
! 177: .Fl l
! 178: flag on the command line
1.1 otto 179: .Pp
180: .Bl -tag -width j(n,x) -offset indent -compact
181: .It s(x)
182: sine
183: .It c(x)
184: cosine
185: .It e(x)
186: exponential
187: .It l(x)
188: log
189: .It a(x)
190: arctangent
191: .It j(n,x)
192: Bessel function
193: .El
194: .Pp
195: All function arguments are passed by value.
196: .Pp
197: The value of a statement that is an expression is printed
198: unless the main operator is an assignment.
1.10 otto 199: The value printed is assigned to the special variable `last'.
200: This is a non-portable extension.
201: A single dot may be used as a synonym for `last'.
1.1 otto 202: Either semicolons or newlines may separate statements.
203: Assignment to
204: .Ar scale
205: influences the number of digits to be retained on arithmetic
206: operations in the manner of
1.4 jmc 207: .Xr dc 1 .
1.1 otto 208: Assignments to
209: .Ar ibase
210: or
211: .Ar obase
212: set the input and output number radix respectively.
213: .Pp
214: The same letter may be used as an array, a function,
215: and a simple variable simultaneously.
216: All variables are global to the program.
217: `Auto' variables are pushed down during function calls.
218: When using arrays as function arguments
1.4 jmc 219: or defining them as automatic variables,
1.1 otto 220: empty square brackets must follow the array name.
221: .Pp
222: For example
223: .Bd -literal -offset indent
224: scale = 20
225: define e(x){
226: auto a, b, c, i, s
227: a = 1
228: b = 1
229: s = 1
230: for(i=1; 1==1; i++){
231: a = a*x
232: b = b*i
233: c = a/b
234: if(c == 0) return(s)
235: s = s+c
236: }
237: }
238: .Ed
239: .Pp
240: defines a function to compute an approximate value of
241: the exponential function and
242: .Pp
243: .Dl for(i=1; i<=10; i++) e(i)
244: .Pp
245: prints approximate values of the exponential function of
246: the first ten integers.
1.11 ! otto 247: .Sh FILES
! 248: .Bl -tag -width /usr/share/misc/bc.library -compact
! 249: .It Pa /usr/share/misc/bc.library
! 250: math library, read when the
! 251: .Fl l
! 252: option is specified on the command line.
! 253: .El
1.1 otto 254: .Sh SEE ALSO
1.4 jmc 255: .Xr dc 1
1.1 otto 256: .Rs
1.11 ! otto 257: .%B USD
! 258: .%V 06
1.1 otto 259: .%A L. L. Cherry
260: .%A R. Morris
261: .%T "BC \- An arbitrary precision desk-calculator language"
262: .Re
1.3 otto 263: .Sh STANDARDS
264: The
265: .Nm
266: utility is expected to conform to the
267: .St -p1003.2
1.4 jmc 268: specification.
1.1 otto 269: .Sh HISTORY
270: The
1.4 jmc 271: .Nm
1.11 ! otto 272: first command appeared in
1.1 otto 273: .At v6 .
1.11 ! otto 274: A complete rewrite of the
! 275: .Nm
! 276: command first appeared in
! 277: .Ox 3.5 .
! 278: .Sh AUTHORS
! 279: The original version of the
! 280: .Nm
! 281: command was written by
! 282: .An Robert Morris
! 283: and
! 284: .An Lorinda Cherry .
! 285: The current version of the
! 286: .Nm
! 287: utility was written by
! 288: .An Otto Moerbeek .
1.1 otto 289: .Sh BUGS
1.4 jmc 290: No
291: .Sq && ,
292: .Sq \(or\(or ,
293: or
294: .Sq \&!
295: operators.
1.1 otto 296: .Pp
297: .Ql Quit
298: is interpreted when read, not when executed.
1.3 otto 299: .Pp
300: Some non-portable extensions, as found in the GNU version of the
301: .Nm
302: utility are not implemented (yet).