~ubuntu-branches/ubuntu/jaunty/bc/jaunty

« back to all changes in this revision

Viewing changes to doc/bc.info

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2002-04-13 11:33:49 UTC
  • Revision ID: james.westby@ubuntu.com-20020413113349-hl2r1t730b91ov68
Tags: upstream-1.06
ImportĀ upstreamĀ versionĀ 1.06

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
This is bc.info, produced by makeinfo version 4.0 from bc.texi.
 
2
 
 
3
 
 
4
File: bc.info,  Node: Top,  Next: Introduction,  Prev: (dir),  Up: (dir)
 
5
 
 
6
* Menu:
 
7
 
 
8
* Introduction::
 
9
* Basic Elements::
 
10
* Expressions::
 
11
* Statements::
 
12
* Functions::
 
13
* Examples::
 
14
* Readline and Libedit Options::
 
15
* GNU `bc' and Other Implementations::
 
16
* Limits::
 
17
* Environment Variables::
 
18
 
 
19
 
 
20
File: bc.info,  Node: Introduction,  Next: Basic Elements,  Prev: Top,  Up: Top
 
21
 
 
22
Introduction
 
23
************
 
24
 
 
25
* Menu:
 
26
 
 
27
* Description::
 
28
* Command Line Options::
 
29
 
 
30
 
 
31
File: bc.info,  Node: Description,  Next: Command Line Options,  Prev: Introduction,  Up: Introduction
 
32
 
 
33
Description
 
34
===========
 
35
 
 
36
   `bc' [ -hlwsqv ] [long-options] [  FILE ... ]
 
37
 
 
38
   `bc' is a language that supports arbitrary precision numbers with
 
39
interactive execution of statements.  There are some similarities in
 
40
the syntax to the C programming language.  A standard math library is
 
41
available by command line option.  If requested, the math library is
 
42
defined before processing any files.  `bc' starts by processing code
 
43
from all the files listed on the command line in the order listed.
 
44
After all files have been processed, `bc' reads from the standard
 
45
input.  All code is executed as it is read.  (If a file contains a
 
46
command to halt the processor, `bc' will never read from the standard
 
47
input.)
 
48
 
 
49
   This version of `bc' contains several extensions beyond traditional
 
50
`bc' implementations and the POSIX draft standard.  Command line
 
51
options can cause these extensions to print a warning or to be
 
52
rejected.  This document describes the language accepted by this
 
53
processor.  Extensions will be identified as such.
 
54
 
 
55
   The author would like to thank Steve Sommars
 
56
(<Steve.Sommars@att.com>) for his extensive help in testing the
 
57
implementation.  Many great suggestions were given.  This is a much
 
58
better product due to his involvement.
 
59
 
 
60
   Email bug reports to <bug-bc@gnu.org>.  Be sure to include the word
 
61
"bc" somewhere in the "Subject:" field.
 
62
 
 
63
 
 
64
File: bc.info,  Node: Command Line Options,  Next: Numbers,  Prev: Description,  Up: Introduction
 
65
 
 
66
Command Line Options
 
67
====================
 
68
 
 
69
   `bc' takes the following options from the command line:
 
70
`-h, --help'
 
71
     Print the usage and exit.
 
72
 
 
73
`-l, --mathlib'
 
74
     Define the standard math library.
 
75
 
 
76
`-w, --warn'
 
77
     Give warnings for extensions to POSIX `bc'.
 
78
 
 
79
`-s, --standard'
 
80
     Process exactly the POSIX `bc' language.
 
81
 
 
82
`-q, --quiet'
 
83
     Do not print the normal GNU `bc' welcome.
 
84
 
 
85
`-v, --version'
 
86
     Print the version number and copyright and quit.
 
87
 
 
88
 
 
89
File: bc.info,  Node: Basic Elements,  Next: Expressions,  Prev: Introduction,  Up: Top
 
90
 
 
91
Basic Elements
 
92
**************
 
93
 
 
94
* Menu:
 
95
 
 
96
* Numbers::
 
97
* Variables::
 
98
* Comments::
 
99
 
 
100
 
 
101
File: bc.info,  Node: Numbers,  Next: Variables,  Prev: Command Line Options,  Up: Basic Elements
 
102
 
 
103
Numbers
 
104
=======
 
105
 
 
106
   The most basic element in `bc' is the number.  Numbers are arbitrary
 
107
precision numbers.  This precision is both in the integer part and the
 
108
fractional part.  All numbers are represented internally in decimal and
 
109
all computation is done in decimal.  (This version truncates results
 
110
from divide and multiply operations.)  There are two attributes of
 
111
numbers, the length and the scale.  The length is the total number of
 
112
significant decimal digits in a number and the scale is the total number
 
113
of decimal digits after the decimal point.  For example, .000001 has a
 
114
length of 6 and scale of 6, while 1935.000 has a length of 7 and a scale
 
115
of 3.
 
116
 
 
117
 
 
118
File: bc.info,  Node: Variables,  Next: Comments,  Prev: Numbers,  Up: Basic Elements
 
119
 
 
120
Variables
 
121
=========
 
122
 
 
123
   Numbers are stored in two types of variables, simple variables and
 
124
arrays.  Both simple variables and array variables are named.  Names
 
125
begin with a letter followed by any number of letters, digits and
 
126
underscores.  All letters must be lower case.  (Full alphanumeric names
 
127
are an extension. In POSIX `bc' all names are a single lower case
 
128
letter.)  The type of variable is clear by the context because all
 
