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