1bc(1)                       General Commands Manual                      bc(1)
2
3
4

NAME

6       bc - An arbitrary precision calculator language
7

SYNTAX

9       bc [ -hlwsqv ] [long-options] [  file ... ]
10

DESCRIPTION

12       bc  is a language that supports arbitrary precision numbers with inter‐
13       active execution of statements.  There are  some  similarities  in  the
14       syntax  to  the  C  programming  language.   A standard math library is
15       available by command line option.  If requested, the  math  library  is
16       defined before processing any files.  bc starts by processing code from
17       all the files listed on the command line in the  order  listed.   After
18       all  files  have been processed, bc reads from the standard input.  All
19       code is executed as it is read.  (If a file contains a command to  halt
20       the processor, bc will never read from the standard input.)
21
22       This  version  of  bc contains several extensions beyond traditional bc
23       implementations and the POSIX draft standard.  Command line options can
24       cause these extensions to print a warning or to be rejected.  This doc‐
25       ument describes the language accepted by  this  processor.   Extensions
26       will be identified as such.
27
28   OPTIONS
29       -h, --help
30              Print the usage and exit.
31
32       -i, --interactive
33              Force interactive mode.
34
35       -l, --mathlib
36              Define the standard math library.
37
38       -w, --warn
39              Give warnings for extensions to POSIX bc.
40
41       -s, --standard
42              Process exactly the POSIX bc language.
43
44       -q, --quiet
45              Do not print the normal GNU bc welcome.
46
47       -v, --version
48              Print the version number and copyright and quit.
49
50   NUMBERS
51       The most basic element in bc is the number.  Numbers are arbitrary pre‐
52       cision numbers.  This precision is both in the  integer  part  and  the
53       fractional part.  All numbers are represented internally in decimal and
54       all computation is done in decimal.  (This  version  truncates  results
55       from divide and multiply operations.)  There are two attributes of num‐
56       bers, the length and the scale.  The length is the total number of dec‐
57       imal digits used by bc to represent a number and the scale is the total
58       number of decimal digits after the decimal point.  For example:
59               .000001 has a length of 6 and scale of 6.
60               1935.000 has a length of 7 and a scale of 3.
61
62   VARIABLES
63       Numbers are stored in two types  of  variables,  simple  variables  and
64       arrays.   Both  simple  variables and array variables are named.  Names
65       begin with a letter followed by  any  number  of  letters,  digits  and
66       underscores.   All  letters  must  be  lower case.  (Full alpha-numeric
67       names are an extension. In POSIX bc all names are a single  lower  case
68       letter.)   The  type  of  variable  is clear by the context because all
69       array variable names will be followed by brackets ([]).
70
71       There are four special variables, scale, ibase, obase, and last.  scale
72       defines  how  some  operations use digits after the decimal point.  The
73       default value of scale is 0. ibase and obase define the conversion base
74       for input and output numbers.  The default for both input and output is
75       base 10.  last (an extension) is a variable that has the value  of  the
76       last  printed  number.  These will be discussed in further detail where
77       appropriate.  All of these variables may have values assigned  to  them
78       as well as used in expressions.
79
80   COMMENTS
81       Comments in bc start with the characters /* and end with the characters
82       */.  Comments may start anywhere and appear as a single  space  in  the
83       input.   (This causes comments to delimit other input items.  For exam‐
84       ple, a comment can not be found in the  middle  of  a  variable  name.)
85       Comments  include  any newlines (end of line) between the start and the
86       end of the comment.
87
88       To support the use of scripts for bc, a single line  comment  has  been
89       added  as  an extension.  A single line comment starts at a # character
90       and continues to the next end of the line.  The end of  line  character
91       is not part of the comment and is processed normally.
92
93   EXPRESSIONS
94       The  numbers  are manipulated by expressions and statements.  Since the
95       language was designed to be interactive, statements and expressions are
96       executed  as  soon  as possible.  There is no "main" program.  Instead,
97       code is executed as it is encountered.  (Functions, discussed in detail
98       later, are defined when encountered.)
99
100       A  simple  expression  is  just  a constant. bc converts constants into
101       internal decimal numbers using the current input base, specified by the
102       variable ibase. (There is an exception in functions.)  The legal values
103       of ibase are 2 through 36. (Bases greater than 16  are  an  extension.)
104       Assigning a value outside this range to ibase will result in a value of
105       2 or 36.  Input numbers may contain the characters 0-9 and A-Z.  (Note:
106       They must be capitals.  Lower case letters are variable names.)  Single
107       digit numbers always have the value of  the  digit  regardless  of  the
108       value of ibase. (i.e. A = 10.)  For multi-digit numbers, bc changes all
109       input digits greater or equal to ibase to the value of  ibase-1.   This
110       makes  the number ZZZ always be the largest 3 digit number of the input
111       base.
112
113       Full expressions are similar to many other high level languages.  Since
114       there  is only one kind of number, there are no rules for mixing types.
115       Instead, there are rules on the scale of expressions.  Every expression
116       has  a  scale.  This is derived from the scale of original numbers, the
117       operation performed and in many cases, the value of the variable scale.
118       Legal  values  of the variable scale are 0 to the maximum number repre‐
119       sentable by a C integer.
120
121       In the following descriptions of legal expressions, "expr" refers to  a
122       complete  expression and "var" refers to a simple or an array variable.
123       A simple variable is just a
124              name
125       and an array variable is specified as
126              name[expr]
127       Unless specifically mentioned the scale of the result  is  the  maximum
128       scale of the expressions involved.
129
130       - expr The result is the negation of the expression.
131
132       ++ var The  variable  is  incremented  by  one and the new value is the
133              result of the expression.
134
135       -- var The variable is decremented by one and  the  new  value  is  the
136              result of the expression.
137
138       var ++
139               The  result  of the expression is the value of the variable and
140              then the variable is incremented by one.
141
142       var -- The result of the expression is the value of  the  variable  and
143              then the variable is decremented by one.
144
145       expr + expr
146              The result of the expression is the sum of the two expressions.
147
148       expr - expr
149              The  result  of  the  expression  is  the  difference of the two
150              expressions.
151
152       expr * expr
153              The result of the expression is the product of the  two  expres‐
154              sions.   If  a and b are the scales of the two expressions, then
155              the scale of the result is: min(a+b,max(scale,a,b))
156
157       expr / expr
158              The result of the expression is the quotient of the two  expres‐
159              sions.   The  scale  of  the result is the value of the variable
160              scale.
161
162       expr % expr
163              The result of the expression is the "remainder" and it  is  com‐
164              puted  in  the following way.  To compute a%b, first a/b is com‐
165              puted to scale digits.  That result is used to compute a-(a/b)*b
166              to  the scale of the maximum of scale+scale(b) and scale(a).  If
167              scale is set to zero and  both  expressions  are  integers  this
168              expression is the integer remainder function.
169
170       expr ^ expr
171              The result of the expression is the value of the first raised to
172              the second. The second expression must be an integer.   (If  the
173              second  expression is not an integer, a warning is generated and
174              the expression is truncated to get an integer value.)  The scale
175              of  the  result  is  scale  if the exponent is negative.  If the
176              exponent is positive the scale of the result is the  minimum  of
177              the  scale  of the first expression times the value of the expo‐
178              nent and the maximum of scale and the scale of the first expres‐
179              sion.    (e.g.   scale(a^b)   =   min(scale(a)*b,   max(  scale,
180              scale(a))).)  It should be noted that expr^0 will always  return
181              the value of 1.
182
183       ( expr )
184              This  alters  the standard precedence to force the evaluation of
185              the expression.
186
187       var = expr
188              The variable is assigned the value of the expression.
189
190       var <op>= expr
191              This is equivalent to "var = var <op> expr" with  the  exception
192              that  the  "var"  part  is evaluated only once.  This can make a
193              difference if "var" is an array.
194
195       Relational expressions are a special kind  of  expression  that  always
196       evaluate to 0 or 1, 0 if the relation is false and 1 if the relation is
197       true.  These may appear in any legal expression.   (POSIX  bc  requires
198       that  relational expressions are used only in if, while, and for state‐
199       ments and that only one relational test may  be  done  in  them.)   The
200       relational operators are
201
202       expr1 < expr2
203              The result is 1 if expr1 is strictly less than expr2.
204
205       expr1 <= expr2
206              The result is 1 if expr1 is less than or equal to expr2.
207
208       expr1 > expr2
209              The result is 1 if expr1 is strictly greater than expr2.
210
211       expr1 >= expr2
212              The result is 1 if expr1 is greater than or equal to expr2.
213
214       expr1 == expr2
215              The result is 1 if expr1 is equal to expr2.
216
217       expr1 != expr2
218              The result is 1 if expr1 is not equal to expr2.
219
220       Boolean  operations  are  also  legal.  (POSIX bc does NOT have boolean
221       operations). The result of all boolean operations  are  0  and  1  (for
222       false  and  true)  as in relational expressions.  The boolean operators
223       are:
224
225       !expr  The result is 1 if expr is 0.
226
227       expr && expr
228              The result is 1 if both expressions are non-zero.
229
230       expr || expr
231              The result is 1 if either expression is non-zero.
232
233       The expression precedence is as follows: (lowest to highest)
234              || operator, left associative
235              && operator, left associative
236              ! operator, nonassociative
237              Relational operators, left associative
238              Assignment operator, right associative
239              + and - operators, left associative
240              *, / and % operators, left associative
241              ^ operator, right associative
242              unary - operator, nonassociative
243              ++ and -- operators, nonassociative
244
245       This precedence was chosen so that POSIX compliant bc programs will run
246       correctly. This will cause the use of the relational and logical opera‐
247       tors to have some unusual behavior when used  with  assignment  expres‐
248       sions.  Consider the expression:
249              a = 3 < 5
250
251       Most C programmers would assume this would assign the result of "3 < 5"
252       (the value 1) to the variable "a".  What this does in bc is assign  the
253       value 3 to the variable "a" and then compare 3 to 5.  It is best to use
254       parenthesis when  using  relational  and  logical  operators  with  the
255       assignment operators.
256
257       There  are  a  few  more  special  expressions that are provided in bc.
258       These have to do with user defined functions  and  standard  functions.
259       They  all  appear  as "name(parameters)".  See the section on functions
260       for user defined functions.  The standard functions are:
261
262       length ( expression )
263              The value of the length function is the  number  of  significant
264              digits in the expression.
265
266       read ( )
267              The  read  function  (an  extension) will read a number from the
268              standard  input,  regardless  of  where  the  function   occurs.
269              Beware, this can cause problems with the mixing of data and pro‐
270              gram in the standard input.  The best use for this  function  is
271              in  a previously written program that needs input from the user,
272              but never allows program code to be input from  the  user.   The
273              value  of the read function is the number read from the standard
274              input using the current value of the variable ibase for the con‐
275              version base.
276
277       scale ( expression )
278              The  value  of  the scale function is the number of digits after
279              the decimal point in the expression.
280
281       sqrt ( expression )
282              The value of the sqrt function is the square root of the expres‐
283              sion.  If the expression is negative, a run time error is gener‐
284              ated.
285
286   STATEMENTS
287       Statements (as in most algebraic languages) provide the  sequencing  of
288       expression  evaluation.  In bc statements are executed "as soon as pos‐
289       sible."  Execution happens when a newline in encountered and  there  is
290       one or more complete statements.  Due to this immediate execution, new‐
291       lines are very important in bc. In fact, both a semicolon and a newline
292       are  used  as  statement separators.  An improperly placed newline will
293       cause a syntax error.  Because newlines are statement separators, it is
294       possible  to  hide  a  newline  by  using the backslash character.  The
295       sequence "\<nl>", where <nl> is the newline appears to bc as whitespace
296       instead of a newline.  A statement list is a series of statements sepa‐
297       rated by semicolons and newlines.  The following is a list of bc state‐
298       ments  and what they do: (Things enclosed in brackets ([]) are optional
299       parts of the statement.)
300
301       expression
302              This statement does one of two things.  If the expression starts
303              with  "<variable>  <assignment>  ...", it is considered to be an
304              assignment statement.  If the expression is  not  an  assignment
305              statement,  the  expression is evaluated and printed to the out‐
306              put.  After the number is printed, a newline  is  printed.   For
307              example,  "a=1"  is  an  assignment  statement and "(a=1)" is an
308              expression that has an embedded assignment.   All  numbers  that
309              are  printed  are  printed in the base specified by the variable
310              obase. The legal values for obase  are  2  through  BC_BASE_MAX.
311              (See  the  section  LIMITS.)   For bases 2 through 16, the usual
312              method of writing numbers is used.  For bases greater  than  16,
313              bc  uses  a multi-character digit method of printing the numbers
314              where each higher base digit is printed as  a  base  10  number.
315              The  multi-character digits are separated by spaces.  Each digit
316              contains the number of characters required to represent the base
317              ten  value  of "obase-1".  Since numbers are of arbitrary preci‐
318              sion, some numbers may not be printable on a single output line.
319              These  long  numbers will be split across lines using the "\" as
320              the last character on a line.  The maximum number of  characters
321              printed  per  line  is 70.  Due to the interactive nature of bc,
322              printing a number  causes  the  side  effect  of  assigning  the
323              printed value to the special variable last. This allows the user
324              to recover the last value printed without having to  retype  the
325              expression  that printed the number.  Assigning to last is legal
326              and will overwrite the last  printed  value  with  the  assigned
327              value.  The newly assigned value will remain until the next num‐
328              ber is printed or another value  is  assigned  to  last.   (Some
329              installations  may allow the use of a single period (.) which is
330              not part of a number as a short hand notation for for last.)
331
332       string The string is printed to the output.  Strings start with a  dou‐
333              ble  quote  character  and contain all characters until the next
334              double quote character.   All  characters  are  take  literally,
335              including  any  newline.   No newline character is printed after
336              the string.
337
338       print list
339              The print statement (an extension) provides  another  method  of
340              output.   The  "list" is a list of strings and expressions sepa‐
341              rated by commas.  Each string or expression is  printed  in  the
342              order  of the list.  No terminating newline is printed.  Expres‐
343              sions are evaluated and their value is printed and  assigned  to
344              the variable last. Strings in the print statement are printed to
345              the output and may contain special characters.  Special  charac‐
346              ters  start with the backslash character (\).  The special char‐
347              acters  recognized  by  bc  are  "a"  (alert   or   bell),   "b"
348              (backspace),  "f"  (form  feed),  "n"  (newline),  "r" (carriage
349              return), "q" (double quote), "t"  (tab),  and  "\"  (backslash).
350              Any other character following the backslash will be ignored.
351
352       { statement_list }
353              This  is  the compound statement.  It allows multiple statements
354              to be grouped together for execution.
355
356       if ( expression ) statement1 [else statement2]
357              The if statement evaluates the expression  and  executes  state‐
358              ment1  or  statement2  depending on the value of the expression.
359              If the expression  is  non-zero,  statement1  is  executed.   If
360              statement2 is present and the value of the expression is 0, then
361              statement2 is executed.  (The else clause is an extension.)
362
363       while ( expression ) statement
364              The while statement will execute the statement while the expres‐
365              sion  is non-zero.  It evaluates the expression before each exe‐
366              cution of the statement.   Termination of the loop is caused  by
367              a zero expression value or the execution of a break statement.
368
369       for ( [expression1] ; [expression2] ; [expression3] ) statement
370              The  for statement controls repeated execution of the statement.
371              Expression1 is evaluated before the loop.  Expression2 is evalu‐
372              ated before each execution of the statement.  If it is non-zero,
373              the statement is evaluated.  If it is zero, the loop  is  termi‐
374              nated.   After  each  execution of the statement, expression3 is
375              evaluated before the reevaluation of  expression2.   If  expres‐
376              sion1  or  expression3  are missing, nothing is evaluated at the
377              point they would be evaluated.  If expression2 is missing, it is
378              the  same  as  substituting  the  value 1 for expression2.  (The
379              optional expressions are an extension.  POSIX  bc  requires  all
380              three  expressions.)   The  following is equivalent code for the
381              for statement:
382              expression1;
383              while (expression2) {
384                 statement;
385                 expression3;
386              }
387
388       break  This statement causes a forced exit of the most recent enclosing
389              while statement or for statement.
390
391       continue
392              The  continue  statement  (an extension)  causes the most recent
393              enclosing for statement to start the next iteration.
394
395       halt   The halt statement (an extension) is an executed statement  that
396              causes  the  bc processor to quit only when it is executed.  For
397              example, "if (0 == 1) halt"  will  not  cause  bc  to  terminate
398              because the halt is not executed.
399
400       return Return  the  value 0 from a function.  (See the section on func‐
401              tions.)
402
403       return ( expression )
404              Return the value of the expression from a  function.   (See  the
405              section on functions.)  As an extension, the parenthesis are not
406              required.
407
408   PSEUDO STATEMENTS
409       These statements are not statements in the traditional sense.  They are
410       not  executed  statements.   Their  function  is performed at "compile"
411       time.
412
413       limits Print the local limits enforced by  the  local  version  of  bc.
414              This is an extension.
415
416       quit   When the quit statement is read, the bc processor is terminated,
417              regardless of where the quit statement is found.   For  example,
418              "if (0 == 1) quit" will cause bc to terminate.
419
420       warranty
421              Print a longer warranty notice.  This is an extension.
422
423   FUNCTIONS
424       Functions  provide  a method of defining a computation that can be exe‐
425       cuted later.  Functions in bc always compute a value and return  it  to
426       the  caller.   Function  definitions  are "dynamic" in the sense that a
427       function is undefined until a definition is encountered in  the  input.
428       That  definition is then used until another definition function for the
429       same name is encountered.  The new definition then replaces  the  older
430       definition.  A function is defined as follows:
431              define name ( parameters ) { newline
432                  auto_list   statement_list }
433       A function call is just an expression of the form "name(parameters)".
434
435       Parameters are numbers or arrays (an extension).  In the function defi‐
436       nition, zero or more parameters are defined by listing their names sep‐
437       arated by commas.  All parameters are call by value parameters.  Arrays
438       are specified in the parameter definition  by  the  notation  "name[]".
439       In the function call, actual parameters are full expressions for number
440       parameters.  The same notation is used for passing arrays as for defin‐
441       ing  array parameters.  The named array is passed by value to the func‐
442       tion.  Since function definitions are dynamic,  parameter  numbers  and
443       types are checked when a function is called.  Any mismatch in number or
444       types of parameters will cause a runtime error.  A runtime  error  will
445       also occur for the call to an undefined function.
446
447       The  auto_list  is  an  optional list of variables that are for "local"
448       use.  The syntax of the auto list (if present) is "auto name,  ...  ;".
449       (The  semicolon  is  optional.)  Each name is the name of an auto vari‐
450       able.  Arrays may be specified by using the same notation  as  used  in
451       parameters.   These  variables have their values pushed onto a stack at
452       the start of the function.  The variables are then initialized to  zero
453       and  used  throughout the execution of the function.  At function exit,
454       these variables are popped so that the original value (at the  time  of
455       the function call) of these variables are restored.  The parameters are
456       really auto variables that are initialized to a value provided  in  the
457       function  call.   Auto  variables  are different than traditional local
458       variables because if function A calls function B, B may access function
459       A's  auto  variables by just using the same name, unless function B has
460       called them auto variables.  Due to the fact that  auto  variables  and
461       parameters are pushed onto a stack, bc supports recursive functions.
462
463       The  function  body  is a list of bc statements.  Again, statements are
464       separated by semicolons or newlines.  Return statements cause the  ter‐
465       mination  of  a function and the return of a value.  There are two ver‐
466       sions of the return statement.  The first form, "return",  returns  the
467       value  0 to the calling expression.  The second form, "return ( expres‐
468       sion )", computes the value of the expression and returns that value to
469       the calling expression.  There is an implied "return (0)" at the end of
470       every function.  This allows a function to terminate and return 0 with‐
471       out an explicit return statement.
472
473       Functions  also  change the usage of the variable ibase.  All constants
474       in the function body will be converted using the value of ibase at  the
475       time of the function call.  Changes of ibase will be ignored during the
476       execution of the function except for the standard function read,  which
477       will always use the current value of ibase for conversion of numbers.
478
479       Several  extensions have been added to functions.  First, the format of
480       the definition has been slightly relaxed.  The  standard  requires  the
481       opening  brace  be on the same line as the define keyword and all other
482       parts must be on following lines.  This version of bc  will  allow  any
483       number  of newlines before and after the opening brace of the function.
484       For example, the following definitions are legal.
485              define d (n) { return (2*n); }
486              define d (n)
487                { return (2*n); }
488
489       Functions may be defined as void.  A void funtion returns no value  and
490       thus  may not be used in any place that needs a value.  A void function
491       does not produce any output when called by itself  on  an  input  line.
492       The  key  word void is placed between the key word define and the func‐
493       tion name.  For example, consider the following session.
494              define py (y) { print "--->", y, "<---", "\n"; }
495              define void px (x) { print "--->", x, "<---", "\n"; }
496              py(1)
497              --->1<---
498              0
499              px(1)
500              --->1<---
501       Since py is not a void function, the call of py(1) prints  the  desired
502       output and then prints a second line that is the value of the function.
503       Since the value of a function that is  not  given  an  explicit  return
504       statement  is zero, the zero is printed.  For px(1), no zero is printed
505       because the function is a void function.
506
507       Also, call by variable for arrays was added.   To  declare  a  call  by
508       variable  array, the declaration of the array parameter in the function
509       definition looks like "*name[]".  The call to the function remains  the
510       same as call by value arrays.
511
512   MATH LIBRARY
513       If  bc  is  invoked with the -l option, a math library is preloaded and
514       the default scale is set to 20.   The  math  functions  will  calculate
515       their  results  to  the  scale set at the time of their call.  The math
516       library defines the following functions:
517
518       s (x)  The sine of x, x is in radians.
519
520       c (x)  The cosine of x, x is in radians.
521
522       a (x)  The arctangent of x, arctangent returns radians.
523
524       l (x)  The natural logarithm of x.
525
526       e (x)  The exponential function of raising e to the value x.
527
528       j (n,x)
529              The Bessel function of integer order n of x.
530
531   EXAMPLES
532       In /bin/sh,  the following will assign the value of "pi" to  the  shell
533       variable pi.
534               pi=$(echo "scale=10; 4*a(1)" | bc -l)
535
536       The following is the definition of the exponential function used in the
537       math library.  This function is written in POSIX bc.
538              scale = 20
539
540              /* Uses the fact that e^x = (e^(x/2))^2
541                 When x is small enough, we use the series:
542                   e^x = 1 + x + x^2/2! + x^3/3! + ...
543              */
544
545              define e(x) {
546                auto  a, d, e, f, i, m, v, z
547
548                /* Check the sign of x. */
549                if (x<0) {
550                  m = 1
551                  x = -x
552                }
553
554                /* Precondition x. */
555                z = scale;
556                scale = 4 + z + .44*x;
557                while (x > 1) {
558                  f += 1;
559                  x /= 2;
560                }
561
562                /* Initialize the variables. */
563                v = 1+x
564                a = x
565                d = 1
566
567                for (i=2; 1; i++) {
568                  e = (a *= x) / (d *= i)
569                  if (e == 0) {
570                    if (f>0) while (f--)  v = v*v;
571                    scale = z
572                    if (m) return (1/v);
573                    return (v/1);
574                  }
575                  v += e
576                }
577              }
578
579       The following is code that uses the extended features of bc  to  imple‐
580       ment a simple program for calculating checkbook balances.  This program
581       is best kept in a file so that it can be used many times without having
582       to retype it at every use.
583              scale=2
584              print "\nCheck book program!\n"
585              print "  Remember, deposits are negative transactions.\n"
586              print "  Exit by a 0 transaction.\n\n"
587
588              print "Initial balance? "; bal = read()
589              bal /= 1
590              print "\n"
591              while (1) {
592                "current balance = "; bal
593                "transaction? "; trans = read()
594                if (trans == 0) break;
595                bal -= trans
596                bal /= 1
597              }
598              quit
599
600       The following is the definition of the recursive factorial function.
601              define f (x) {
602                if (x <= 1) return (1);
603                return (f(x-1) * x);
604              }
605
606   READLINE AND LIBEDIT OPTIONS
607       GNU bc can be compiled (via a configure option) to use the GNU readline
608       input editor library or the BSD libedit library.  This allows the  user
609       to do editing of lines before sending them to bc.  It also allows for a
610       history of previous lines typed.  When this option is selected, bc  has
611       one  more special variable.  This special variable, history is the num‐
612       ber of lines of history retained.  For readline, a value  of  -1  means
613       that  an  unlimited  number of history lines are retained.  Setting the
614       value of history to a positive number restricts the number  of  history
615       lines  to  the  number given.  The value of 0 disables the history fea‐
616       ture.  The default value is 100. For more information,  read  the  user
617       manuals  for  the GNU readline, history and BSD libedit libraries.  One
618       can not enable both readline and libedit at the same time.
619
620   DIFFERENCES
621       This version of bc was implemented from the POSIX P1003.2/D11 draft and
622       contains  several  differences and extensions relative to the draft and
623       traditional implementations.  It is not implemented in the  traditional
624       way  using  dc(1).   This  version is a single process which parses and
625       runs a byte code translation of the  program.   There  is  an  "undocu‐
626       mented"  option (-c) that causes the program to output the byte code to
627       the standard output instead of running it.   It  was  mainly  used  for
628       debugging the parser and preparing the math library.
629
630       A  major  source  of  differences  is  extensions,  where  a feature is
631       extended to add more functionality and additions,  where  new  features
632       are added.  The following is the list of differences and extensions.
633
634       LANG environment
635              This  version does not conform to the POSIX standard in the pro‐
636              cessing of the LANG environment  variable  and  all  environment
637              variables starting with LC_.
638
639       names  Traditional and POSIX bc have single letter names for functions,
640              variables and arrays.  They have been extended to be multi-char‐
641              acter  names  that  start with a letter and may contain letters,
642              numbers and the underscore character.
643
644       Strings
645              Strings are not allowed to contain NUL characters.   POSIX  says
646              all characters must be included in strings.
647
648       last   POSIX bc does not have a last variable.  Some implementations of
649              bc use the period (.) in a similar way.
650
651       comparisons
652              POSIX bc allows comparisons only in the if statement, the  while
653              statement,  and  the  second  expression  of  the for statement.
654              Also, only one relational operation is allowed in each of  those
655              statements.
656
657       if statement, else clause
658              POSIX bc does not have an else clause.
659
660       for statement
661              POSIX  bc  requires  all  expressions  to  be present in the for
662              statement.
663
664       &&, ||, !
665              POSIX bc does not have the logical operators.
666
667       read function
668              POSIX bc does not have a read function.
669
670       print statement
671              POSIX bc does not have a print statement .
672
673       continue statement
674              POSIX bc does not have a continue statement.
675
676       return statement
677              POSIX bc requires parentheses around the return expression.
678
679       array parameters
680              POSIX bc does not (currently) support array parameters in  full.
681              The POSIX grammar allows for arrays in function definitions, but
682              does not provide a method to  specify  an  array  as  an  actual
683              parameter.   (This  is most likely an oversight in the grammar.)
684              Traditional implementations of bc have only call by value  array
685              parameters.
686
687       function format
688              POSIX  bc  requires  the  opening  brace on the same line as the
689              define key word and the auto statement on the next line.
690
691       =+, =-, =*, =/, =%, =^
692              POSIX bc does not require these "old style" assignment operators
693              to be defined.  This version may allow these "old style" assign‐
694              ments.  Use the limits statement to see if the installed version
695              supports  them.   If  it does support the "old style" assignment
696              operators, the statement "a =- 1" will decrement a by 1  instead
697              of setting a to the value -1.
698
699       spaces in numbers
700              Other  implementations of bc allow spaces in numbers.  For exam‐
701              ple, "x=1 3" would assign the value 13 to the variable  x.   The
702              same statement would cause a syntax error in this version of bc.
703
704       errors and execution
705              This  implementation  varies from other implementations in terms
706              of what code will be executed when syntax and other  errors  are
707              found  in the program.  If a syntax error is found in a function
708              definition, error recovery tries to  find  the  beginning  of  a
709              statement  and  continue  to  parse the function.  Once a syntax
710              error is found  in  the  function,  the  function  will  not  be
711              callable  and  becomes undefined.  Syntax errors in the interac‐
712              tive execution code will invalidate the current execution block.
713              The execution block is terminated by an end of line that appears
714              after a complete sequence of statements.  For example,
715              a = 1
716              b = 2
717       has two execution blocks and
718              { a = 1
719                b = 2 }
720       has one execution block.  Any runtime error will terminate  the  execu‐
721       tion of the current execution block.  A runtime warning will not termi‐
722       nate the current execution block.
723
724       Interrupts
725              During an interactive session, the SIGINT signal (usually gener‐
726              ated  by  the  control-C character from the terminal) will cause
727              execution of the current execution block to be interrupted.   It
728              will  display  a  "runtime"  error indicating which function was
729              interrupted.  After all runtime structures have been cleaned up,
730              a  message  will  be printed to notify the user that bc is ready
731              for more input.  All previously defined functions remain defined
732              and  the  value  of  all non-auto variables are the value at the
733              point of interruption.  All auto variables and function  parame‐
734              ters  are  removed  during  the clean up process.  During a non-
735              interactive session, the SIGINT signal will terminate the entire
736              run of bc.
737
738   LIMITS
739       The  following are the limits currently in place for this bc processor.
740       Some of them may have been changed by an installation.  Use the  limits
741       statement to see the actual values.
742
743       BC_BASE_MAX
744              The  maximum  output  base is currently set at 999.  The maximum
745              input base is 16.
746
747       BC_DIM_MAX
748              This is currently an arbitrary limit of  65535  as  distributed.
749              Your installation may be different.
750
751       BC_SCALE_MAX
752              The  number  of  digits  after  the  decimal point is limited to
753              INT_MAX digits.  Also, the number of digits before  the  decimal
754              point is limited to INT_MAX digits.
755
756       BC_STRING_MAX
757              The  limit  on  the  number of characters in a string is INT_MAX
758              characters.
759
760       exponent
761              The value of the exponent in the raise operation (^) is  limited
762              to LONG_MAX.
763
764       variable names
765              The  current  limit  on  the number of unique names is 32767 for
766              each of simple variables, arrays and functions.
767