129
array variable names will be followed by brackets ( [ ] ).
 
130
 
 
131
   There are four special variables, SCALE, IBASE, OBASE, and LAST.
 
132
SCALE defines how some operations use digits after the decimal point.
 
133
The default value of SCALE is 0. IBASE and OBASE define the conversion
 
134
base for input and output numbers.  The default for both input and
 
135
output is base 10.  LAST (an extension) is a variable that has the
 
136
value of the last printed number.  These will be discussed in further
 
137
detail where appropriate.  All of these variables may have values
 
138
assigned to them as well as used in expressions.
 
139
 
 
140
 
 
141
File: bc.info,  Node: Comments,  Prev: Variables,  Up: Basic Elements
 
142
 
 
143
Comments
 
144
========
 
145
 
 
146
   Comments in `bc' start with the characters `/*' and end with the
 
147
characters `*/'.  Comments may start anywhere and appear as a single
 
148
space in the input.  (This causes comments to delimit other input
 
149
items.  For example, a comment can not be found in the middle of a
 
150
variable name.)  Comments include any newlines (end of line) between
 
151
the start and the end of the comment.
 
152
 
 
153
   To support the use of scripts for `bc', a single line comment has
 
154
been added as an extension.  A single line comment starts at a `#'
 
155
character and continues to the next end of the line.  The end of line
 
156
character is not part of the comment and is processed normally.
 
157
 
 
158
 
 
159
File: bc.info,  Node: Expressions,  Next: Statements,  Prev: Basic Elements,  Up: Top
 
160
 
 
161
Expressions
 
162
***********
 
163
 
 
164
* Menu:
 
165
 
 
166
* About Expressions and Special Variables::
 
167
* Basic Expressions::
 
168
* Relational Expressions::
 
169
* Boolean Expressions::
 
170
* Precedence::
 
171
* Special Expressions::
 
172
 
 
173
 
 
174
File: bc.info,  Node: About Expressions and Special Variables,  Next: Basic Expressions,  Prev: Expressions,  Up: Expressions
 
175
 
 
176
About Expressions and Special Variables
 
177
=======================================
 
178
 
 
179
   The numbers are manipulated by expressions and statements.  Since
 
180
the language was designed to be interactive, statements and expressions
 
181
are executed as soon as possible.  There is no main program.  Instead,
 
182
code is executed as it is encountered.  (Functions, discussed in detail
 
183
later, are defined when encountered.)
 
184
 
 
185
   A simple expression is just a constant. `bc' converts constants into
 
186
internal decimal numbers using the current input base, specified by the
 
187
variable IBASE. (There is an exception in functions.)  The legal values
 
188
of IBASE are 2 through 16.  Assigning a value outside this range to
 
189
IBASE will result in a value of 2 or 16.  Input numbers may contain the
 
190
characters 0-9 and A-F. (Note: They must be capitals.  Lower case
 
191
letters are variable names.)  Single digit numbers always have the
 
192
value of the digit regardless of the value of IBASE. (i.e. A = 10.)
 
193
For multi-digit numbers, `bc' changes all input digits greater or equal
 
194
to IBASE to the value of IBASE-1.  This makes the number `FFF' always
 
195
be the largest 3 digit number of the input base.
 
196
 
 
197
   Full expressions are similar to many other high level languages.
 
198
Since there is only one kind of number, there are no rules for mixing
 
199
types.  Instead, there are rules on the scale of expressions.  Every
 
200
expression has a scale.  This is derived from the scale of original
 
201
numbers, the operation performed and in many cases, the value of the
 
202
variable SCALE. Legal values of the variable SCALE are 0 to the maximum
 
203
number representable by a C integer.
 
204
 
 
205
 
 
206
File: bc.info,  Node: Basic Expressions,  Next: Relational Expressions,  Prev: About Expressions and Special Variables,  Up: Expressions
 
207
 
 
208
Basic Expressions
 
209
=================
 
210
 
 
211
   In the following descriptions of legal expressions, "expr" refers to
 
212
a complete expression and "VAR" refers to a simple or an array variable.
 
213
A simple variable is just a
 
214
 
 
215
   NAME
 
216
 
 
217
   and an array variable is specified as
 
218
 
 
219
   NAME[EXPR]
 
220
 
 
221
   Unless specifically mentioned the scale of the result is the maximum
 
222
scale of the expressions involved.
 
223
 
 
224
`- expr'
 
225
     The result is the negation of the expression.
 
226
 
 
227
`++ VAR'
 
228
     The variable is incremented by one and the new value is the result
 
229
     of the expression.
 
230
 
 
231
`-- VAR'
 
232
     The variable is decremented by one and the new value is the result
 
233
     of the expression.
 
234
 
 
235
`VAR ++'
 
236
     The result of the expression is the value of the variable and then
 
237
     the variable is incremented by one.
 
238
 
 
239
`VAR --'
 
240
     The result of the expression is the value of the variable and then
 
241
     the variable is decremented by one.
 
242
 
 
243
`expr + expr'
 
244
     The result of the expression is the sum of the two expressions.
 
245
 
 
246
`expr - expr'
 
247
     The result of the expression is the difference of the two
 
248
     expressions.
 
249
 
 
250
`expr * expr'
 
251
     The result of the expression is the product of the two expressions.
 
252
 
 
253
`expr / expr'
 
254
     The result of the expression is the quotient of the two
 
255
     expressions.  The scale of the result is the value of the variable
 
