1 .\" $OpenBSD: bc.1,v 1.17 2004/08/25 21:59:59 jmc Exp $
2 .\" $DragonFly: src/usr.bin/bc/bc.1,v 1.1 2004/09/20 04:20:34 dillon Exp $
4 .\" Copyright (C) Caldera International Inc. 2001-2002.
5 .\" All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code and documentation must retain the above
11 .\" copyright notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" This product includes software developed or owned by Caldera
18 .\" International, Inc.
19 .\" 4. Neither the name of Caldera International, Inc. nor the names of other
20 .\" contributors may be used to endorse or promote products derived from
21 .\" this software without specific prior written permission.
23 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24 .\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 .\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28 .\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 .\" POSSIBILITY OF SUCH DAMAGE.
36 .\" @(#)bc.1 6.8 (Berkeley) 8/8/91
42 .Nd arbitrary-precision arithmetic language and calculator
49 is an interactive processor for a language which resembles
50 C but provides unlimited precision arithmetic.
51 It takes input from any files given, then reads
58 is actually a preprocessor for
60 which it invokes automatically, unless the
64 In this case the generated
66 instructions are sent to the standard output,
67 instead of being interpreted by a running
72 of an arbitrary precision math library.
77 programs is as follows:
84 As a non-portable extension, it is possible to use long names
85 in addition to single letter names.
86 A long name is a sequence starting with a lowercase letter
87 followed by any number of lowercase letters and digits.
88 The underscore character
93 .Bd -unfilled -offset indent -compact
94 are enclosed in /* and */
95 are enclosed in # and the next newline
98 The newline is not part of the line comment,
99 which in itself is a non-portable extension.
102 .Bd -unfilled -offset indent -compact
104 array elements: L [ E ]
105 The words `ibase', `obase', and `scale'
106 The word `last' or a single dot
110 .Bd -unfilled -offset indent -compact
111 arbitrarily long numbers with optional sign and decimal point
114 length ( E ) number of significant decimal digits
115 scale ( E ) number of digits right of decimal point
120 .Sq \e<newline><whitespace>
121 is ignored within numbers.
125 The following arithmetic and logical operators can be used.
126 The semantics of the operators is the same as in the C language.
127 They are listed in order of decreasing precedence.
128 Operators in the same group have the same precedence.
129 .Bl -column -offset indent "= += \-= *= /= %= ^=" "Associativity" \
130 "multiply, divide, modulus"
131 .It Sy "Operator" Ta Sy "Associativity" Ta Sy "Description"
132 .It "++ \-\-" Ta "none" Ta "increment, decrement"
133 .It "\-" Ta "none" Ta "unary minus"
134 .It "^" Ta "right" Ta "power"
135 .It "* / %" Ta "left" Ta "multiply, divide, modulus"
136 .It "+ \-" Ta "left" Ta "plus, minus"
137 .It "= += -= *= /= %= ^=" Ta "right" Ta "assignment"
138 .It "== <= >= != < >" Ta "none" Ta "relational"
139 .It "!" Ta "none" Ta "boolean not"
140 .It "&&" Ta "left" Ta "boolean and"
141 .It "||" Ta "left" Ta "boolean or"
145 .Bl -bullet -offset indent
147 The relational operators may appear in any expression.
150 standard only allows them in the conditional expression of an
157 The relational operators have a lower precedence than the assignment
159 This has the consequence that the expression
163 which is probably not what the programmer intended.
165 In contrast with the C language, the relational operators all have
166 the same precedence, and are non-associative.
169 will produce a syntax error.
171 The boolean operators (!, && and ||) are non-portable extensions.
174 (!) operator has much lower precedence than the same operator in the
176 This has the consequence that the expression
180 Prudent programmers use parentheses when writing expressions involving
185 .Bd -unfilled -offset indent -compact
196 a string of characters, enclosed in double quotes
200 A string may contain any character, except double quote.
201 The if statement with an else branch is a non-portable extension.
202 All three E's in a for statement may be empty.
203 This is a non-portable extension.
204 The continue and print statements are also non-portable extensions.
206 The print statement takes a list of comma-separated expressions.
207 Each expression in the list is evaluated and the computed
208 value is printed and assigned to the variable `last'.
209 No trailing newline is printed.
210 The expression may also be a string enclosed in double quotes.
211 Within these strings the following escape sequences may be used:
228 Any other character following a backslash will be ignored.
229 Strings will not be assigned to `last'.
232 .Bd -unfilled -offset indent
233 define L ( L ,..., L ) {
240 As a non-portable extension, the opening brace of the define statement
241 may appear on the next line.
242 The return statement may also appear in the following forms:
243 .Bd -unfilled -offset indent
249 The first two are equivalent to the statement
251 The last form is a non-portable extension.
252 Not specifying a return statement is equivalent to writing
255 Functions available in the math library, which is loaded by specifying the
257 flag on the command line
259 .Bl -tag -width j(n,x) -offset indent -compact
274 All function arguments are passed by value.
276 The value of a statement that is an expression is printed
277 unless the main operator is an assignment.
278 The value printed is assigned to the special variable `last'.
279 This is a non-portable extension.
280 A single dot may be used as a synonym for `last'.
281 Either semicolons or newlines may separate statements.
284 influences the number of digits to be retained on arithmetic
285 operations in the manner of
291 set the input and output number radix respectively.
293 The same letter may be used as an array, a function,
294 and a simple variable simultaneously.
295 All variables are global to the program.
296 `Auto' variables are pushed down during function calls.
297 When using arrays as function arguments
298 or defining them as automatic variables,
299 empty square brackets must follow the array name.
302 .Bd -literal -offset indent
319 defines a function to compute an approximate value of
320 the exponential function and
322 .Dl for(i=1; i<=10; i++) e(i)
324 prints approximate values of the exponential function of
325 the first ten integers.
327 .Bl -tag -width /usr/share/misc/bc.library -compact
328 .It Pa /usr/share/misc/bc.library
329 math library, read when the
331 option is specified on the command line.
336 "BC \- An Arbitrary Precision Desk-Calculator Language",
337 .Pa /usr/share/doc/usd/06.bc/ .
341 utility is expected to conform to the
347 first command appeared in
349 A complete rewrite of the
351 command first appeared in
354 The original version of the
356 command was written by
360 The current version of the
362 utility was written by
366 is interpreted when read, not when executed.
368 Some non-portable extensions, as found in the GNU version of the
370 utility are not implemented (yet).