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

ENVIRONMENT VARIABLES

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

DIAGNOSTICS

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

BUGS

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

AUTHOR

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

ACKNOWLEDGEMENTS

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