256
     `scale'
 
257
 
 
258
`expr % expr'
 
259
     The result of the expression is the "remainder" and it is computed
 
260
     in the following way.  To compute a%b, first a/b is computed to
 
261
     SCALE digits.  That result is used to compute a-(a/b)*b to the
 
262
     scale of the maximum of SCALE+scale(b) and scale(a).  If SCALE is
 
263
     set to zero and both expressions are integers this expression is
 
264
     the integer remainder function.
 
265
 
 
266
`expr ^ expr'
 
267
     The result of the expression is the value of the first raised to
 
268
     the second. The second expression must be an integer.  (If the
 
269
     second expression is not an integer, a warning is generated and the
 
270
     expression is truncated to get an integer value.)  The scale of the
 
271
     result is SCALE if the exponent is negative.  If the exponent is
 
272
     positive the scale of the result is the minimum of the scale of the
 
273
     first expression times the value of the exponent and the maximum of
 
274
     SCALE and the scale of the first expression.  (e.g. scale(a^b) =
 
275
     min(scale(a)*b, max(SCALE, scale(a))).)  It should be noted that
 
276
     expr^0 will always return the value of 1.
 
277
 
 
278
`( expr )'
 
279
     This alters the standard precedence to force the evaluation of the
 
280
     expression.
 
281
 
 
282
`VAR = expr'
 
283
     The variable is assigned the value of the expression.
 
284
 
 
285
`VAR <op>= expr'
 
286
     This is equivalent to "VAR = VAR <op> expr" with the exception
 
287
     that the "VAR" part is evaluated only once.  This can make a
 
288
     difference if "VAR" is an array.
 
289
 
 
290
 
 
291
File: bc.info,  Node: Relational Expressions,  Next: Boolean Expressions,  Prev: Basic Expressions,  Up: Expressions
 
292
 
 
293
Relational Expressions
 
294
======================
 
295
 
 
296
   Relational expressions are a special kind of expression that always
 
297
evaluate to 0 or 1, 0 if the relation is false and 1 if the relation is
 
298
true.  These may appear in any legal expression.  (POSIX `bc' requires
 
299
that relational expressions are used only in `if', `while', and `for'
 
300
statements and that only one relational test may be done in them.)  The
 
301
relational operators are
 
302
 
 
303
`expr1 < expr2'
 
304
     The result is 1 if expr1 is strictly less than expr2.
 
305
 
 
306
`expr1 <= expr2'
 
307
     The result is 1 if expr1 is less than or equal to expr2.
 
308
 
 
309
`expr1 > expr2'
 
310
     The result is 1 if expr1 is strictly greater than expr2.
 
311
 
 
312
`expr1 >= expr2'
 
313
     The result is 1 if expr1 is greater than or equal to expr2.
 
314
 
 
315
`expr1 == expr2'
 
316
     The result is 1 if expr1 is equal to expr2.
 
317
 
 
318
`expr1 != expr2'
 
319
     The result is 1 if expr1 is not equal to expr2.
 
320
 
 
321
 
 
322
File: bc.info,  Node: Boolean Expressions,  Next: Precedence,  Prev: Relational Expressions,  Up: Expressions
 
323
 
 
324
Boolean Expressions
 
325
===================
 
326
 
 
327
   Boolean operations are also legal.  (POSIX `bc' does NOT have
 
328
boolean operations). The result of all boolean operations are 0 and 1
 
329
(for false and true) as in relational expressions.  The boolean
 
330
operators are:
 
331
 
 
332
`!expr'
 
333
     The result is 1 if expr is 0.
 
334
 
 
335
`expr && expr'
 
336
     The result is 1 if both expressions are non-zero.
 
337
 
 
338
`expr || expr'
 
339
     The result is 1 if either expression is non-zero.
 
340
 
 
341
 
 
342
File: bc.info,  Node: Precedence,  Next: Special Expressions,  Prev: Boolean Expressions,  Up: Expressions
 
343
 
 
344
Precedence
 
345
==========
 
346
 
 
347
   The expression precedence is as follows: (lowest to highest)
 
348
 
 
349
     || operator, left associative
 
350
     && operator, left associative
 
351
     ! operator, nonassociative
 
352
     Relational operators, left associative
 
353
     Assignment operator, right associative
 
354
     + and - operators, left associative
 
355
     *, / and % operators, left associative
 
356
     ^ operator, right associative
 
357
     unary - operator, nonassociative
 
358
     ++ and -- operators, nonassociative
 
359
 
 
360
   This precedence was chosen so that POSIX compliant `bc' programs
 
361
will run correctly. This will cause the use of the relational and
 
362
logical operators to have some unusual behavior when used with
 
363
assignment expressions.  Consider the expression:
 
364
 
 
365
     a = 3 < 5
 
366
 
 
367
   Most C programmers would assume this would assign the result of "3 <
 
368
5" (the value 1) to the variable "a".  What this does in `bc' is assign
 
369
the value 3 to the variable "a" and then compare 3 to 5.  It is best to
 
370
use parentheses when using relational and logical operators with the
 
371
assignment operators.
 
372
 
 
373
 
 
374
File: bc.info,  Node: Special Expressions,  Prev: Precedence,  Up: Expressions
 
375
 
 
376
Special Expressions
 
377
===================
 
