Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ bc(1) — AIX/RT 2.2.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

dc

bc

PURPOSE

     Provides  an interpreter  for arbitrary-precision  arith-
     metic language.

SYNOPSIS
     bc [ -c ] [ -l ] [ file ... ]


DESCRIPTION

     The bc  command is  an interactive process  that provides
     unlimited precision arithmetic.  It is a preprocessor for
     the dc command.  bc  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.

     The following description of syntax  for bc uses the fol-
     lowing  abbreviations:   L  means letters  a-z;  E  means
     expressions; S means statements.

       Names

         Simple variables: L
         Array elements: L[E]
         The words ibase, obase, and scale.
         Comments are enclosed in /* and */.

       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 to the right of the 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
              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.

     The value  of a statement  that is an expression  is dis-
     played  unless the  main  operator is  an assignment.   A
     semicolon  or  new-line character  separates  statements.
     Assignments  to  scale  controls the  number  of  decimal
     places printed on output  and maintained during multipli-
     cation,  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 parame-
     ters, or define them as automatic variables, empty square
     brackets must follow the array name.

     All for statements must have all three E's.

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

FLAGS

     -c Compiles file, but does not invoke dc.
     -l Includes a library of math functions.

EXAMPLES

     1.  To use bc as a calculator:

            You:
              bc
              1/4
         System:
              0
            You:
              scale = 1  /* Keep 1 decimal place  */
              1/4
         System:
              0.2
            You:
              scale = 3  /* Keep 3 decimal places */
              1/4
         System:
              0.250
            You:
              16+63/5
         System:
              28.600

            You:
              (16+63)/5
         System:
              15.800
            You:
              71/6
         System:
              11.833
            You:
              1/6
         System:
              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 the Enter key, except for assignments.

         When you enter bc  expressions directly from the key-
         board,  press END  OF  FILE (Ctrl-D)  to  end the  bc
         session and return to the shell command line.
     2.  To convert numbers from one base to another:

            You:
              bc
              obase = 16    /* Display numbers in Hexadecimal */
              ibase = 8     /* Input numbers in Octal         */
              12
         System:
              A
            You:
              123
         System:
              53
            You:
              123456
         System:
              A72E

         When you enter bc  expressions directly from the key-
         board,  press END  OF  FILE (Ctrl-D)  to  end the  bc
         session and return to the shell command line.
     3.  To write and run C-like programs:

            You:
              bc -l prog.bc
              e(2)    /*  e squared   */
         System:
              7.38905609893065022723
            You:
              f(5)    /*  5 factorial */
         System:
              120
            You:
              f(10)   /* 10 factorial */
         System:
              3628800

         This interprets  the bc  program saved  in "prog.bc",
         then reads  more bc statements from  the work station
         keyboard.   Starting bc  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:

           /* 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.   When  you   enter  bc
         expressions directly from the  keyboard, press END OF
         FILE (Ctrl-D) to end the bc session and return to the
         shell command line.
     4.  To  convert an  infix  expression  to reverse  polish
         notation (RPN):

            You:
              bc -c
              (a * b) % (3 + 4 * c)
         System:
              lalb* 3 4lc*+%ps.

         This compiles  the bc infix-notation  expression into
         one that the dc  command can interpret.  dc evaluates
         extended  RPN expressions.   In the  compiled output,
         the l (ell) before each  variable name is the dc sub-
         command 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.  For  more details,
         see "Redirection of Input and Output." When you enter
         bc expressions directly from  the keyboard, press END
         OF FILE (Ctrl-D) to end  the bc session and return to
         the shell command line.

FILES

     /usr/lib/lib.b    Mathematical library.
     /usr/bin/dc       Desk calculator proper.

RELATED INFORMATION

     The following command:  "dc."

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