bc(1) bc(1)
NAME
bc - arbitrary precision arithmetic language
SYNOPSIS
bc [-c] [-d] [-l] [--] [file]...
DESCRIPTION
You can use bc to perform arithmetical calculations. bc is an interac-
tive program for a C-like input language and is actually a preproces-
sor for dc(1). bc automatically invokes the desk calculator dc and
outputs results with any required degree of precision.
OPTIONS
-c
-d bc simply generates statements for the desk calculator dc and
writes them to standard output, without actually invoking dc. The
-d option has the same effect as the -c option.
-l -l stands for /usr/lib/lib.b, which is a library containing bc
programs for various mathematical functions.
The -l option must be specified if you want to use any the fol-
lowing functions:
s(x) sine
c(x) cosine
e(x) base e exponential function
l(x) natural logarithm
a(x) arctangent
j(n,x) n-th order Bessel function
-- If file begins with a dash (-), the end of the command-line
options must be marked with --.
file Name of file containing a bc program. You may specify more than
one file. When all statements from all files have been processed,
bc reads from standard input. You can then enter further state-
ments.
file not specified: bc reads from standard input.
Page 1 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
ELEMENTS OF BC PROGRAMS
A bc program consists of
- definitions
- statements
- comments
The following symbols are used in defining the structure of bc pro-
grams:
L stands for one of the letters a-z
E stands for an expression
S stands for a statement
Comments
Comments are enclosed in /* ... */ as in C.
Statements
Statements in bc can be:
⊕ Expressions (see Expressions)
The value of a statement that is an expression is printed unless
the main operator is an assignment.
⊕ Blocks (grouped statements): {S; ...; S}
⊕ Conditional statement: if (E) S
If expression E is true, i.e. has a non-zero value, then statement
S is executed.
⊕ Iteration statements:
- while (E) S
Expression E is evaluated and if it has a non-zero value, state-
ment S is executed. E is then evaluated again, and S is executed
again if E is still non-zero. This process is repeated for as
long as E has a non-zero value.
Page 2 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
- for (E; E; E) S
First, the first expression is evaluated. Next, the second
expression is evaluated and if it has a non-zero value, state-
ment S is executed. Lastly, the third expression is evaluated.
Then the second expression is evaluated again and statement S is
again executed if it is non-zero, and so on. Unlike in C pro-
grams, a for statement must always contain three expressions.
⊕ Jump statement: break
The break statement can be used only within an iteration statement.
It causes termination of the nearest while or for statement. Pro-
gram execution continues with the statement that follows the ter-
minated iteration statement.
⊕ Termination statement: quit
The quit statement stops execution of a bc program. The quit state-
ment is interpreted as soon as it is read, not when the bc program
is executed.
Example:
The following bc program terminates immediately without printing
the value of a:
a=5
if (a>10) quit
(a)
You can separate statements from one another with a semicolon or a
newline character.
Expressions
Expressions consist of operands and operators.
Operands are names or arbitrarily long numbers with optional sign and
decimal point.
Names
L simple variables
L function names
L[E] array elements
ibase base (radix) for input numbers, default: 10
obase base (radix) for output numbers, default: 10
Page 3 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
scale number of fractional digits, default: 0
If arrays are used as function arguments or defined as automatic vari-
ables, empty square brackets must follow the array name.
The same name may be used simultaneously for an array, a function, and
a simple variable. All variables are global to a bc program.
Other operands
(E) result of E
sqrt(E) square root of E
length(E) number of significant decimal digits in E
scale(E) number of fractional digits in E
L(E, ...,E)
Operators
+ - * / addition, subtraction, multiplication, division
^ power operator
% remainder of integer division (can now also be applied to
floating-point numbers.
++ -- increment and decrement operators, which can be applied to
names in prefix or postfix notation
< <= == >= > !=
relational operators (less than, less than or equal to,
equal to, greater than or equal to, greater than, not
equal to)
= assignment operator
=@ compound assignment operators, where a=@b is the same as
a=a@b. @ can be any of the operators +, -, *, /, ^, or %.
The logical operators && and || are not recognized by the bc command.
Page 4 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
Function definitions
define L (L, ...,L) {
auto L, ...,L
S; ...;S
return (E)
}
Example:
define p(x) {
auto q
q = p * p
return (q)
}
Declaring the identifiers of a function as auto restricts their scope
to that function. All function arguments are passed by value.
Functions in the math library /usr/lib/lib.b
Definitions of the mathematical functions listed below are contained
in the library /usr/lib/lib.b. The functions can be accessed by cal-
ling bc with option -l.
s(x) sine
c(x) cosine
e(x) base e exponential function
l(x) natural logarithm
a(x) arctangent
j(n,x) n-th order Bessel function
x values in mathematical functions must be specified in absolute
radian measure.
If you have write permission for /usr/lib/lib.b, you can add defini-
tions of further functions and also modify or delete existing ones.
Defining bases for input and output numbers
With ibase and obase you can specify the base used for interpreting
input and output values (input and output number radix). The following
rules apply:
1. If you do not explicitly assign values to ibase and obase, input
numbers are interpreted as decimal and results are output in decimal.
Page 5 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
2. If you have already defined the input base with an ibase=n state-
ment, the number that you use to define the output base in an
obase=m statement must be in input base n as well.
Example:
The input base is to be 2, the output base 16:
$ bc
ibase=2
obase=10000
10100000/1010
10
Fractional digits
Each expression E in bc is associated with a specific number of frac-
tional digits. You can use scale to inspect or change this number and
the scale(E) function just to inspect it.
Example:
In the following example the value of operand a is divided by the
value of operand b with scale initially left unset: the result
contains no fractional digits. Then scale is assigned a value of
8: the result of the division is now correct to 8 places after
the decimal point.
Finally we inspect the number of fractional digits in the result
and the value of scale.
$ bc
a=15.0
b=7.8
a/b
1
scale=8
a/b
1.92307692
scale(a/b)
8
scale
8
If you join two expressions using an operator, the number of frac-
tional digits associated with the result is governed by a rule
specific to the operator you use. The rules for bc operators are
described below. Various symbols are used in the descriptions:
Page 6 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
a = first operand
b = second operand
R = number of fractional digits in the result of a calculation
A = scale(a)
B = scale(b)
-
++
-- The unary minus sign and the increment and decrement operators ++
and -- (in prefix and postfix notation) do not affect the number
of fractional digits.
Rule: scale(E) = scale(-E) = scale(--E) = scale(++E)...
Example:
a is assigned a value with three fractional digits. The query
function scale(a) here always returns 3, regardless of whether a
has a -, -- or ++ operator and regardless of the fact that scale
was previously assigned a different value:
$ bc
scale=1
a=1.123
scale(a)
3
scale(-a)
-3
scale(a++)
3
scale(--a)
3
+
- With the binary operators + and -, R is equal to the number of
fractional digits in the operand with the most fractional digits,
regardless of whether you have previously assigned scale some
other value.
Rule: R = max(A,B)
Example:
scale is assigned a value of 1. Operand a is assigned a value
with 2 fractional digits, b a value with three fractional digits.
Thus b has more fractional digits than a, and also more than
scale. The query function returns 3 for both operands, the
greater number in b taking precedence.
Page 7 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
$ bc
scale=1
a=0.12
b=0.123
scale(a+b)
3
scale(b-a)
3
* With multiplications, a value previously assigned to scale is
significant: bc first calculates max, which is the highest of the
values scale, A and B. It then forms the sum of A and B and com-
pares this value with max. R is then the lower (min) of these two
values.
Rule: R = min(A+B, max(scale, A, B))
Example:
scale has a value of 9, A and B are both 1. Thus the highest of
the three values is 9. The sum of the number of fractional digits
in the two operands is 2. The number of fractional digits in the
result of the multiplication is the lower from the comparison of
max and this sum, which is 2.
$ bc
scale=9
a=0.1
b=0.1
scale(a*b)
2
/ With divisions, the precision of the result is equal to the value
of scale:
Rule: R = scale
Example:
scale is first given a value of 8. Then the operands are assigned
integer values. The result is also an integer. In spite of that,
the query function returns 8, and the result is shown correct to
8 places after the decimal point.
$ bc
scale=8
a=16
b=4
scale(a/b)
8
a/b
4.00000000
Page 8 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
^ With the power operator, R is formed as follows:
- If the integer exponent e is equal to or greater than 0:
bc takes the higher (max) of the two values scale and A. It
then multiplies A by the absolute value m of the exponent,
compares the result with max, and takes the lower of the two
values.
Rule: R = min(A*m, max(scale, A))
- If the integer exponent e is less than 0:
The precision of the result is equal to the value of scale.
Rule: R = scale
Example 1
scale is given a value of 7. a has one fractional digit, and the
absolute value of exponent e is 4, i.e. greater than 0. The
higher value from the comparison of scale and a is 7. However,
the result of multiplying a and m is 4. Thus the number of frac-
tional digits after exponentiation is 4:
$ bc
scale=7
a=3.1
e=4
scale(a^e)
4
a^e
92.3512
Example 2
However, if you set the exponent e to -4, the number of frac-
tional digits is equal to the value of scale:
$ bc
scale=7
a=3.1
e=-4
scale(a^e)
7
a^e
.0108281
Page 9 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
=
=@ With the assignment operators, the value of R is equal to that of
A after assignment. For a given compound operator =@, the rule
for calculating the number of fractional digits is the same as
for the corresponding simple operator @.
Rules: R = scale(b) and R = scale(a@b)
Example 1:
a is assigned a number with one fractional digit, b a number with
two fractional digits: scale(a) is 1 and scale(b) is 2. If you
inspect scale(a) after assigning the operands, bc returns a value
of 2, which is scale(b). If you then inspect a again, the value
returned is the value assigned to b:
$ bc
a=0.1
b=0.12
scale(a)
1
scale(b)
2
a=b
scale(a)
2
a
.12
Example 2
a is assigned a number with two fractional digits, b a number
with three. In the function call, a is assigned the value result-
ing from adding the two operands. bc sees the number of frac-
tional digits in the result of an addition as being equal to the
number of fractional digits in the operand with the most frac-
tional digits. After assignment a has the same number of digits,
i.e. three:
$ bc
a=0.12
a=0.123
scale(a)
2
scale(b)
3
a=+b
scale(a)
3
a
.243
Page 10 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
% With remaindering, if the value of scale is non-zero, the result
is computed as follows:
a%b = a - (a / b) * b
First the division is performed using the precision of scale. The
precision of the multiplication by b is equal to:
scale + B
In other words the multiplication is performed with full preci-
sion. Thus here the % operator can be used as a measure of the
precision with which the division is performed.
Finally R is whichever is the higher of A and (scale + B).
Rule: R = max((scale + B), A)
Example:
scale has a value of 4, a and b each have one fractional digit.
The result of remaindering has five fractional digits:
$ bc
scale=4
a=1.2
b=1.1
scale(a%b)
5
a%b
.00001
EXIT STATUS
0 All input files were processed successfully
non-zero value
An error occurred during execution of bc
LOCALE
The LCMESSAGES environment variable governs the language in which
message texts are displayed. If LCMESSAGES is undefined or is defined
as the null string, it defaults to the value of LANG. If LANG is like-
wise undefined or null, the system acts as if it were not internation-
alized.
The LCALL environment variable governs the entire locale. LCALL
takes precedence over all the other environment variables which affect
internationalization.
Page 11 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
EXAMPLES
Example 1
Addition, subtraction, multiplication, and division of numbers. Non-
integer results are to have 2 digits after the decimal point.
$ bc
scale=2
3+7
10
8-15
-7
7*6
42
3/5
.60
quit
$
Example 2
Defining a function to compute an approximate value of the exponential
function.
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
}
}
The termination criterion of the for loop is contained in the body of
the loop [if(c==0)] and is not given by the second expression of the
for statement as is usually the case. In a C program, the second
expression of the for statement would simply be omitted; but in a bc
program the for statement must always have 3 expressions. The expres-
sion 1==1, which is always true, has been inserted for this reason.
Page 12 Reliant UNIX 5.44 Printed 11/98
bc(1) bc(1)
Example 3
Printing approximate values of the exponential function of the first
ten integers.
for(i=1;i<=10;i++) e(i)
Example 4
Printing the squares of the integers 1 to 4:
$ bc
for(i=1;i<5;i++) {i*i}
1
4
9
16
quit
Example 5
Printing the input to dc for calculating the squares of the integers 1
to 4.
Input:
$ bc -c
for(i=1;i<5;i++) {i*i}
Output:
[lili*ps.lid1+sis.li 5>0]s0
1dsis.li 5>0
FILES
/usr/lib/lib.b
Math library
/usr/bin/dc
Desk calculator
SEE ALSO
dc(1).
Page 13 Reliant UNIX 5.44 Printed 11/98