378
 
 
379
   There are a few more special expressions that are provided in `bc'.
 
380
These have to do with user-defined functions and standard functions.
 
381
They all appear as "NAME`('PARAMETERS`)'".  *Note Functions::, for
 
382
user-defined functions.  The standard functions are:
 
383
 
 
384
`length ( expression )'
 
385
     The value of the length function is the number of significant
 
386
     digits in the expression.
 
387
 
 
388
`read ( )'
 
389
     The `read' function (an extension) will read a number from the
 
390
     standard input, regardless of where the function occurs.  Beware,
 
391
     this can cause problems with the mixing of data and program in the
 
392
     standard input.  The best use for this function is in a previously
 
393
     written program that needs input from the user, but never allows
 
394
     program code to be input from the user.  The value of the `read'
 
395
     function is the number read from the standard input using the
 
396
     current value of the variable IBASE for the conversion base.
 
397
 
 
398
`scale ( expression )'
 
399
     The value of the `scale' function is the number of digits after the
 
400
     decimal point in the expression.
 
401
 
 
402
`sqrt ( expression )'
 
403
     The value of the `sqrt' function is the square root of the
 
404
     expression.  If the expression is negative, a run time error is
 
405
     generated.
 
406
 
 
407
 
 
408
File: bc.info,  Node: Statements,  Next: Functions,  Prev: Expressions,  Up: Top
 
409
 
 
410
Statements
 
411
**********
 
412
 
 
413
* Menu:
 
414
 
 
415
* Pseudo Statements::
 
416
 
 
417
   Statements (as in most algebraic languages) provide the sequencing of
 
418
expression evaluation.  In `bc' statements are executed "as soon as
 
419
possible."  Execution happens when a newline in encountered and there
 
420
is one or more complete statements.  Due to this immediate execution,
 
421
newlines are very important in `bc'. In fact, both a semicolon and a
 
422
newline are used as statement separators.  An improperly placed newline
 
423
will cause a syntax error.  Because newlines are statement separators,
 
424
it is possible to hide a newline by using the backslash character.  The
 
425
sequence "\<nl>", where <nl> is the newline appears to `bc' as
 
426
whitespace instead of a newline.  A statement list is a series of
 
427
statements separated by semicolons and newlines.  The following is a
 
428
list of `bc' statements and what they do: (Things enclosed in brackets
 
429
( [ ] ) are optional parts of the statement.)
 
430
 
 
431
EXPRESSION
 
432
     This statement does one of two things.  If the expression starts
 
433
     with "<variable> <assignment> ...", it is considered to be an
 
434
     assignment statement.  If the expression is not an assignment
 
435
     statement, the expression is evaluated and printed to the output.
 
436
     After the number is printed, a newline is printed.  For example,
 
437
     "a=1" is an assignment statement and "(a=1)" is an expression that
 
438
     has an embedded assignment.  All numbers that are printed are
 
439
     printed in the base specified by the variable OBASE. The legal
 
440
     values for OBASE are 2 through BC_BASE_MAX (*note Environment
 
441
     Variables::).  For bases 2 through 16, the usual method of writing
 
442
     numbers is used.  For bases greater than 16, `bc' uses a
 
443
     multi-character digit method of printing the numbers where each
 
444
     higher base digit is printed as a base 10 number.  The
 
445
     multi-character digits are separated by spaces.  Each digit
 
446
     contains the number of characters required to represent the base
 
447
     ten value of "OBASE -1".  Since numbers are of arbitrary
 
448
     precision, some numbers may not be printable on a single output
 
449
     line.  These long numbers will be split across lines using the "\"
 
450
     as the last character on a line.  The maximum number of characters
 
451
     printed per line is 70.  Due to the interactive nature of `bc',
 
452
     printing a number causes the side effect of assigning the printed
 
453
     value to the special variable LAST. This allows the user to
 
454
     recover the last value printed without having to retype the
 
455
     expression that printed the number.  Assigning to LAST is legal
 
456
     and will overwrite the last printed value with the assigned value.
 
457
     The newly assigned value will remain until the next number is
 
458
     printed or another value is assigned to LAST.  (Some installations
 
459
     may allow the use of a single period (.) which is not part of a
 
460
     number as a short hand notation for for LAST.)
 
461
 
 
462
STRING
 
463
     The string is printed to the output.  Strings start with a double
 
464
     quote character and contain all characters until the next double
 
465
     quote character.  All characters are taken literally, including
 
466
     any newline.  No newline character is printed after the string.
 
467
 
 
468
`PRINT' LIST
 
469
     The `print' statement (an extension) provides another method of
 
470
     output.  The LIST is a list of strings and expressions separated by
 
471
     commas.  Each string or expression is printed in the order of the
 
472
     list.  No terminating newline is printed.  Expressions are
 
473
     evaluated and their value is printed and assigned to the variable
 
474
     `last'. Strings in the print statement are printed to the output
 
475
     and may contain special characters.  Special characters start with
 
476
     the backslash character (\e).  The special characters recognized
 
477
     by `bc' are "a" (alert or bell), "b" (backspace), "f" (form feed),
 
478
     "n" (newline), "r" (carriage return), "q" (double quote), "t"
 
479
     (tab), and "\e" (backslash).  Any other character following the
 
480
     backslash will be ignored.
 
481
 
 
482
{ STATEMENT_LIST }
 
483
     This is the compound statement.  It allows multiple statements to
 
484
     be grouped together for execution.
 
485
 
 
486
`IF' ( EXPRESSION ) STATEMENT1 [`ELSE' STATEMENT2]
 
487
     The if statement evaluates the expression and executes statement1
 
488
     or statement2 depending on the value of the expression.  If the
 
