OSDN Git Service

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