Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

dc(1)

bc(1)  —  Commands

NAME

bc − Provides a processor for arbitrary-precision arithmetic language

SYNOPSIS

bc [-cl] [file ...]

The bc command is an interactive program that provides unlimited precision arithmetic.  It is a preprocessor for the dc command. 

FLAGS

-cCompiles file, but does not invoke dc. 

-lIncludes a library of mathematical functions specified by file.  Also sets the number of digits retained after the decimal point (the scale) to 20; by default the scale is 0. 

DESCRIPTION

The bc command invokes dc automatically, unless the -c (compile only) flag is specified.  If the -c flag is specified, the output from bc 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).  The command also has a scaling provision for decimal point notation.  The syntax for bc is similar to that of the C language. 

The bc command takes input first from the specified file.  When bc reaches the end of the input file, it reads standard input. 

In the following description of syntax for bc, letter means one of the letters a-z. 

Comments

Comments are enclosed in /∗ and ∗/. 

Names

Simple variables: letter

Array elements: letter[expression]

The words ibase, obase, and scale

Comments are enclosed in /∗ and ∗/

Other Operands

Arbitrarily long numbers with optional sign and decimal point. 

(expression)

sqrt (expression)

length (expression)Number of significant decimal digits

scale (expression)Number of digits to right of decimal point

letter (expression,...,expression)

Operators

+  -  ∗  /  %  ^  (% is remainder; ^ is power)

++  -- (prefix and suffix; apply to names)

==  <=  >=  !=  <>

=  =+  =-  =∗  =/  =%  ^=

+=  -=  ∗=  /=  %=

Statements

expression

{statement;...;statement}

if (expression) statement

while (expression)  statement

for (expression;expression;expression) statement

(null statement)

break

quit

Function Definitions

define letter ( letter,...,letter ) {
auto letter,...,letter
statement;...statement
return ( expression )
}

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

General Syntax

All function parameters are passed by value. 

The value of a statement that is an expression is displayed, unless the main operator is an assignment.  A semicolon or newline 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.  Automatic variables are pushed down during function calls.  When you use arrays as function parameters, or define them as automatic variables, empty brackets must follow the array name. 

All for statements must have all three expressions. 

The quit statement is interpreted when read, not when executed. 

EXAMPLES

When you enter bc expressions directly from the keyboard, press the End-of-File key sequence to end the bc session and return to the shell command line. 

To use bc as a calculator, proceed as follows:

Enter:

$ bc
1/4

The system responds as follows:

0

Enter:

scale = 1  /∗ Keep 1 decimal place  ∗/
1/4

The system responds as follows:

0.2

Enter:

scale = 3  /∗ Keep 3 decimal places ∗/
1/4

The system responds as follows:

0.250

Enter:

16+63/5

The system responds as follows:

28.600

Enter:

(16+63)/5

The system responds as follows:

15.800

Enter:

71/6

The system responds as follows:

11.833

Enter:

1/6

The system responds as follows:

0.166

You may type the comments (enclosed in /∗ ∗/), but they are provided only for your information.  The bc command displays the value of each expression when you press <Return>, except for assignments. 

To convert numbers from one base to another, proceed as follows:

Enter:

bc
obase = 16/∗ Display numbers in Hexadecimal ∗/
ibase = 8/∗ Input numbers in Octal∗/
12

The system responds as follows:

A

Enter:

123

The system responds as follows:

53

Enter:

123456

The system responds as follows:

A72E

To write and run C-like programs, proceed as follows:

Create the following file prog.bc:

/∗ compute the factorial of n ∗/
  define f(n) {
auto i, r;
r = 1;
for (i=2; i<=n; i++) r =∗ i;
return (r);
  }

Enter:

bc -l prog.bc

This interprets the bc program saved in prog.bc, then reads more bc command statements from standard input (the 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 prog.bc. 

Enter:

e(2)/∗  e squared∗/

The system responds as follows:

7.38905609893065022723

Enter:

f(5)/∗  5 factorial ∗/

The system responds as follows:

120

Enter:

f(10)/∗ 10 factorial ∗/

The system responds as follows:

3628800

The statement following a for or while statement must begin on the same line. 

To convert an infix expression to Reverse Polish Notation (RPN), enter:

Enter:

bc -c
(a ∗ b) % (3 + 4 ∗ c)

The system responds as follows:

lalb∗ 3 4lc∗+%ps.

This compiles the bc infix-notation expression into one that the dc command can interpret.  The dc command evaluates extended RPN expressions.  In the compiled output, the lowercase l before each variable name is the dc subcommand to load 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 dc to evaluate later by redirecting the standard output of this command. 

FILES

/usr/share/lib/lib.bMathematical library. 

/usr/bin/dcDesk calculator proper; uses bc as preprocessor. 

RELATED INFORMATION

Commands:  dc(1). 

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026