489
     expression is non-zero, statement1 is executed.  If statement2 is
 
490
     present and the value of the expression is 0, then statement2 is
 
491
     executed.  (The `else' clause is an extension.)
 
492
 
 
493
`WHILE' ( EXPRESSION ) STATEMENT
 
494
     The while statement will execute the statement while the expression
 
495
     is non-zero.  It evaluates the expression before each execution of
 
496
     the statement.   Termination of the loop is caused by a zero
 
497
     expression value or the execution of a `break' statement.
 
498
 
 
499
`FOR' ( [EXPRESSION1] ; [EXPRESSION2] ; [EXPRESSION3] ) STATEMENT
 
500
     The `for' statement controls repeated execution of the statement.
 
501
     EXPRESSION1 is evaluated before the loop.  EXPRESSION2 is
 
502
     evaluated before each execution of the statement.  If it is
 
503
     non-zero, the statement is evaluated.  If it is zero, the loop is
 
504
     terminated.  After each execution of the statement, EXPRESSION3 is
 
505
     evaluated before the reevaluation of expression2.  If EXPRESSION1
 
506
     or EXPRESSION3 are missing, nothing is evaluated at the point they
 
507
     would be evaluated.  If EXPRESSION2 is missing, it is the same as
 
508
     substituting the value 1 for EXPRESSION2.  (The optional
 
509
     expressions are an extension. POSIX `bc' requires all three
 
510
     expressions.)  The following is equivalent code for the `for'
 
511
     statement:
 
512
 
 
513
          expression1;
 
514
          while (expression2) {
 
515
             statement;
 
516
             expression3;
 
517
          }
 
518
 
 
519
`BREAK'
 
520
     This statement causes a forced exit of the most recent enclosing
 
521
     `while' statement or `for' statement.
 
522
 
 
523
`CONTINUE'
 
524
     The `continue' statement (an extension)  causes the most recent
 
525
     enclosing `for' statement to start the next iteration.
 
526
 
 
527
`HALT'
 
528
     The `halt' statement (an extension) is an executed statement that
 
529
     causes the `bc' processor to quit only when it is executed.  For
 
530
     example, "if (0 == 1) halt" will not cause `bc' to terminate
 
531
     because the `halt' is not executed.
 
532
 
 
533
`RETURN'
 
534
     Return the value 0 from a function.  (*Note Functions::.)
 
535
 
 
536
`RETURN' ( EXPRESSION )
 
537
     Return the value of the expression from a function.  (*Note
 
538
     Functions::.)  As an extension, the parenthesis are not required.
 
539
 
 
540
 
 
541
File: bc.info,  Node: Pseudo Statements,  Prev: Statements,  Up: Statements
 
542
 
 
543
Pseudo Statements
 
544
=================
 
545
 
 
546
   These statements are not statements in the traditional sense.  They
 
547
are not executed statements.  Their function is performed at "compile"
 
548
time.
 
549
 
 
550
`limits'
 
551
     Print the local limits enforced by the local version of `bc'.  This
 
552
     is an extension.
 
553
 
 
554
`quit'
 
555
     When the `quit' statement is read, the `bc' processor is
 
556
     terminated, regardless of where the `quit' statement is found.  For
 
557
     example, "if (0 == 1) quit" will cause `bc' to terminate.
 
558
 
 
559
`warranty'
 
560
     Print a longer warranty notice.  This is an extension.
 
561
 
 
562
 
 
563
File: bc.info,  Node: Functions,  Next: Examples,  Prev: Statements,  Up: Top
 
564
 
 
565
Functions
 
566
*********
 
567
 
 
568
* Menu:
 
569
 
 
570
* Math Library Functions::
 
571
 
 
572
   Functions provide a method of defining a computation that can be
 
573
executed later.  Functions in `bc' always compute a value and return it
 
574
to the caller.  Function definitions are "dynamic" in the sense that a
 
575
function is undefined until a definition is encountered in the input.
 
576
That definition is then used until another definition function for the
 
577
same name is encountered.  The new definition then replaces the older
 
578
definition.  A function is defined as follows:
 
579
 
 
580
     `define' NAME `(' PARAMETERS `)' `{' NEWLINE
 
581
         AUTO_LIST   STATEMENT_LIST `}'
 
582
 
 
583
   A function call is just an expression of the form "`name'
 
584
`('PARAMETERS`)'".
 
585
 
 
586
   Parameters are numbers or arrays (an extension).  In the function
 
587
definition, zero or more parameters are defined by listing their names
 
588
separated by commas.  Numbers are only call by value parameters.
 
589
Arrays are only call by variable.  Arrays are specified in the
 
590
parameter definition by the notation "NAME`[ ]'".   In the function
 
591
call, actual parameters are full expressions for number parameters.
 
592
The same notation is used for passing arrays as for defining array
 
593
parameters.  The named array is passed by variable to the function.
 
594
Since function definitions are dynamic, parameter numbers and types are
 
595
checked when a function is called.  Any mismatch in number or types of
 
596
parameters will cause a runtime error.  A runtime error will also occur
 
597
for the call to an undefined function.
 
598
 
 
599
   The AUTO_LIST is an optional list of variables that are for "local"
 
600
use.  The syntax of the auto list (if present) is "`auto' NAME, ... ;".
 
601
(The semicolon is optional.)  Each NAME is the name of an auto
 
602
variable.  Arrays may be specified by using the same notation as used
 
603
in parameters.  These variables have their values pushed onto a stack
 
