COHERENT manpages
This page displays the COHERENT manpage for bc [Interactive calculator with arbitrary precision].
List of available manpages
Index
bc -- Command
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 interactive calculator,
where the user types arithmetic expressions in a syntax reminiscent of C.
If you invoke bc with no file argument, it reads the standard input. For
example:
Input Output
(1000+23)*42 42966
k = 2^10
16 * k 16384
2 ^ 100 1267650600228229401496703205376
You can invoke bc with one or more file arguments. After bc reads each
file, it reads the standard input. This provides a convenient way to read
programs that are stored in files. COHERENT includes a library of
mathematical functions for bc; to use it, invoke bc with its option -l.
The following summarizes briefly the facilities provided by bc. More
information is available in the tutorial to bc that is included with this
manual.
The delimiters `/*' and `*/' enclose comments. Names of variables or
functions consist of a lower-case letter followed by any number of letters
or digits. (Names cannot begin with an upper-case letter because numbers
with a base greater than ten may need upper-case letters for their
notation.) The three built-in variables obase, ibase, and scale represent,
respectively, 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). Variables may be simple
variables or arrays, and need not be pre-declared, with the exception of
variables internal 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 upper-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
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.
bc recognizes the following operators:
+ - * / % ^ ++
-- = += -= *= /= %=
^= == != < <= >>=
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 semicolons or newlines, and may
be grouped with braces into compound statements.
bc prints the value of any statement that is an expression 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 library lib.b holds code written in bc for the following mathematical
variables and functions:
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
If you invoke bc with its option -l, it reads lib.b and thus makes the
above functions and constants available to you.
Examples
The first example calculates the factorial of its positive integer 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,
libmp
bc Desk Calculator Language, tutorial
Notes
Line numbers do not accompany error messages in source files.
bc performs integer calculations with arbitrary precision, limited only by
the memory available. However, the results of some calculations on numbers
with fractional parts depends on the specified scale; see the tutorial for
details.











