2 .\" $OpenBSD: bc.1,v 1.30 2014/01/14 07:42:42 jmc 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
43 .Nd arbitrary-precision arithmetic language and calculator
47 .Op Fl e Ar expression
51 is an interactive processor for a language which resembles
52 C but provides unlimited precision arithmetic.
53 It takes input from any expressions on the command line and
54 any files given, then reads the standard input.
60 is actually a preprocessor for
62 which it invokes automatically, unless the
66 In this case the generated
68 instructions are sent to the standard output,
69 instead of being interpreted by a running
72 .It Fl e Ar expression , Fl Fl expression Ar expression
77 options are specified, they are processed in the order given,
78 separated by newlines.
80 Prints usage information.
81 .It Fl l , Fl Fl mathlib
82 Allow specification of an arbitrary precision math library.
83 The definitions in the library are available to command line
85 .It Fl v , Fl Fl version
86 Prints version information.
91 programs is as follows:
98 As a non-portable extension, it is possible to use long names
99 in addition to single letter names.
100 A long name is a sequence starting with a lowercase letter
101 followed by any number of lowercase letters and digits.
102 The underscore character
107 .Bd -unfilled -offset indent -compact
108 are enclosed in /* and */
109 are enclosed in # and the next newline
112 The newline is not part of the line comment,
113 which in itself is a non-portable extension.
116 .Bd -unfilled -offset indent -compact
118 array elements: L [ E ]
119 The words `ibase', `obase', and `scale'
120 The word `last' or a single dot
124 .Bd -unfilled -offset indent -compact
125 arbitrarily long numbers with optional sign and decimal point
128 length ( E ) number of significant decimal digits
129 scale ( E ) number of digits right of decimal point
134 .Sq \e<newline><whitespace>
135 is ignored within numbers.
139 The following arithmetic and logical operators can be used.
140 The semantics of the operators is the same as in the C language.
141 They are listed in order of decreasing precedence.
142 Operators in the same group have the same precedence.
143 .Bl -column "= += \-= *= /= %= ^=" "Associativity" "multiply, divide, modulus" -offset indent
144 .It Sy "Operator" Ta Sy "Associativity" Ta Sy "Description"
145 .It "++ \-\-" Ta "none" Ta "increment, decrement"
146 .It "\-" Ta "none" Ta "unary minus"
147 .It "^" Ta "right" Ta "power"
148 .It "* / %" Ta "left" Ta "multiply, divide, modulus"
149 .It "+ \-" Ta "left" Ta "plus, minus"
150 .It "= += -= *= /= %= ^=" Ta "right" Ta "assignment"
151 .It "== <= >= != < >" Ta "none" Ta "relational"
152 .It "!" Ta "none" Ta "boolean not"
153 .It "&&" Ta "left" Ta "boolean and"
154 .It "||" Ta "left" Ta "boolean or"
158 .Bl -bullet -offset indent
160 The relational operators may appear in any expression.
163 standard only allows them in the conditional expression of an
170 The relational operators have a lower precedence than the assignment
172 This has the consequence that the expression
176 which is probably not what the programmer intended.
178 In contrast with the C language, the relational operators all have
179 the same precedence, and are non-associative.
182 will produce a syntax error.
184 The boolean operators (!, && and ||) are non-portable extensions.
187 (!) operator has much lower precedence than the same operator in the
189 This has the consequence that the expression
193 Prudent programmers use parentheses when writing expressions involving
198 .Bd -unfilled -offset indent -compact
209 a string of characters, enclosed in double quotes
213 A string may contain any character, except double quote.
214 The if statement with an else branch is a non-portable extension.
215 All three E's in a for statement may be empty.
216 This is a non-portable extension.
217 The continue and print statements are also non-portable extensions.
219 The print statement takes a list of comma-separated expressions.
220 Each expression in the list is evaluated and the computed
221 value is printed and assigned to the variable `last'.
222 No trailing newline is printed.
223 The expression may also be a string enclosed in double quotes.
224 Within these strings the following escape sequences may be used:
241 Any other character following a backslash will be ignored.
242 Strings will not be assigned to `last'.
245 .Bd -unfilled -offset indent
246 define L ( L ,..., L ) {
253 As a non-portable extension, the opening brace of the define statement
254 may appear on the next line.
255 The return statement may also appear in the following forms:
256 .Bd -unfilled -offset indent
262 The first two are equivalent to the statement
264 The last form is a non-portable extension.
265 Not specifying a return statement is equivalent to writing
268 Functions available in the math library, which is loaded by specifying the
270 flag on the command line
272 .Bl -tag -width j(n,x) -offset indent -compact
287 All function arguments are passed by value.
289 The value of a statement that is an expression is printed
290 unless the main operator is an assignment.
291 The value printed is assigned to the special variable `last'.
292 This is a non-portable extension.
293 A single dot may be used as a synonym for `last'.
294 Either semicolons or newlines may separate statements.
297 influences the number of digits to be retained on arithmetic
298 operations in the manner of
304 set the input and output number radix respectively.
306 The same letter may be used as an array, a function,
307 and a simple variable simultaneously.
308 All variables are global to the program.
309 `Auto' variables are pushed down during function calls.
310 When using arrays as function arguments
311 or defining them as automatic variables,
312 empty square brackets must follow the array name.
315 .Bd -literal -offset indent
332 defines a function to compute an approximate value of
333 the exponential function and
335 .Dl for(i=1; i<=10; i++) e(i)
337 prints approximate values of the exponential function of
338 the first ten integers.
339 .Bd -literal -offset indent
340 $ bc -l -e 'scale = 500; 2 * a(2^10000)' -e quit
343 prints an approximation of pi.
344 .Sh COMMAND LINE EDITING
346 supports interactive command line editing, via the
349 It is enabled by default if input is from a tty.
350 Previous lines can be recalled and edited with the arrow keys,
351 and other GNU Emacs-style editing keys may be used as well.
355 library is configured with a
359 for more information.
361 .Bl -tag -width /usr/share/misc/bc.library -compact
362 .It Pa /usr/share/misc/bc.library
363 math library, read when the
365 option is specified on the command line.
372 options are no-ops for compatibility with some other implementations of
374 and their use is discouraged.
380 utility is compliant with the
386 as well as the parts noted above,
387 are extensions to that specification.
391 command first appeared in
393 A complete rewrite of the
395 command first appeared in
399 The original version of the
401 command was written by
405 The current version of the
407 utility was written by
411 is interpreted when read, not when executed.
413 Some non-portable extensions, as found in the GNU version of the
415 utility are not implemented (yet).