604
at the start of the function.  The variables are then initialized to
 
605
zero and used throughout the execution of the function.  At function
 
606
exit, these variables are popped so that the original value (at the
 
607
time of the function call) of these variables are restored.  The
 
608
parameters are really auto variables that are initialized to a value
 
609
provided in the function call.  Auto variables are different than
 
610
traditional local variables because if function A calls function B, B
 
611
may access function A's auto variables by just using the same name,
 
612
unless function B has called them auto variables.  Due to the fact that
 
613
auto variables and parameters are pushed onto a stack, `bc' supports
 
614
recursive functions.
 
615
 
 
616
   The function body is a list of `bc' statements.  Again, statements
 
617
are separated by semicolons or newlines.  Return statements cause the
 
618
termination of a function and the return of a value.  There are two
 
619
versions of the return statement.  The first form, "`return'", returns
 
620
the value 0 to the calling expression.  The second form, "`return' (
 
621
EXPRESSION )", computes the value of the expression and returns that
 
622
value to the calling expression.  There is an implied "`return' (0)" at
 
623
the end of every function.  This allows a function to terminate and
 
624
return 0 without an explicit `return' statement.
 
625
 
 
626
   Functions also change the usage of the variable IBASE.  All
 
627
constants in the function body will be converted using the value of
 
628
IBASE at the time of the function call.  Changes of IBASE will be
 
629
ignored during the execution of the function except for the standard
 
630
function `read', which will always use the current value of IBASE for
 
631
conversion of numbers.
 
632
 
 
633
   As an extension, the format of the definition has been slightly
 
634
relaxed.  The standard requires the opening brace be on the same line
 
635
as the `define' keyword and all other parts must be on following lines.
 
636
This version of `bc' will allow any number of newlines before and after
 
637
the opening brace of the function.  For example, the following
 
638
definitions are legal.
 
639
 
 
640
        define d (n) { return (2*n); }
 
641
        define d (n)
 
642
            { return (2*n); }
 
643
 
 
644
 
 
645
File: bc.info,  Node: Math Library Functions,  Prev: Functions,  Up: Functions
 
646
 
 
647
Math Library Functions
 
648
======================
 
649
 
 
650
   If `bc' is invoked with the `-l' option, a math library is preloaded
 
651
and the default SCALE is set to 20.  The math functions will calculate
 
652
their results to the scale set at the time of their call.  The math
 
653
library defines the following functions:
 
654
 
 
655
`s (X)'
 
656
     The sine of X, X is in radians.
 
657
 
 
658
`c (X)'
 
659
     The cosine of X, X is in radians.
 
660
 
 
661
`a (X)'
 
662
     The arctangent of X, arctangent returns radians.
 
663
 
 
664
`l (X)'
 
665
     The natural logarithm of X.
 
666
 
 
667
`E (X)'
 
668
     The exponential function of raising E to the value X.
 
669
 
 
670
`J (N,X)'
 
671
     The bessel function of integer order N of X.
 
672
 
 
673
 
 
674
File: bc.info,  Node: Examples,  Next: Readline and Libedit Options,  Prev: Functions,  Up: Top
 
675
 
 
676
Examples
 
677
********
 
678
 
 
679
   In /bin/sh,  the following will assign the value of "pi" to the shell
 
680
variable PI.
 
681
 
 
682
     pi=$(echo "scale=10; 4*a(1)" | bc -l)
 
683
 
 
684
   The following is the definition of the exponential function used in
 
685
the math library.  This function is written in POSIX `bc'.
 
686
 
 
687
 
 
688
     scale = 20
 
689
     
 
690
     /* Uses the fact that e^x = (e^(x/2))^2
 
691
        When x is small enough, we use the series:
 
692
          e^x = 1 + x + x^2/2! + x^3/3! + ...
 
693
     */
 
694
     
 
695
     define e(x) {
 
696
       auto  a, d, e, f, i, m, v, z
 
697
     
 
698
       /* Check the sign of x. */
 
699
       if (x<0) {
 
700
         m = 1
 
701
         x = -x
 
702
       }
 
703
     
 
704
       /* Precondition x. */
 
705
       z = scale;
 
706
       scale = 4 + z + .44*x;
 
707
       while (x > 1) {
 
708
         f += 1;
 
709
         x /= 2;
 
710
       }
 
711
     
 
712
       /* Initialize the variables. */
 
713
       v = 1+x
 
714
       a = x
 
715
       d = 1
 
716
     
 
717
       for (i=2; 1; i++) {
 
718
         e = (a *= x) / (d *= i)
 
719
         if (e == 0) {
 
720
           if (f>0) while (f--)  v = v*v;
 
721
           scale = z
 
722
           if (m) return (1/v);
 
723
           return (v/1);
 
724
         }
 
725
         v += e
 
726
       }
 
727
     }
 
728
 
 
729
   The following is code that uses the extended features of `bc' to
 
730
implement a simple program for calculating checkbook balances.  This
 
731
program is best kept in a file so that it can be used many times
 
732
without having to retype it at every use.
 
733
 
 
734
 
 
735
     scale=2
 
736
     print "\nCheck book program\n!"
 
737
     print "  Remember, deposits are negative transactions.\n"
 
738
     print "  Exit by a 0 transaction.\n\n"
 
739
     
 
740
     print "Initial balance? "; bal = read()
 
741
     bal /= 1
 
742
     print "\n"
 
