1 .\" $OpenBSD: bc.1,v 1.22 2007/05/31 19:20:07 jmc Exp $
3 .\" Copyright (C) Caldera International Inc. 2001-2002.
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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.
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.
35 .\" @(#)bc.1 6.8 (Berkeley) 8/8/91
42 .Nd arbitrary-precision arithmetic language and calculator
46 .Op Fl e Ar expression
50 is an interactive processor for a language which resembles
51 C but provides unlimited precision arithmetic.
52 It takes input from any expressions on the command line and
53 any files given, then reads the standard input.
59 is actually a preprocessor for
61 which it invokes automatically, unless the
65 In this case the generated
67 instructions are sent to the standard output,
68 instead of being interpreted by a running
71 .It Fl e Ar expression
76 options are specified, they are processed in the order given,
77 separated by newlines.
79 Allow specification of an arbitrary precision math library.
80 The definitions in the library are available to command line
86 programs is as follows:
93 As a non-portable extension, it is possible to use long names
94 in addition to single letter names.
95 A long name is a sequence starting with a lowercase letter
96 followed by any number of lowercase letters and digits.
97 The underscore character
102 .Bd -unfilled -offset indent -compact
103 are enclosed in /* and */
104 are enclosed in # and the next newline
107 The newline is not part of the line comment,
108 which in itself is a non-portable extension.
111 .Bd -unfilled -offset indent -compact
113 array elements: L [ E ]
114 The words `ibase', `obase', and `scale'
115 The word `last' or a single dot
119 .Bd -unfilled -offset indent -compact
120 arbitrarily long numbers with optional sign and decimal point
123 length ( E ) number of significant decimal digits
124 scale ( E ) number of digits right of decimal point
129 .Sq \e<newline><whitespace>
130 is ignored within numbers.
134 The following arithmetic and logical operators can be used.
135 The semantics of the operators is the same as in the C language.
136 They are listed in order of decreasing precedence.
137 Operators in the same group have the same precedence.
138 .Bl -column "= += \-= *= /= %= ^=" "Associativity" "multiply, divide, modulus" -offset indent
139 .It Sy "Operator" Ta Sy "Associativity" Ta Sy "Description"
140 .It "++ \-\-" Ta "none" Ta "increment, decrement"
141 .It "\-" Ta "none" Ta "unary minus"
142 .It "^" Ta "right" Ta "power"
143 .It "* / %" Ta "left" Ta "multiply, divide, modulus"
144 .It "+ \-" Ta "left" Ta "plus, minus"
145 .It "= += -= *= /= %= ^=" Ta "right" Ta "assignment"
146 .It "== <= >= != < >" Ta "none" Ta "relational"
147 .It "!" Ta "none" Ta "boolean not"
148 .It "&&" Ta "left" Ta "boolean and"
149 .It "||" Ta "left" Ta "boolean or"
153 .Bl -bullet -offset indent
155 The relational operators may appear in any expression.
158 standard only allows them in the conditional expression of an
165 The relational operators have a lower precedence than the assignment
167 This has the consequence that the expression
171 which is probably not what the programmer intended.
173 In contrast with the C language, the relational operators all have
174 the same precedence, and are non-associative.
177 will produce a syntax error.
179 The boolean operators (!, && and ||) are non-portable extensions.
182 (!) operator has much lower precedence than the same operator in the
184 This has the consequence that the expression
188 Prudent programmers use parentheses when writing expressions involving
193 .Bd -unfilled -offset indent -compact
204 a string of characters, enclosed in double quotes
208 A string may contain any character, except a double quote.
209 The if statement with an else branch is a non-portable extension.
210 All three E's in a for statement may be empty.
211 This is a non-portable extension.
212 The continue and print statements are also non-portable extensions.
214 The print statement takes a list of comma-separated expressions.
215 Each expression in the list is evaluated and the computed
216 value is printed and assigned to the variable `last'.
217 No trailing newline is printed.
218 The expression may also be a string enclosed in double quotes.
219 Within these strings the following escape sequences may be used:
236 Any other character following a backslash will be ignored.
237 Strings will not be assigned to `last'.
240 .Bd -unfilled -offset indent
241 define L ( L ,..., L ) {
248 As a non-portable extension, the opening brace of the define statement
249 may appear on the next line.
250 The return statement may also appear in the following forms:
251 .Bd -unfilled -offset indent
257 The first two are equivalent to the statement
259 The last form is a non-portable extension.
260 Not specifying a return statement is equivalent to writing
263 Functions available in the math library, which is loaded by specifying the
265 flag on the command line
267 .Bl -tag -width j(n,x) -offset indent -compact
282 All function arguments are passed by value.
284 The value of a statement that is an expression is printed
285 unless the main operator is an assignment.
286 The value printed is assigned to the special variable `last'.
287 This is a non-portable extension.
288 A single dot may be used as a synonym for `last'.
289 Either semicolons or newlines may separate statements.
292 influences the number of digits to be retained on arithmetic
293 operations in the manner of
299 set the input and output number radix respectively.
301 The same letter may be used as an array, a function,
302 and a simple variable simultaneously.
303 All variables are global to the program.
304 `Auto' variables are pushed down during function calls.
305 When using arrays as function arguments
306 or defining them as automatic variables,
307 empty square brackets must follow the array name.
310 .Bd -literal -offset indent
327 defines a function to compute an approximate value of
328 the exponential function and
330 .Dl for(i=1; i<=10; i++) e(i)
332 prints approximate values of the exponential function of
333 the first ten integers.
334 .Bd -literal -offset indent
335 $ bc -l -e 'scale = 500; 2 * a(2^10000)' -e quit
337 prints an approximation of pi.
339 .Bl -tag -width /usr/share/misc/bc.library -compact
340 .It Pa /usr/share/misc/bc.library
341 math library, read when the
343 option is specified on the command line.
350 .%B "4.4BSD Users's Supplementary Documents (USD)"
351 .%T "BC \- An Arbitrary Precision Desk-Calculator Language"
356 utility is compliant with the
362 are extensions to that specification.
366 command first appeared in
368 A complete rewrite of the
370 command first appeared in
374 The original version of the
376 command was written by
380 The current version of the
382 utility was written by
386 is interpreted when read, not when executed.
388 Some non-portable extensions, as found in the GNU version of the
390 utility are not implemented (yet).