BSD 4_3_Tahoe development
[unix-history] / usr / man / cat1 / bc.0
BC(1) UNIX Programmer's Manual BC(1)
N\bNA\bAM\bME\bE
bc - arbitrary-precision arithmetic language and calculator
S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS
b\bbc\bc [ -\b-c\bc ] [ -\b-l\bl ] [ file ... ]
D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
_\bB_\bc is an interactive processor for a language which resem-
bles C but provides unlimited precision arithmetic. It
takes input from any files given, then reads the standard
input. The -\b-l\bl argument stands for the name of an arbitrary
precision math library. The syntax for _\bb_\bc programs is as
follows; L means letter a-z, E means expression, S means
statement.
Comments
are enclosed in /* and */.
Names
simple variables: L
array elements: L [ E ]
The words `ibase', `obase', and `scale'
Other operands
arbitrarily long numbers with optional sign and
decimal point.
( E )
sqrt ( E )
length ( E ) number of significant decimal digits
scale ( E ) number of digits right of decimal point
L ( E , ... , E )
Operators
+ - * / % ^ (% is remainder; ^ is power)
++ -- (prefix and postfix; apply to names)
== <= >= != < >
= += -= *= /= %= ^=
Statements
E
{ S ; ... ; S }
if ( E ) S
while ( E ) S
for ( E ; E ; E ) S
null statement
break
quit
Function definitions
define L ( L ,..., L ) {
auto L, ... , L
S; ... S
Printed 7/9/88 July 28, 1987 1
BC(1) UNIX Programmer's Manual BC(1)
return ( E )
}
Functions in -\b-l\bl math library
s(x) sine
c(x) cosine
e(x) exponential
l(x) log
a(x) arctangent
j(n,x) Bessel function
All function arguments are passed by value.
The value of a statement that is an expression is printed
unless the main operator is an assignment. Either semi-
colons or newlines may separate statements. Assignment to
_\bs_\bc_\ba_\bl_\be influences the number of digits to be retained on
arithmetic operations in the manner of _\bd_\bc(1). Assignments
to _\bi_\bb_\ba_\bs_\be or _\bo_\bb_\ba_\bs_\be set the input and output number radix
respectively.
The same letter may be used as an array, a function, and a
simple variable simultaneously. All variables are global to
the program. `Auto' variables are pushed down during func-
tion calls. When using arrays as function arguments or
defining them as automatic variables empty square brackets
must follow the array name.
For example
scale = 20
define e(x){
auto a, b, c, i, s
a = 1
b = 1
s = 1
for(i=1; 1==1; i++){
a = a*x
b = b*i
c = a/b
if(c == 0) return(s)
s = s+c
}
}
defines a function to compute an approximate value of the
exponential function and
for(i=1; i<=10; i++) e(i)
prints approximate values of the exponential function of the
first ten integers.
Printed 7/9/88 July 28, 1987 2
BC(1) UNIX Programmer's Manual BC(1)
_\bB_\bc is actually a preprocessor for _\bd_\bc(1), which it invokes
automatically, unless the -\b-c\bc (compile only) option is
present. In this case the _\bd_\bc input is sent to the standard
output instead.
F\bFI\bIL\bLE\bES\bS
/usr/lib/lib.b mathematical library
dc(1) desk calculator proper
S\bSE\bEE\bE A\bAL\bLS\bSO\bO
dc(1)
L. L. Cherry and R. Morris, _\bB_\bC - _\bA_\bn _\ba_\br_\bb_\bi_\bt_\br_\ba_\br_\by _\bp_\br_\be_\bc_\bi_\bs_\bi_\bo_\bn
_\bd_\be_\bs_\bk-_\bc_\ba_\bl_\bc_\bu_\bl_\ba_\bt_\bo_\br _\bl_\ba_\bn_\bg_\bu_\ba_\bg_\be
B\bBU\bUG\bGS\bS
No &&, ||, or ! operators.
_\bF_\bo_\br statement must have all three E's.
_\bQ_\bu_\bi_\bt is interpreted when read, not when executed.
Printed 7/9/88 July 28, 1987 3