.TH bc "" "" Command
.PC "Interactive calculator with arbitrary precision"
\fBbc [ \-l ] [ \fIfile ...\fR ]
.PP
.HS
.SH Option:
.IC \fB\-l\fR
Use the extended \fBbc\fR library
.Pp
If no \fIfile\fR is specified, \fBbc\fR reads stdin.
.HE
.II lib.b
.II "library^bc"
.B bc
is a language that performs calculations
on numbers with an arbitrary number of digits.
.B bc
is most commonly used as an interactive calculator,
where the user types arithmetic expressions in a syntax reminiscent of C.
If you invoke
.B bc
with no
.I file
argument, it reads the standard input.
For example:
.DM
.ta 0.5i 1.75i
	\fIInput	Output\fP
	(1000+23)*42	42966
	k = 2^10
	16 * k	16384
	2 ^ 100	1267650600228229401496703205376
.DE
.PP
You can invoke
.B bc
with one or more
.I file
arguments.
After
.B bc
reads each
.IR file ,
it reads the standard input.
This provides a convenient way to read programs that are stored in files.
\*(CO includes
a library of mathematical functions for
.BR bc ;
to use it, invoke
.B bc
with its option
.BR \-l .
.PP
The following summarizes briefly the facilities provided by
.BR bc .
More information is available in the tutorial to \fBbc\fR that
is included with this manual.
.PP
The delimiters `\fB/*\fR' and `\fB*/\fR' 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
.BR obase ,
.BR ibase ,
and
.B 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
.BR x25 ,
.BR array[10] ,
and
.BR number .
.PP
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.
.PP
Certain names are reserved for use as key words.
The key words recognized by
.B bc
include the following:
.IP "\fBif, for, do, while\fR"
Test conditions and define loops,
with syntax identical to C
.IP "\fBbreak, continue\fR"
Alter control flow within
.B for
and
.B while
loops.
.IP \fBquit\fR
Tell
.B bc
to exit immediately
.IP "\fBdefine \fIfunction \fB(\fIarg, ..., arg\^\fB)\fR"
Define a
.B bc
function by a compound statement, as in C.
.IP "\fBauto \fIvar, ..., var\fR"
Define variables that are local to a function,
rather than having global scope.
.IP "\fBreturn (\fIvalue\^\fB)\fR"
Return a value from a function.
.IP "\fBscale (\fIvalue\^\fB)\fR"
Return the number of digits to the right of the decimal point in
.IR value .
.IP "\fBsqrt (\fIvalue\^\fB)\fR"
Return the square root of
.I value
.IP "\fBlength (\fIvalue\^\fB)\fR"
Return the number of decimal digits in
.IR value .
.PP
.B bc
recognizes the following operators:
.DM
	+	\-	*	/	%	^	++
	\-\-	=	+=	\-=	*=	/=	%=
	^=	==	!=	<	<=	>	>=
.DE
.PP
These operators are similar to those in C,
with the exception of \fB^\fR and \fB^=\fR,
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.
.PP
.B bc
prints the value of any statement that is an expression
but is not an assignment.
.PP
As in the editor
.BR ed ,
an `!' at the beginning of a line causes that line to
be sent as a command to the \*(CO shell
.BR sh .
.PP
The library
.B lib.b
holds code written in
.B bc
for the following mathematical variables and functions:
.DS
.ta 0.75i
\fBatan\fP(\fIz\fP)	Arctangent of \fIz\fP
\fBcos\fP(\fIz\fP)	Cosine of \fIz\fP
\fBexp\fP(\fIz\fP)	Exponential function of \fIz\fP
\fBj\fP(\fIn,z\fP)	\fIn\fPth order Bessel function of \fIz\fP
\fBln\fP(\fIz\fP)	Natural logarithm of \fIz\fP
\fBpi\fP	Value of pi to 100 digits
\fBsin\fP(\fIz\fP)	Sine of \fIz\fP
.DE
.PP
If you invoke
.B bc
with its option
.BR \-l ,
it reads
.B lib.b
and thus makes the above functions and constants available to you.
.SH Examples
The first example
calculates the factorial of its positive integer argument
by recursion.
.DM
/*
 * Factorial function implemented by recursion.
 */
define fact(n) {
	if (n <= 1) return (n);
	return (n * fact(n\-1));
}
.DE
.PP
The second example also calculates the factorial of its
positive integer argument, this time by iteration.
.DM
/*
 * Factorial function implemented by iteration.
 */
define fact(n) {
	auto result;

	result = 1;
	for (i=1; i<=n; i++) result *= i;
	return (result);
}
.DE
.SH Files
.II /usr/lib/lib.b
\fB/usr/lib/lib.b\fR \(em Source code for the library
.SH "See Also"
.Xr commands, commands
.Xr conv, conv
.Xr dc, dc
.Xr libmp libmp
.br
\fIbc Desk Calculator Language\fR, tutorial
.SH Notes
Line numbers do not accompany error messages in source files.
.PP
.B 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
.BR scale ;
see the tutorial for details.