743
     while (1) {
 
744
       "current balance = "; bal
 
745
       "transaction? "; trans = read()
 
746
       if (trans == 0) break;
 
747
       bal -= trans
 
748
       bal /= 1
 
749
     }
 
750
     quit
 
751
 
 
752
   The following is the definition of the recursive factorial function.
 
753
 
 
754
 
 
755
     define f (x) {
 
756
       if (x <= 1) return (1);
 
757
       return (f(x-1) * x);
 
758
     }
 
759
 
 
760
 
 
761
File: bc.info,  Node: Readline and Libedit Options,  Next: GNU `bc' and Other Implementations,  Prev: Examples,  Up: Top
 
762
 
 
763
Readline and Libedit Options
 
764
****************************
 
765
 
 
766
   GNU `bc' can be compiled (via a configure option) to use the GNU
 
767
`readline' input editor library or the BSD `libedit' library.  This
 
768
allows the user to do more editing of lines before sending them to
 
769
`bc'.  It also allows for a history of previous lines typed.  When this
 
770
option is selected, `bc' has one more special variable.  This special
 
771
variable, HISTORY is the number of lines of history retained.  A value
 
772
of -1 means that an unlimited number of history lines are retained.
 
773
This is the default value.  Setting the value of HISTORY to a positive
 
774
number restricts the number of history lines to the number given.  The
 
775
value of 0 disables the history feature.  For more information, read
 
776
the user manuals for the GNU `readline', `history' and BSD `libedit'
 
777
libraries.  One can not enable both `readline' and `libedit' at the
 
778
same time.
 
779
 
 
780
 
 
781
File: bc.info,  Node: GNU `bc' and Other Implementations,  Next: Limits,  Prev: Readline and Libedit Options,  Up: Top
 
782
 
 
783
GNU `bc' and Other Implementations
 
784
**********************************
 
785
 
 
786
   This version of `bc' was implemented from the POSIX P1003.2/D11
 
787
draft and contains several differences and extensions relative to the
 
788
draft and traditional implementations.  It is not implemented in the
 
789
traditional way using `dc'.  This version is a single process which
 
790
parses and runs a byte code translation of the program.  There is an
 
791
"undocumented" option (-c) that causes the program to output the byte
 
792
code to the standard output instead of running it.  It was mainly used
 
793
for debugging the parser and preparing the math library.
 
794
 
 
795
   A major source of differences is extensions, where a feature is
 
796
extended to add more functionality and additions, where new features
 
797
are added.  The following is the list of differences and extensions.
 
798
 
 
799
LANG ENVIRONMENT
 
800
     This version does not conform to the POSIX standard in the
 
801
     processing of the LANG environment variable and all environment
 
802
     variables starting with LC_.
 
803
 
 
804
NAMES
 
805
     Traditional and POSIX `bc' have single letter names for functions,
 
806
     variables and arrays.  They have been extended to be
 
807
     multi-character names that start with a letter and may contain
 
808
     letters, numbers and the underscore character.
 
809
 
 
810
STRINGS
 
811
     Strings are not allowed to contain NUL characters.  POSIX says all
 
812
     characters must be included in strings.
 
813
 
 
814
LAST
 
815
     POSIX `bc' does not have a \fBlast variable.  Some implementations
 
816
     of `bc' use the period (.) in a similar way.
 
817
 
 
818
COMPARISONS
 
819
     POSIX `bc' allows comparisons only in the `if' statement, the
 
820
     `while' statement, and the second expression of the `for'
 
821
     statement.  Also, only one relational operation is allowed in each
 
822
     of those statements.
 
823
 
 
824
IF STATEMENT, ELSE CLAUSE
 
825
     POSIX `bc' does not have an `else' clause.
 
826
 
 
827
FOR STATEMENT
 
828
     POSIX `bc' requires all expressions to be present in the `for'
 
829
     statement.
 
830
 
 
831
&&, ||, !
 
832
     POSIX `bc' does not have the logical operators.
 
833
 
 
834
READ FUNCTION
 
835
     POSIX `bc' does not have a `read' function.
 
836
 
 
837
PRINT STATEMENT
 
838
     POSIX `bc' does not have a `print' statement.
 
839
 
 
840
CONTINUE STATEMENT
 
841
     POSIX `bc' does not have a continue statement.
 
842
 
 
843
ARRAY PARAMETERS
 
844
     POSIX `bc' does not (currently) support array parameters in full.
 
845
     The POSIX grammar allows for arrays in function definitions, but
 
846
     does not provide a method to specify an array as an actual
 
847
     parameter.  (This is most likely an oversight in the grammar.)
 
848
     Traditional implementations of `bc' have only call by value array
 
849
     parameters.
 
850
 
 
851
FUNCTION FORMAT
 
852
     POSIX `bc' requires the opening brace on the same line as the
 
853
     `define' key word and the `auto' statement on the next line.
 
854
 
 
855
=+, =-, =*, =/, =%, =^
 
856
     POSIX `bc' does not require these "old style" assignment operators
 
857
     to be defined.  This version may allow these "old style"
 
858
     assignments.  Use the `limits' statement to see if the installed
 
859
     version supports them.  If it does support the "old style"
 
860
     assignment operators, the statement "a =- 1" will decrement `a' by
 
861
     1 instead of setting `a' to the value -1.
 
862
 
 
863
SPACES IN NUMBERS
 
864
     Other implementations of `bc' allow spaces in numbers.  For
 
865
     example, "x=1 3" would assign the value 13 to the variable x.  The
 
866
     same statement would cause a syntax error in this version of `bc'.
 
