bc Command bc
Interactive calculator with arbitrary precision
bc [ -l ] [ file ... ]
bc is a language that performs calculations on numbers with an
arbitrary number of digits. bc is most commonly used as an in-
teractive calculator, where the user types arithmetic expressions
in a syntax reminiscent of C. If bc is invoked with no file ar-
guments on its command line, it reads the standard input. For
example:
Input Output
bc
(1000+23)*42 42966
k = 2^10
16 * k 16384
2 ^ 100 1267650600228229401496703205376
bc may also be invoked with one or more file arguments. After bc
reads each file, it reads the standard input. This provides a
convenient way to access programs in files. A library of mathe-
matical functions is available, obtained by using the -l option.
The following summarizes briefly the facilities provided by bc.
More information is available in the tutorial to bc that is in-
cluded with this manual.
Comments are enclosed between the delimiters `/*' and `*/'.
Names of variables or functions must begin with a lower-case let-
ter, and may have any number of subsequent letters or digits.
Names may not begin with an upper-case letter because numbers
with a base greater than ten may need need upper-case letters for
their notation. The three built-in variables obase, ibase, and
scale represent the number base for printing numbers (default,
ten), the number base for reading numbers (default, ten), and the
number of digits after the decimal (radix) point (default, zero),
respectively. Variables may be simple variables or arrays, and
need not be pre-declared, with the exception of variables inter-
nal to functions. Some examples of variables and array elements
are x25, array[10], and number.
Numbers are any string of digits, and may have one decimal point.
Digits are taken from the ordinary digits (0-9) and then the up-
per-case letters (A-F), in that order.
Certain names are reserved for use as key words. The key words
recognized by bc include the following:
if, for, do, while
Test conditions and define loops, with syntax identical to C
COHERENT Lexicon Page 1
bc Command bc
break, continue
Alter control flow within for and while loops.
quit Tell bc to exit immediately
define function (arg, ..., arg)
Define a bc function by a compound statement, as in C.
auto var, ..., var
Define variables that are local to a function, rather than
having global scope.
return (value)
Return a value from a function.
scale (value)
Return the number of digits to the right of the decimal
point in value.
sqrt (value)
Return the square root of value
length (value)
Return the number of decimal digits in value.
The following operators are recognized:
+ -* / % ^ ++
-- =+= -= *= /= %=
^= ==!= < <= > >=
These operators are similar to those in C, with the exception of
^ and ^=, which are exponentiation operators. Expressions can be
grouped with parentheses. Statements are separated with semi-
colons or newlines, and may be made into compound statements with
braces. bc prints the value of any statement that is an expres-
sion but is not an assignment.
As in the editor ed, an `!' at the beginning of a line causes
that line to be sent as a command to the COHERENT shell sh.
The built-in mathematics library contains the following functions
and variables:
atan(z) Arctangent of z
cos(z) Cosine of z
exp(z) Exponential function of z
j(n,z) nth order Bessel function of z
ln(z) Natural logarithm of z
pi Value of pi to 100 digits
sin(z) Sine of z
COHERENT Lexicon Page 2
bc Command bc
***** Examples *****
The first example calculates the factorial of its positive in-
teger argument by recursion.
/*
* Factorial function implemented by recursion.
*/
define fact(n) {
if (n <= 1) return (n);
return (n * fact(n-1));
}
The second example also calculates the factorial of its positive
integer argument, this time by iteration.
/*
* Factorial function implemented by iteration.
*/
define fact(n) {
auto result;
result = 1;
for (i=1; i<=n; i++) result *= i;
return (result);
}
***** Files *****
/usr/lib/lib.b -- Source code for the library
***** See Also *****
commands, conv, dc, multi-precision arithmetic
bc Desk Calculator Language, tutorial
***** Notes *****
Line numbers do not accompany error messages in source files.
COHERENT Lexicon Page 3