Annotation of src/usr.bin/bc/bc.1, Revision 1.10
1.10 ! otto 1: .\" $OpenBSD: bc.1,v 1.9 2003/10/22 12:24:41 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
107: Operators
108: .Bd -unfilled -offset indent -compact
1.4 jmc 109: + \- * / % ^ (`%' is remainder; `^' is power)
110: ++ \-\- (prefix and postfix; apply to names)
111: == <= >= != < >
112: = += \-= *= /= %= ^=
1.1 otto 113: .Ed
114: .Pp
115: Statements
116: .Bd -unfilled -offset indent -compact
117: E
118: { S ; ... ; S }
119: if ( E ) S
1.9 otto 120: if ( E ) S else S
1.1 otto 121: while ( E ) S
122: for ( E ; E ; E ) S
123: null statement
124: break
1.6 otto 125: continue
1.1 otto 126: quit
1.6 otto 127: a string of characters, enclosed in double quotes
1.1 otto 128: .Ed
129: .Pp
1.9 otto 130: The if statement with an else branch is a non-portable extension.
1.8 jmc 131: All three E's in a for statement may be empty.
1.7 otto 132: This is a non-portable extension.
1.8 jmc 133: The continue statement is also a non-portable extension.
1.7 otto 134: .Pp
1.1 otto 135: Function definitions
136: .Bd -unfilled -offset indent -compact
137: define L ( L ,..., L ) {
138: auto L, ... , L
139: S; ... S
140: return ( E )
141: }
142: .Ed
1.5 otto 143: .Pp
144: The sequence
145: .Sq \e\<newline><whitespace>
146: is ignored within numbers.
1.6 otto 147: A string may contain any character, except double quote.
1.1 otto 148: .Pp
149: Functions in
1.4 jmc 150: .Fl lm
1.1 otto 151: math library
152: .Bl -tag -width j(n,x) -offset indent -compact
153: .It s(x)
154: sine
155: .It c(x)
156: cosine
157: .It e(x)
158: exponential
159: .It l(x)
160: log
161: .It a(x)
162: arctangent
163: .It j(n,x)
164: Bessel function
165: .El
166: .Pp
167: All function arguments are passed by value.
168: .Pp
169: The value of a statement that is an expression is printed
170: unless the main operator is an assignment.
1.10 ! otto 171: The value printed is assigned to the special variable `last'.
! 172: This is a non-portable extension.
! 173: A single dot may be used as a synonym for `last'.
1.1 otto 174: Either semicolons or newlines may separate statements.
175: Assignment to
176: .Ar scale
177: influences the number of digits to be retained on arithmetic
178: operations in the manner of
1.4 jmc 179: .Xr dc 1 .
1.1 otto 180: Assignments to
181: .Ar ibase
182: or
183: .Ar obase
184: set the input and output number radix respectively.
185: .Pp
186: The same letter may be used as an array, a function,
187: and a simple variable simultaneously.
188: All variables are global to the program.
189: `Auto' variables are pushed down during function calls.
190: When using arrays as function arguments
1.4 jmc 191: or defining them as automatic variables,
1.1 otto 192: empty square brackets must follow the array name.
193: .Pp
194: For example
195: .Bd -literal -offset indent
196: scale = 20
197: define e(x){
198: auto a, b, c, i, s
199: a = 1
200: b = 1
201: s = 1
202: for(i=1; 1==1; i++){
203: a = a*x
204: b = b*i
205: c = a/b
206: if(c == 0) return(s)
207: s = s+c
208: }
209: }
210: .Ed
211: .Pp
212: defines a function to compute an approximate value of
213: the exponential function and
214: .Pp
215: .Dl for(i=1; i<=10; i++) e(i)
216: .Pp
217: prints approximate values of the exponential function of
218: the first ten integers.
219: .Sh SEE ALSO
1.4 jmc 220: .Xr dc 1
1.1 otto 221: .Rs
222: .%A L. L. Cherry
223: .%A R. Morris
224: .%T "BC \- An arbitrary precision desk-calculator language"
225: .Re
1.3 otto 226: .Sh STANDARDS
227: The
228: .Nm
229: utility is expected to conform to the
230: .St -p1003.2
1.4 jmc 231: specification.
1.1 otto 232: .Sh HISTORY
233: The
1.4 jmc 234: .Nm
1.1 otto 235: command appeared in
236: .At v6 .
237: .Sh BUGS
1.4 jmc 238: No
239: .Sq && ,
240: .Sq \(or\(or ,
241: or
242: .Sq \&!
243: operators.
1.1 otto 244: .Pp
245: .Ql Quit
246: is interpreted when read, not when executed.
1.3 otto 247: .Pp
248: Some non-portable extensions, as found in the GNU version of the
249: .Nm
250: utility are not implemented (yet).