867
 
 
868
ERRORS AND EXECUTION
 
869
     This implementation varies from other implementations in terms of
 
870
     what code will be executed when syntax and other errors are found
 
871
     in the program.  If a syntax error is found in a function
 
872
     definition, error recovery tries to find the beginning of a
 
873
     statement and continue to parse the function.  Once a syntax error
 
874
     is found in the function, the function will not be callable and
 
875
     becomes undefined.  Syntax errors in the interactive execution
 
876
     code will invalidate the current execution block.  The execution
 
877
     block is terminated by an end of line that appears after a
 
878
     complete sequence of statements.  For example,
 
879
 
 
880
          a = 1
 
881
          b = 2
 
882
 
 
883
     has two execution blocks and
 
884
 
 
885
          { a = 1
 
886
            b = 2 }
 
887
 
 
888
     has one execution block.  Any runtime error will terminate the
 
889
     execution of the current execution block.  A runtime warning will
 
890
     not terminate the current execution block.
 
891
 
 
892
INTERRUPTS
 
893
     During an interactive session, the SIGINT signal (usually
 
894
     generated by the control-C character from the terminal) will cause
 
895
     execution of the current execution block to be interrupted.  It
 
896
     will display a "runtime" error indicating which function was
 
897
     interrupted.  After all runtime structures have been cleaned up, a
 
898
     message will be printed to notify the user that `bc' is ready for
 
899
     more input.  All previously defined functions remain defined and
 
900
     the value of all non-auto variables are the value at the point of
 
901
     interruption.  All auto variables and function parameters are
 
902
     removed during the clean up process.  During a non-interactive
 
903
     session, the SIGINT signal will terminate the entire run of `bc'.
 
904
 
 
905
 
 
906
File: bc.info,  Node: Limits,  Next: Environment Variables,  Prev: GNU `bc' and Other Implementations,  Up: Top
 
907
 
 
908
Limits
 
909
******
 
910
 
 
911
   The following are the limits currently in place for this `bc'
 
912
processor.  Some of them may have been changed by an installation.  Use
 
913
the `limits' statement to see the actual values.
 
914
 
 
915
`BC_BASE_MAX'
 
916
     The maximum output base is currently set at 999.  The maximum
 
917
     input base is 16.
 
918
 
 
919
`BC_DIM_MAX'
 
920
     This is currently an arbitrary limit of 65535 as distributed.  Your
 
921
     installation may be different.
 
922
 
 
923
`BC_SCALE_MAX'
 
924
     The number of digits after the decimal point is limited to INT_MAX
 
925
     digits.  Also, the number of digits before the decimal point is
 
926
     limited to INT_MAX digits.
 
927
 
 
928
`BC_STRING_MAX'
 
929
     The limit on the number of characters in a string is INT_MAX
 
930
     characters.
 
931
 
 
932
`exponent'
 
933
     The value of the exponent in the raise operation (^) is limited to
 
934
     LONG_MAX.
 
935
 
 
936
`multiply'
 
937
     The multiply routine may yield incorrect results if a number has
 
938
     more than LONG_MAX / 90 total digits.  For 32 bit longs, this
 
939
     number is 23,860,929 digits.
 
940
 
 
941
`variable names'
 
942
     The current limit on the number of unique names is 32767 for each
 
943
     of simple variables, arrays and functions.
 
944
 
 
945
 
 
946
File: bc.info,  Node: Environment Variables,  Prev: Limits,  Up: Top
 
947
 
 
948
Environment Variables
 
949
*********************
 
950
 
 
951
   The following environment variables are processed by `bc':
 
952
 
 
953
`POSIXLY_CORRECT'
 
954
     This is the same as the -s option (*note Command Line Options::).
 
955
 
 
956
`BC_ENV_ARGS'
 
957
     This is another mechanism to get arguments to `bc'.  The format is
 
958
     the same as the command line arguments.  These arguments are
 
959
     processed first, so any files listed in the environent arguments
 
960
     are processed before any command line argument files.  This allows
 
961
     the user to set up "standard" options and files to be processed at
 
962
     every invocation of `bc'.  The files in the environment variables
 
963
     would typically contain function definitions for functions the user
 
964
     wants defined every time `bc' is run.
 
965
 
 
966
`BC_LINE_LENGTH'
 
967
     This should be an integer specifing the number of characters in an
 
968
     output line for numbers. This includes the backslash and newline
 
969
     characters for long numbers.
 
970
 
 
971
 
 
972
 
 
973
Tag Table:
 
974
Node: Top65
 
975
Node: Introduction354
 
976
Node: Description515
 
977
Node: Command Line Options1969
 
978
Node: Basic Elements2533
 
979
Node: Numbers2704
 
980
Node: Variables3467
 
981
Node: Comments4576
 
982
Node: Expressions5317
 
983
Node: About Expressions and Special Variables5597
 
984
Node: Basic Expressions7333
 
985
Node: Relational Expressions10274
 
986
Node: Boolean Expressions11279
 
987
Node: Precedence11834
 
988
Node: Special Expressions12994
 
989
Node: Statements14376
 
990
Node: Pseudo Statements21001
 
991
Node: Functions21649
 
992
Node: Math Library Functions25703
 
993
Node: Examples26413
 
994
Node: Readline and Libedit Options28431
 
995
Node: GNU `bc' and Other Implementations29458
 
996
Node: Limits34700
 
997
Node: Environment Variables35953
 
998
 
 
999
End Tag Table