ENVIRONMENT VARIABLES

769       The following environment variables are processed by bc:
770
771       POSIXLY_CORRECT
772              This is the same as the -s option.
773
774       BC_ENV_ARGS
775              This is another mechanism to get arguments to bc.  The format is
776              the  same  as  the  command line arguments.  These arguments are
777              processed first, so any files listed in  the  environment  argu‐
778              ments  are  processed  before  any  command line argument files.
779              This allows the user to set up "standard" options and  files  to
780              be  processed at every invocation of bc.  The files in the envi‐
781              ronment variables would typically contain  function  definitions
782              for functions the user wants defined every time bc is run.
783
784       BC_LINE_LENGTH
785              This should be an integer specifying the number of characters in
786              an output line for numbers. This includes the backslash and new‐
787              line characters for long numbers.  As an extension, the value of
788              zero disables the multi-line feature.  Any other value  of  this
789              variable that is less than 3 sets the line length to 70.
790

DIAGNOSTICS

792       If  any file on the command line can not be opened, bc will report that
793       the file is unavailable and terminate.  Also, there are compile and run
794       time diagnostics that should be self-explanatory.
795

BUGS

797       Error recovery is not very good yet.
798
799       Email  bug  reports  to  bug-bc@gnu.org.   Be  sure to include the word
800       ``bc'' somewhere in the ``Subject:'' field.
801

AUTHOR

803       Philip A. Nelson
804       philnelson@acm.org
805

ACKNOWLEDGEMENTS

807       The author would like to thank  Steve  Sommars  (Steve.Sommars@att.com)
808       for  his extensive help in testing the implementation.  Many great sug‐
809       gestions were given.  This is a much better product due to his involve‐
810       ment.
811
812
813
814GNU Project                       2006-06-11                             bc(1)
Impressum