BC(1,C) AIX Commands Reference BC(1,C)
-------------------------------------------------------------------------------
bc
PURPOSE
Provides an interpreter for arbitrary-precision arithmetic language.
SYNTAX
+--------+ +--------+
bc ---| one of |---| |---|
| +----+ | | |
+-| -c |-+ +- file -+
| -l | ^ |
+----+ +------+
DESCRIPTION
The bc command is an interactive process that provides unlimited precision
arithmetic. It is a preprocessor for the dc command. The bc command invokes
the dc command automatically, unless the -c (compile only) flag is specified.
If the -c flag is specified, the output from the bc command goes to the
standard output.
The bc command lets you specify an input and output base in decimal, octal, or
hexadecimal (the default is decimal) notation. The command also has a scaling
provision for decimal point notation. The syntax for the bc command is similar
to that of the C language.
The bc command takes input first from the specified file. When the command
reaches the end of the input file, it reads standard input.
The following description of syntax for the bc command uses the following
abbreviations: L stands for the letters a through z; E for expressions; and S
for statements.
When you enter bc expressions directly from the keyboard, press the END OF FILE
(Ctrl-D) key to end the bc session and return to the shell command line.
Names
Simple variables: L
Array elements: L[E]
The words ibase, obase, and scale.
Comments are enclosed in /* and */.
Other Operands
Processed November 8, 1990 BC(1,C) 1
BC(1,C) AIX Commands Reference BC(1,C)
Arbitrarily long numbers with optional sign and decimal point.
( E )
sqrt ( E )
length ( E ) number of significant decimal digits
scale ( E ) number of digits to the right of the decimal point
L ( E,...,E )
Operators
+ - * / % (remainder); ^ (power)
++ -- (prefix and postfix; apply to names)
== <= >= != <>
= = + =- =* =/ =% =^
Statements
E
{ S;...;S }
if (E) S
while ( E ) S
for (E;E;E) S
Note: All for statements must have all three E's.
(null statement)
break
Note: The quit statement is interpreted when read, not when executed.
quit
Function Definitions
define L ( L,...,L ) {
auto L,...,L
S;...S
return ( E )
}
Functions in -l Math Library
s(x) sine
c(x) cosine
e(x) exponential
l(x) log
a(x) arctangent
j(n,x) Bessel function
All function parameters are passed by value.
Processed November 8, 1990 BC(1,C) 2
BC(1,C) AIX Commands Reference BC(1,C)
The value of a statement that is an expression is displayed unless the main
operator is an assignment. A semicolon or new-line character separates
statements. Assignments to scale control the number of decimal places printed
on output and maintained during multiplication, division, and exponentiation.
Assignments to ibase or obase set the input and output number radix
respectively.
The same letter may refer to an array, a function, and a simple variable
simultaneously. All variables are global to the program. "Auto" variables are
pushed down during function calls. When you use arrays as function parameters
or define them as automatic variables, empty square brackets must follow the
array name.
FLAGS
-c Compiles file, but does not invoke the dc command.
-l Includes a library of math functions.
EXAMPLES
1. To use bc as a calculator:
Enter:
bc
1/4
The system responds:
0
Enter:
scale = 1 /* Keep 1 decimal place */
1/4
The system responds:
0.2
Enter:
scale = 3 /* Keep 3 decimal places */
1/4
The system responds:
0.250
Enter:
Processed November 8, 1990 BC(1,C) 3
BC(1,C) AIX Commands Reference BC(1,C)
16+63/5
The system responds:
28.600
Enter:
(16+63)/5
The system responds:
15.800
Enter:
71/6
The system responds:
11.833
Enter:
1/6
The system responds:
0.166
You can type comments, which are enclosed in "/* and */", but they are
provided only for your information. The bc command displays the value of
each expression, (except for assignments) when you press the Enter key.
2. To convert numbers from one base to another:
Enter:
bc
obase = 16 /* Display numbers in Hexadecimal */
ibase = 8 /* Input numbers in Octal */
12
The system responds:
A
Enter:
123
The system responds:
Processed November 8, 1990 BC(1,C) 4
BC(1,C) AIX Commands Reference BC(1,C)
53
Enter:
123456
The system responds:
A72E
3. To write and run C-like programs:
Enter:
bc -l prog.bc
e(2) /* e squared */
The system responds:
7.38905609893065022723
Enter:
f(5) /* 5 factorial */
The system responds:
120
Enter:
f(10) /* 10 factorial */
The system responds:
3628800
This interprets the bc program saved in "prog.bc", then reads more bc
statements from the work station keyboard. Starting the "bc" command with
the "-l" flag makes the math library available. This example uses the "e"
(exponential) function from the math library, and "f" is defined in the
program file "prog.bc" as:
Processed November 8, 1990 BC(1,C) 5
BC(1,C) AIX Commands Reference BC(1,C)
/* compute the factorial of n */
define f(n) {
auto i, r;
r = 1;
for (i=2; i<=n; i++) r =* i;
return (r);
}
The statement following a for or while statement must begin on the same
line.
4. To convert an infix expression to reverse polish notation (RPN):
Enter:
bc -c
(a * b) % (3 + 4 * c)
The system responds:
lalb* 3 4lc*+%ps.
In this example the bc infix-notation expression is compiled into one that
the dc command can interpret. The dc command evaluates extended RPN
expressions. In the compiled output, the "l" (ell) before each variable
name is the dc subcommand that loads the value of the variable onto the
stack. The "p" displays the value on top of the stack, and the "s."
discards the top value by storing it in register . (dot). You can save the
RPN expression in a file for the dc command to evaluate later by
redirecting the standard output of this command. For more details, see
"Redirection of Input and Output."
FILES
/usr/lib/lib.b Mathematical library.
/usr/bin/dc Desk calculator proper.
RELATED INFORMATION
See "dc."
Processed November 8, 1990 BC(1,C) 6