1
This is bc.info, produced by makeinfo version 4.0 from bc.texi.
1
This is bc.info, produced by makeinfo version 4.8 from bc.texi.
4
* bc: (bc). An arbitrary precision calculator language.
4
8
File: bc.info, Node: Top, Next: Introduction, Prev: (dir), Up: (dir)
14
18
* Readline and Libedit Options::
15
* GNU `bc' and Other Implementations::
19
* Comparison with Other Implementations::
17
21
* Environment Variables::
20
24
File: bc.info, Node: Introduction, Next: Basic Elements, Prev: Top, Up: Top
31
35
File: bc.info, Node: Description, Next: Command Line Options, Prev: Introduction, Up: Introduction
36
`bc' [ -hlwsqv ] [long-options] [ FILE ... ]
40
`bc' [ -hlwsqv ] [long-options] [ FILE ... ]
38
42
`bc' is a language that supports arbitrary precision numbers with
39
43
interactive execution of statements. There are some similarities in
101
106
File: bc.info, Node: Numbers, Next: Variables, Prev: Command Line Options, Up: Basic Elements
106
The most basic element in `bc' is the number. Numbers are arbitrary
111
The most basic element in `bc' is the number. Numbers are arbitrary
107
112
precision numbers. This precision is both in the integer part and the
108
113
fractional part. All numbers are represented internally in decimal and
109
114
all computation is done in decimal. (This version truncates results
118
123
File: bc.info, Node: Variables, Next: Comments, Prev: Numbers, Up: Basic Elements
123
Numbers are stored in two types of variables, simple variables and
128
Numbers are stored in two types of variables, simple variables and
124
129
arrays. Both simple variables and array variables are named. Names
125
130
begin with a letter followed by any number of letters, digits and
126
131
underscores. All letters must be lower case. (Full alphanumeric names
141
146
File: bc.info, Node: Comments, Prev: Variables, Up: Basic Elements
146
Comments in `bc' start with the characters `/*' and end with the
151
Comments in `bc' start with the characters `/*' and end with the
147
152
characters `*/'. Comments may start anywhere and appear as a single
148
153
space in the input. (This causes comments to delimit other input
149
154
items. For example, a comment can not be found in the middle of a
174
179
File: bc.info, Node: About Expressions and Special Variables, Next: Basic Expressions, Prev: Expressions, Up: Expressions
176
About Expressions and Special Variables
177
=======================================
181
3.1 About Expressions and Special Variables
182
===========================================
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
184
The numbers are manipulated by expressions and statements. Since the
185
language was designed to be interactive, statements and expressions are
186
executed as soon as possible. There is no main program. Instead, code
187
is executed as it is encountered. (Functions, discussed in detail
183
188
later, are defined when encountered.)
185
190
A simple expression is just a constant. `bc' converts constants into
206
211
File: bc.info, Node: Basic Expressions, Next: Relational Expressions, Prev: About Expressions and Special Variables, Up: Expressions
213
3.2 Basic Expressions
214
=====================
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.
216
In the following descriptions of legal expressions, "expr" refers to a
217
complete expression and "VAR" refers to a simple or an array variable.
213
218
A simple variable is just a
291
296
File: bc.info, Node: Relational Expressions, Next: Boolean Expressions, Prev: Basic Expressions, Up: Expressions
293
Relational Expressions
294
======================
298
3.3 Relational Expressions
299
==========================
296
Relational expressions are a special kind of expression that always
301
Relational expressions are a special kind of expression that always
297
302
evaluate to 0 or 1, 0 if the relation is false and 1 if the relation is
298
303
true. These may appear in any legal expression. (POSIX `bc' requires
299
304
that relational expressions are used only in `if', `while', and `for'
322
327
File: bc.info, Node: Boolean Expressions, Next: Precedence, Prev: Relational Expressions, Up: Expressions
329
3.4 Boolean Expressions
330
=======================
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
332
Boolean operations are also legal. (POSIX `bc' does NOT have boolean
333
operations). The result of all boolean operations are 0 and 1 (for
334
false and true) as in relational expressions. The boolean operators
333
338
The result is 1 if expr is 0.
342
347
File: bc.info, Node: Precedence, Next: Special Expressions, Prev: Boolean Expressions, Up: Expressions
347
The expression precedence is as follows: (lowest to highest)
352
The expression precedence is as follows: (lowest to highest)
349
354
|| operator, left associative
350
355
&& operator, left associative
374
379
File: bc.info, Node: Special Expressions, Prev: Precedence, Up: Expressions
381
3.6 Special Expressions
382
=======================
379
There are a few more special expressions that are provided in `bc'.
384
There are a few more special expressions that are provided in `bc'.
380
385
These have to do with user-defined functions and standard functions.
381
386
They all appear as "NAME`('PARAMETERS`)'". *Note Functions::, for
382
387
user-defined functions. The standard functions are:
541
546
File: bc.info, Node: Pseudo Statements, Prev: Statements, Up: Statements
548
4.1 Pseudo Statements
549
=====================
546
These statements are not statements in the traditional sense. They
547
are not executed statements. Their function is performed at "compile"
551
These statements are not statements in the traditional sense. They are
552
not executed statements. Their function is performed at "compile" time.
551
555
Print the local limits enforced by the local version of `bc'. This
586
590
Parameters are numbers or arrays (an extension). In the function
587
591
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.
592
separated by commas. All parameters are call by value parameters.
593
Arrays are specified in the parameter definition by the notation
594
"NAME`[ ]'". In the function call, actual parameters are full
595
expressions for number parameters. The same notation is used for
596
passing arrays as for defining array parameters. The named array is
597
passed by value to the function. Since function definitions are
598
dynamic, parameter numbers and types are checked when a function is
599
called. Any mismatch in number or types of parameters will cause a
600
runtime error. A runtime error will also occur for the call to an
599
603
The AUTO_LIST is an optional list of variables that are for "local"
600
604
use. The syntax of the auto list (if present) is "`auto' NAME, ... ;".
630
634
function `read', which will always use the current value of IBASE for
631
635
conversion of numbers.
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.
637
Several extensions have been added to functions. First, the format
638
of the definition has been slightly relaxed. The standard requires the
639
opening brace be on the same line as the `define' keyword and all other
640
parts must be on following lines. This version of `bc' will allow any
641
number of newlines before and after the opening brace of the function.
642
For example, the following definitions are legal.
640
644
define d (n) { return (2*n); }
642
646
{ return (2*n); }
648
Functions may be defined as `void'. A void funtion returns no value
649
and thus may not be used in any place that needs a value. A void
650
function does not produce any output when called by itself on an input
651
line. The key word `void' is placed between the key word `define' and
652
the function name. For example, consider the following session.
654
define py (y) { print "--->", y, "<---", "\n"; }
655
define void px (x) { print "--->", x, "<---", "\n"; }
662
Since `py' is not a void function, the call of `py(1)' prints the
663
desired output and then prints a second line that is the value of the
664
function. Since the value of a function that is not given an explicit
665
return statement is zero, the zero is printed. For `px(1)', no zero is
666
printed because the function is a void function.
668
Also, call by variable for arrays was added. To declare a call by
669
variable array, the declaration of the array parameter in the function
670
definition looks like "`*'NAME`[]'". The call to the function remains
671
the same as call by value arrays.
645
674
File: bc.info, Node: Math Library Functions, Prev: Functions, Up: Functions
647
Math Library Functions
648
======================
676
5.1 Math Library Functions
677
==========================
650
If `bc' is invoked with the `-l' option, a math library is preloaded
679
If `bc' is invoked with the `-l' option, a math library is preloaded
651
680
and the default SCALE is set to 20. The math functions will calculate
652
681
their results to the scale set at the time of their call. The math
653
682
library defines the following functions:
665
694
The natural logarithm of X.
668
697
The exponential function of raising E to the value X.
671
The bessel function of integer order N of X.
700
The Bessel function of integer order N of X.
674
703
File: bc.info, Node: Examples, Next: Readline and Libedit Options, Prev: Functions, Up: Top
679
In /bin/sh, the following will assign the value of "pi" to the shell
708
In /bin/sh, the following will assign the value of "pi" to the shell
682
711
pi=$(echo "scale=10; 4*a(1)" | bc -l)
690
719
/* Uses the fact that e^x = (e^(x/2))^2
691
720
When x is small enough, we use the series:
692
721
e^x = 1 + x + x^2/2! + x^3/3! + ...
696
725
auto a, d, e, f, i, m, v, z
698
727
/* Check the sign of x. */
704
733
/* Precondition x. */
706
735
scale = 4 + z + .44*x;
761
File: bc.info, Node: Readline and Libedit Options, Next: GNU `bc' and Other Implementations, Prev: Examples, Up: Top
763
Readline and Libedit Options
764
****************************
766
GNU `bc' can be compiled (via a configure option) to use the GNU
790
File: bc.info, Node: Readline and Libedit Options, Next: Comparison with Other Implementations, Prev: Examples, Up: Top
792
7 Readline and Libedit Options
793
******************************
795
GNU `bc' can be compiled (via a configure option) to use the GNU
767
796
`readline' input editor library or the BSD `libedit' library. This
768
797
allows the user to do more editing of lines before sending them to
769
798
`bc'. It also allows for a history of previous lines typed. When this
781
File: bc.info, Node: GNU `bc' and Other Implementations, Next: Limits, Prev: Readline and Libedit Options, Up: Top
783
GNU `bc' and Other Implementations
784
**********************************
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
810
File: bc.info, Node: Comparison with Other Implementations, Next: Limits, Prev: Readline and Libedit Options, Up: Top
812
8 Comparison with Other Implementations
813
***************************************
815
This version of `bc' was implemented from the POSIX P1003.2/D11 draft
816
and contains several differences and extensions relative to the draft
817
and traditional implementations. It is not implemented in the
789
818
traditional way using `dc'. This version is a single process which
790
819
parses and runs a byte code translation of the program. There is an
791
820
"undocumented" option (-c) that causes the program to output the byte
903
932
session, the SIGINT signal will terminate the entire run of `bc'.
906
File: bc.info, Node: Limits, Next: Environment Variables, Prev: GNU `bc' and Other Implementations, Up: Top
911
The following are the limits currently in place for this `bc'
935
File: bc.info, Node: Limits, Next: Environment Variables, Prev: Comparison with Other Implementations, Up: Top
940
The following are the limits currently in place for this `bc'
912
941
processor. Some of them may have been changed by an installation. Use
913
942
the `limits' statement to see the actual values.
957
986
This is another mechanism to get arguments to `bc'. The format is
958
987
the same as the command line arguments. These arguments are
959
processed first, so any files listed in the environent arguments
988
processed first, so any files listed in the environment arguments
960
989
are processed before any command line argument files. This allows
961
990
the user to set up "standard" options and files to be processed at
962
991
every invocation of `bc'. The files in the environment variables
964
993
wants defined every time `bc' is run.
967
This should be an integer specifing the number of characters in an
996
This should be an integer specifying the number of characters in an
968
997
output line for numbers. This includes the backslash and newline
969
characters for long numbers.
998
characters for long numbers. As an extension, the value of zero
999
disables the multi-line feature. Any other value of this variable
1000
that is less than 3 sets the line length to 70.
975
Node: Introduction354
976
Node: Description515
977
Node: Command Line Options1969
978
Node: Basic Elements2533
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
994
Node: Readline and Libedit Options28431
995
Node: GNU `bc' and Other Implementations29458
997
Node: Environment Variables35953
1006
Node: Introduction473
1007
Node: Description638
1008
Node: Command Line Options2097
1009
Node: Basic Elements2667
1011
Node: Variables3610
1013
Node: Expressions5470
1014
Node: About Expressions and Special Variables5754
1015
Node: Basic Expressions7495
1016
Node: Relational Expressions10441
1017
Node: Boolean Expressions11451
1018
Node: Precedence12011
1019
Node: Special Expressions13176
1020
Node: Statements14563
1021
Node: Pseudo Statements21192
1022
Node: Functions21845
1023
Node: Math Library Functions27008
1024
Node: Examples27723
1025
Node: Readline and Libedit Options29707
1026
Node: Comparison with Other Implementations30738
1028
Node: Environment Variables37247