Annotation of src/usr.bin/bc/bc.1, Revision 1.14
1.14 ! otto 1: .\" $OpenBSD: bc.1,v 1.13 2003/11/12 19:35:54 jmc 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
1.14 ! otto 116: .Pp
! 117: The following arithmetic and logical operators can be used.
! 118: The semantics of the operators is the same as in the C language.
! 119: They are listed in order of decreasing precedence.
! 120: Operators in the same group have the same precedence.
! 121: .Bl -column -offset indent "= += \-= *= /= %= ^=" "Associativity" \
! 122: "multiply, divide, modulus"
! 123: .It Sy "Operator" Ta Sy "Associativity" Ta Sy "Description"
! 124: .It "++ \-\-" Ta "none" Ta "increment, decrement"
! 125: .It "\-" Ta "none" Ta "unary minus"
! 126: .It "^" Ta "right" Ta "power"
! 127: .It "* / %" Ta "left" Ta "multiply, divide, modulus"
! 128: .It "+ \-" Ta "left" Ta "plus, minus"
! 129: .It "= += -= *= /= %= ^=" Ta "right" Ta "assignment"
! 130: .It "== <= >= != < >" Ta "none" Ta "relational"
! 131: .It "!" Ta "none" Ta "boolean not"
! 132: .It "&&" Ta "left" Ta "boolean and"
! 133: .It "||" Ta "left" Ta "boolean or"
! 134: .El
! 135: .Pp
! 136: Note the following:
! 137: .Bl -bullet -offset indent
! 138: .It
! 139: The relational operators may appear in any expression.
! 140: The
! 141: .St -p1003.2
! 142: standard only allows them in the conditional expression of an
! 143: .Sq if ,
! 144: .Sq while
! 145: or
! 146: .Sq for
! 147: statement.
! 148: .It
! 149: The relational operators have a lower precedence than the assignment
! 150: operators.
! 151: This has the consequence that the expression
! 152: .Sy a = b < c
! 153: is interpreted as
! 154: .Sy (a = b) < c ,
! 155: which is probably not what the programmer intended.
! 156: .It
! 157: In contrast with the C language, the relational operators all have
! 158: the same precedence, and are non-associative.
! 159: The expression
! 160: .Sy a < b < c
! 161: will produce a syntax error.
! 162: .It
! 163: The boolean operators (!, && and ||) are non-portable extensions.
! 164: .It
! 165: The boolean not
! 166: (!) operator has much lower precedence than the same operator in the
! 167: C language.
! 168: This has the consquence that the expression
! 169: .Sy !a < b
! 170: is interpeted as
! 171: .Sy !(a < b) .
! 172: Prudent programmers use parentheses when writing expressions involving
! 173: boolean operators.
! 174: .El
1.1 otto 175: .Pp
176: Statements
177: .Bd -unfilled -offset indent -compact
178: E
179: { S ; ... ; S }
180: if ( E ) S
1.9 otto 181: if ( E ) S else S
1.1 otto 182: while ( E ) S
183: for ( E ; E ; E ) S
184: null statement
185: break
1.6 otto 186: continue
1.1 otto 187: quit
1.6 otto 188: a string of characters, enclosed in double quotes
1.11 otto 189: print E ,..., E
1.1 otto 190: .Ed
191: .Pp
1.11 otto 192: A string may contain any character, except double quote.
1.9 otto 193: The if statement with an else branch is a non-portable extension.
1.8 jmc 194: All three E's in a for statement may be empty.
1.7 otto 195: This is a non-portable extension.
1.11 otto 196: The continue and print statements are also non-portable extensions.
197: .Pp
198: The print statement takes a list of comma-separated expressions.
199: Each expression in the list is evaluated and the computed
200: value is printed and assigned to the variable `last'.
201: No trailing newline is printed.
202: The expression may also be a string enclosed in double quotes.
203: Within these strings the following escape sequences may be used:
204: .Sq \ea
205: for bell (alert),
206: .Sq \eb
207: for backspace,
208: .Sq \ef
209: for formfeed,
210: .Sq \en
211: for newline,
212: .Sq \er
213: for carriage return,
214: .Sq \et
215: for tab,
216: .Sq \eq
217: for double quote and
218: .Sq \e\e
219: for backslash.
220: Any other character following a backslash will be ignored.
221: Strings will not be assigned to `last'.
1.7 otto 222: .Pp
1.1 otto 223: Function definitions
1.13 jmc 224: .Bd -unfilled -offset indent
1.1 otto 225: define L ( L ,..., L ) {
226: auto L, ... , L
227: S; ... S
228: return ( E )
229: }
230: .Ed
1.12 otto 231: .Pp
232: As a non-portable extension, the opening brace of the define statement
233: may appear on the next line.
234: The return statement may also appear in the following forms:
235: .Bd -unfilled -offset indent
236: return
237: return ()
238: return E
239: .Ed
240: .Pp
241: The first two are equivalent to the statement
242: .Dq return 0 .
243: The last form is a non-portable extension.
244: Not specifying a return statement is equivalent to writing
245: .Dq return (0) .
1.5 otto 246: .Pp
1.11 otto 247: Functions available in the math library, which is loaded by specifying the
248: .Fl l
249: flag on the command line
1.1 otto 250: .Pp
251: .Bl -tag -width j(n,x) -offset indent -compact
252: .It s(x)
253: sine
254: .It c(x)
255: cosine
256: .It e(x)
257: exponential
258: .It l(x)
259: log
260: .It a(x)
261: arctangent
262: .It j(n,x)
263: Bessel function
264: .El
265: .Pp
266: All function arguments are passed by value.
267: .Pp
268: The value of a statement that is an expression is printed
269: unless the main operator is an assignment.
1.10 otto 270: The value printed is assigned to the special variable `last'.
271: This is a non-portable extension.
272: A single dot may be used as a synonym for `last'.
1.1 otto 273: Either semicolons or newlines may separate statements.
274: Assignment to
275: .Ar scale
276: influences the number of digits to be retained on arithmetic
277: operations in the manner of
1.4 jmc 278: .Xr dc 1 .
1.1 otto 279: Assignments to
280: .Ar ibase
281: or
282: .Ar obase
283: set the input and output number radix respectively.
284: .Pp
285: The same letter may be used as an array, a function,
286: and a simple variable simultaneously.
287: All variables are global to the program.
288: `Auto' variables are pushed down during function calls.
289: When using arrays as function arguments
1.4 jmc 290: or defining them as automatic variables,
1.1 otto 291: empty square brackets must follow the array name.
292: .Pp
293: For example
294: .Bd -literal -offset indent
295: scale = 20
296: define e(x){
297: auto a, b, c, i, s
298: a = 1
299: b = 1
300: s = 1
301: for(i=1; 1==1; i++){
302: a = a*x
303: b = b*i
304: c = a/b
305: if(c == 0) return(s)
306: s = s+c
307: }
308: }
309: .Ed
310: .Pp
311: defines a function to compute an approximate value of
312: the exponential function and
313: .Pp
314: .Dl for(i=1; i<=10; i++) e(i)
315: .Pp
316: prints approximate values of the exponential function of
317: the first ten integers.
1.11 otto 318: .Sh FILES
319: .Bl -tag -width /usr/share/misc/bc.library -compact
320: .It Pa /usr/share/misc/bc.library
321: math library, read when the
322: .Fl l
323: option is specified on the command line.
324: .El
1.1 otto 325: .Sh SEE ALSO
1.4 jmc 326: .Xr dc 1
1.1 otto 327: .Rs
1.14 ! otto 328: .%B USD:06
1.1 otto 329: .%A L. L. Cherry
330: .%A R. Morris
331: .%T "BC \- An arbitrary precision desk-calculator language"
332: .Re
1.3 otto 333: .Sh STANDARDS
334: The
335: .Nm
336: utility is expected to conform to the
337: .St -p1003.2
1.4 jmc 338: specification.
1.1 otto 339: .Sh HISTORY
340: The
1.4 jmc 341: .Nm
1.11 otto 342: first command appeared in
1.1 otto 343: .At v6 .
1.11 otto 344: A complete rewrite of the
345: .Nm
346: command first appeared in
347: .Ox 3.5 .
348: .Sh AUTHORS
349: The original version of the
350: .Nm
351: command was written by
352: .An Robert Morris
353: and
354: .An Lorinda Cherry .
355: The current version of the
356: .Nm
357: utility was written by
358: .An Otto Moerbeek .
1.1 otto 359: .Sh BUGS
360: .Ql Quit
361: is interpreted when read, not when executed.
1.3 otto 362: .Pp
363: Some non-portable extensions, as found in the GNU version of the
364: .Nm
365: utility are not implemented (yet).