home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / man / cat1 / bc.0 < prev    next >
Text File  |  1993-12-07  |  42KB  |  1,057 lines

  1.  
  2.  
  3.  
  4. bc(1)                                                       bc(1)
  5.  
  6.  
  7. NNAAMMEE
  8.        bc - An arbitrary precision calculator language
  9.  
  10. SSYYNNTTAAXX
  11.        bbcc [ --llwwss ] [  _f_i_l_e _._._. ]
  12.  
  13. VVEERRSSIIOONN
  14.        This man page documents GNU bc version 1.02.
  15.  
  16. DDEESSCCRRIIPPTTIIOONN
  17.        bbcc is a language that supports arbitrary precision numbers
  18.        with interactive execution of statements.  There are  some
  19.        similarities  in the syntax to the C programming language.
  20.        A standard math  library  is  available  by  command  line
  21.        option.   If requested, the math library is defined before
  22.        processing any files.  bbcc starts by processing  code  from
  23.        all  the  files  listed  on  the command line in the order
  24.        listed.  After all files have  been  processed,  bbcc  reads
  25.        from  the  standard  input.  All code is executed as it is
  26.        read.  (If a file contains a command to halt  the  proces-
  27.        sor, bbcc will never read from the standard input.)
  28.  
  29.        This version of bbcc contains several extensions beyond tra-
  30.        ditional bbcc implementations and the POSIX draft  standard.
  31.        Command line options can cause these extensions to print a
  32.        warning or to be rejected.  This  document  describes  the
  33.        language  accepted  by this processor.  Extensions will be
  34.        identified as such.
  35.  
  36.    OOPPTTIIOONNSS
  37.        -l     Define the standard math library.
  38.  
  39.        -w     Give warnings for extensions to POSIX bbcc.
  40.  
  41.        -s     Process exactly the POSIX bbcc language.
  42.  
  43.    NNUUMMBBEERRSS
  44.        The most basic element in bbcc is the number.   Numbers  are
  45.        arbitrary  precision  numbers.   This precision is both in
  46.        the integer part and the fractional part.  All numbers are
  47.        represented  internally  in decimal and all computation is
  48.        done in decimal.  (This  version  truncates  results  from
  49.        divide and multiply operations.)  There are two attributes
  50.        of numbers, the length and the scale.  The length  is  the
  51.        total number of significant decimal digits in a number and
  52.        the scale is the total number of decimal digits after  the
  53.        decimal point.  For example:
  54.                .000001 has a length of 6 and scale of 6.
  55.                1935.000 has a length of 7 and a scale of 3.
  56.  
  57.    VVAARRIIAABBLLEESS
  58.        Numbers are stored in two types of variables, simple vari-
  59.        ables and arrays.  Both simple variables and  array  vari-
  60.        ables  are  named.   Names begin with a letter followed by
  61.  
  62.  
  63.  
  64.                                 .                               1
  65.  
  66.  
  67.  
  68.  
  69.  
  70. bc(1)                                                       bc(1)
  71.  
  72.  
  73.        any number of letters, digits and underscores.   All  let-
  74.        ters must be lower case.  (Full alpha-numeric names are an
  75.        extension. In POSIX bbcc all names are a single  lower  case
  76.        letter.)   The  type  of  variable is clear by the context
  77.        because all array  variable  names  will  be  followed  by
  78.        brackets ([]).
  79.  
  80.        There are four special variables, ssccaallee,, iibbaassee,, oobbaassee,, and
  81.        llaasstt.  ssccaallee defines how some operations use digits  after
  82.        the decimal point.  The default value of ssccaallee is 0. iibbaassee
  83.        and oobbaassee define the conversion base for input and  output
  84.        numbers.   The  default  for both input and output is base
  85.        10.  llaasstt (an extension) is a variable that has the  value
  86.        of  the  last  printed number.  These will be discussed in
  87.        further detail where appropriate.  All of these  variables
  88.        may  have  values  assigned  to  them  as  well as used in
  89.        expressions.
  90.  
  91.    CCOOMMMMEENNTTSS
  92.        Comments in bbcc start with the characters //** and  end  with
  93.        the characters **//.  Comments may start anywhere and appear
  94.        as a single space in the input.  (This causes comments  to
  95.        delimit other input items.  For example, a comment can not
  96.        be found in the middle  of  a  variable  name.)   Comments
  97.        include  any  newlines (end of line) between the start and
  98.        the end of the comment.
  99.  
  100.    EEXXPPRREESSSSIIOONNSS
  101.        The numbers are manipulated by expressions and statements.
  102.        Since  the language was designed to be interactive, state-
  103.        ments and expressions are executed as  soon  as  possible.
  104.        There  is no "main" program.  Instead, code is executed as
  105.        it is encountered.  (Functions, discussed in detail later,
  106.        are defined when encountered.)
  107.  
  108.        A  simple  expression is just a constant. bbcc converts con-
  109.        stants into internal decimal  numbers  using  the  current
  110.        input  base, specified by the variable iibbaassee. (There is an
  111.        exception in functions.)  The legal values of iibbaassee are  2
  112.        through  16  (F).  Assigning a value outside this range to
  113.        iibbaassee will result in a value of 2 or  16.   Input  numbers
  114.        may  contain  the characters 0-9 and A-F. (Note: They must
  115.        be capitals.  Lower  case  letters  are  variable  names.)
  116.        Single  digit  numbers  always have the value of the digit
  117.        regardless of the value of iibbaassee.  (i.e.  A  =  10.)   For
  118.        multi-digit  numbers,  bbcc changes all input digits greater
  119.        or equal to ibase to the value of iibbaassee-1.  This makes the
  120.        number  FFFFFF  always  be  the largest 3 digit number of the
  121.        input base.
  122.  
  123.        Full expressions are similar to many other high level lan-
  124.        guages.  Since there is only one kind of number, there are
  125.        no rules for mixing types.  Instead, there  are  rules  on
  126.        the  scale  of expressions.  Every expression has a scale.
  127.  
  128.  
  129.  
  130.                                 .                               2
  131.  
  132.  
  133.  
  134.  
  135.  
  136. bc(1)                                                       bc(1)
  137.  
  138.  
  139.        This is derived from the scale of  original  numbers,  the
  140.        operation  performed  and  in many cases, the value of the
  141.        variable ssccaallee. Legal values of the variable ssccaallee  are  0
  142.        to the maximum number representable by a C integer.
  143.  
  144.        In the following descriptions of legal expressions, "expr"
  145.        refers to a complete expression and "var" refers to a sim-
  146.        ple or an array variable.  A simple variable is just a
  147.               _n_a_m_e
  148.        and an array variable is specified as
  149.               _n_a_m_e[_e_x_p_r]
  150.        Unless  specifically  mentioned the scale of the result is
  151.        the maximum scale of the expressions involved.
  152.  
  153.        - expr The result is the negation of the expression.
  154.  
  155.        ++ var The variable is incremented  by  one  and  the  new
  156.               value is the result of the expression.
  157.  
  158.        -- var The  variable  is  decremented  by  one and the new
  159.               value is the result of the expression.
  160.  
  161.        var ++  The result of the expression is the value  of  the
  162.               variable  and  then  the variable is incremented by
  163.               one.
  164.  
  165.        var -- The result of the expression is the  value  of  the
  166.               variable  and  then  the variable is decremented by
  167.               one.
  168.  
  169.        expr + expr
  170.               The result of the expression is the sum of the  two
  171.               expressions.
  172.  
  173.        expr - expr
  174.               The  result  of the expression is the difference of
  175.               the two expressions.
  176.  
  177.        expr * expr
  178.               The result of the expression is the product of  the
  179.               two expressions.
  180.  
  181.        expr / expr
  182.               The result of the expression is the quotient of the
  183.               two expressions.  The scale of the  result  is  the
  184.               value of the variable ssccaallee.
  185.  
  186.        expr % expr
  187.               The result of the expression is the "remainder" and
  188.               it is computed in the following  way.   To  compute
  189.               a%b,  first  a/b is computed to ssccaallee digits.  That
  190.               result is used to compute a-(