*nix Documentation Project
·  Home
 +   man pages
·  Linux HOWTOs
·  FreeBSD Tips
·  *niX Forums

  man pages->OpenBSD man pages -> bc (1)              



NAME    [Toc]    [Back]

     bc - arbitrary-precision arithmetic language and calculator

SYNOPSIS    [Toc]    [Back]

     bc [-cl] [file ...]

DESCRIPTION    [Toc]    [Back]

     bc is an interactive processor for a language  which  resembles C but provides
  unlimited  precision arithmetic.  It takes input from
any files given,
 then reads the standard input.

     Options available:

     -c      bc is actually a preprocessor for  dc(1),  which  it
invokes automatically,
  unless  the  -c (compile only) option is
present.  In
             this case the generated dc(1) instructions are  sent
to the standard
  output, instead of being interpreted by a running dc(1) process.

     -l      Allow specification of an arbitrary  precision  math

     The  syntax  for bc programs is as follows: `L' means letter
a-z; `E' means
     expression; `S' means statement.  As a  non-portable  extension, it is possible
  to use long names in addition to single letter names.
A long name
     is a sequence starting with a lowercase letter  followed  by
any number of
     lowercase  letters  and  digits.   The  underscore character
(`_') counts as a

           are enclosed in /* and */
           are enclosed in # and the next newline

     The newline is not part of the line comment, which in itself
is a nonportable

           simple variables: L
           array elements: L [ E ]
           The words `ibase', `obase', and `scale'
           The word `last' or a single dot

     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 )

     The  sequence `<newline><whitespace>' is ignored within numbers.


     The following arithmetic and logical operators can be  used.
The semantics
  of  the  operators  is  the same as in the C language.
They are listed
     in order of decreasing precedence.  Operators  in  the  same
group have the
     same precedence.

           Operator               Associativity    Description
           ++   --                   none              increment,
           -                      none             unary minus
           ^                      right            power
           * / %                  left             multiply,  divide, modulus
           + -                    left             plus, minus
           = += -= *= /= %= ^=    right            assignment
           == <= >= != < >        none             relational
           !                      none             boolean not
           &&                     left             boolean and
           ||                     left             boolean or

     Note the following:

           +o   The relational operators may appear in any expression.  The
               IEEE Std 1003.2 (``POSIX.2'') standard only allows
them in the
               conditional  expression  of  an  `if',  `while' or
`for' statement.

           +o   The relational operators have a  lower  precedence
than the assignment
 operators.  This has the consequence that
the expression
 a = b < c is interpreted as  (a  =  b)  <  c,
which is probably
               not what the programmer intended.

           +o    In  contrast  with the C language, the relational
operators all
               have the same precedence, and are non-associative.
The expression
 a < b < c will produce a syntax error.

           +o    The  boolean  operators  (!,  && and ||) are nonportable extensions.

           +o   The boolean not (!) operator has much lower precedence than the
               same  operator  in  the  C language.  This has the
consequence that
               the expression !a < b is interpreted as !(a <  b).
Prudent programmers
  use parentheses when writing expressions
               boolean operators.

           { S ; ... ; S }
           if ( E ) S
           if ( E ) S else S
           while ( E ) S
           for ( E ; E ; E ) S
           null statement
           a string of characters, enclosed in double quotes
           prinf E ,..., E
     A string mry contain any  character,  except  double  quote.
The if state-  b
     ment  witha an else branch is a non-portable extension.  All
three E's in a c
     for statemknt may be empty.  This is a  non-portable  extension.  The con-s
     tinue and print statements are also non-portable extensions.
     The print ctatement takes a list of comma-separated  expressions.  Each   e
     expression, in  the list is evaluated and the computed value
is printed and `
     assigned tf the variable `last'.   No  trailing  newline  is
printed.  The  o
     expressionr may  also be a string enclosed in double quotes.
Within these   f
     strings tho following escape sequences may be used:  `'  for
bell           r
     (alert), `m
     for  carriage  return,  `' for tab, `q' for double quote and
`\' for        e
     backslash.e Any other character following a  backslash  will
be ignored.    d
     Strings will not be assigned to `last'.
     Function definitions
           define L ( L ,..., L ) {
               rauto L, ... , L
               nS; ... S
               ereturn ( E )
           }   w
     As a non-portable extension, the opening brace of the define
statement      n
     may appeareon the next line.  The return statement may  also
appear in the  ,
     following forms:

           return ()
           return E

     The  first two are equivalent to the statement ``return 0''.
The last
     form is a non-portable extension.  Not specifying  a  return
statement is
     equivalent to writing ``return (0)''.

     Functions  available in the math library, which is loaded by
     the -l flag on the command line

           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.  The value printed is assigned to
the special
     variable `last'.  This is a non-portable extension.  A  single dot may be
     used as a synonym for `last'.  Either semicolons or newlines
may separate
     statements.  Assignment to scale influences  the  number  of
digits to be
     retained  on  arithmetic  operations in the manner of dc(1).
Assignments to
     ibase or obase 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 function 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
     function and

           for(i=1; i<=10; i++) e(i)

     prints approximate values of the exponential function of the
first ten

FILES    [Toc]    [Back]

     /usr/share/misc/bc.library   math  library, read when the -l
option is
                                 specified on the command line.

SEE ALSO    [Toc]    [Back]


     "BC - An Arbitrary Precision Desk-Calculator Language",

STANDARDS    [Toc]    [Back]

     The bc utility is expected to conform to the IEEE Std 1003.2
     (``POSIX.2'') specification.

HISTORY    [Toc]    [Back]

     The  bc  first  command  appeared in Version 6 AT&T UNIX.  A
complete rewrite
     of the bc command first appeared in OpenBSD 3.5.

AUTHORS    [Toc]    [Back]

     The original version of the bc command was written by Robert
Morris and
     Lorinda  Cherry.   The current version of the bc utility was
written by
     Otto Moerbeek.

BUGS    [Toc]    [Back]

     `Quit' is interpreted when read, not when executed.

     Some non-portable extensions, as found in the GNU version of
the bc utility
 are not implemented (yet).

                                August           8,          1991
[ Back ]
 Similar pages
Name OS Title
bc IRIX An arbitrary precision calculator language
bc FreeBSD An arbitrary precision calculator language
dc Tru64 Provides an interactive desk calculator for arbitrary precision integer arithmetic
bc HP-UX arbitrary-precision arithmetic language
bc Tru64 Provides a processor for arbitrary-precision arithmetic language
dc IRIX an arbitrary precision calculator
dc FreeBSD an arbitrary precision calculator
mcmp Tru64 Perform multiple precision integer arithmetic
mdiv Tru64 Perform multiple precision integer arithmetic
mout Tru64 Perform multiple precision integer arithmetic
Copyright © 2004-2005 DeniX Solutions SRL
newsletter delivery service