bc bc [ -lwsqv ] [long-options] [ file … ]
Supports arbitrary precision numbers.
Executes files on the command line, then reads from the standard input and processes interactively.
--mathlib |
Suggestion: In .profile set up alias bc='/usr/bin/bc -q ~/.bin/bc.ini' and
create ~/.bin/bc.ini containing :scale=2
then echo 5/6|bc will output .83
length: number of significant digits
scale : number of digits retained after the dot as a result of a calculation. Default:0̸!1.23456 has a length of 6 and scale of 5. 1234.567 has a length of 7 and a scale of 3.
9,A-F. (NOT lowercase, which are variables).
ibase. (i.e. A = 10̸.)ibase, are assigned the value of ibase-1 to each digit,ibase=10̸, num=FF8CB assigns 99899 to num.
a, f, x1, y, index23, loop_counter, x
[ ].
scale defines how some operations use digits after the dot. Default 0̸. scale, the numbers and the operation performed.scale of the maximum scale of the expressions involved, unless specifically mentioned.1.234 in the asignment has a scale of 3 so result has scale of 3.scale=2 a=1.234 a 1.234
last has the value of the last number output.
Some implementations also permit using dot (.)
(extension)
history size of readline history, -1 max, (not retained across sesssions.)
ibase and obase base for input and output. Default base 10, maximun ibase 16.obase=F+1 always sets obase to 16 (i.e. hexadecimal)ibase=A always sets ibase to 10.ibase is 16, ibase=1016 keeps ibase to 16 ! ibase is 2, ibase=102 keeps ibase to 2 !obase is not synchronized with ibase, Example: obase=2;binary=9;binary displays 1001 !
For obases greater than 16, a multi-digit, base 10 format is used.
ibase=F+1 obase=A FFFF 65535 obase=64 FFFF 06 55 35 ibase=A 99 01 35 1*6410 + 3510 64*64 01 00 00 1*64*6410
seed number for example 7.072965818657960876747883483892103789226398317430339722953342321756\ 85465614416059257569049933955795950168976560235023498535156250
/* and */ surround a comment, may start anywhere,
acts as a single space in the input,
delimit other input items, can be multi-line. # begins a comment which continues to the end of line.
- expr |
Relational expressions may appear in an expression.
(POSIX bc requires that relational expressions are used only in if, while, and for statements and that only one relational test may be done in them.)
| Relational operators | |||||||||||||||||||||
result is 1 (true) if
expr1 < expr2 | |||||||||||||||||||||
Expression precedence (highest to lowest)
++ -- |
& | ^ && || << >> &= |= ^= &&= ||= <<= >>= ?:Are not available. Use easyOnLineConverter.com
appears as name(parameters).
read ( ) | ||||||||||||||||||||
User defined functions provide a method of defining a computation that can be executed later which return a value to the caller.
A function definition :
define name ( [parameter[, …] ] ) { 
[ auto name [ , … ]] ; statement_list }
A function call:
name(parameters).
Parameters can be numbers or arrays. Numbers are call_by value (i.e. cannot be changed within the function).
Arrays are only call_by variable. Arrays are specified in the parameter definition by the notation name[].
In the function call, parameters are full expressions for number parameters. The same notation is
used for passing arrays as for defining array parameters. The named array is passed by variable to the function.
Variables are global (i.e. function B can access variables in the main as well as in function A) unless included in
auto list.
Auto variables have their current values pushed onto a stack at the start of the function, initialized to zero
and used throughout the execution of the function.
At function exit, these variables are popped so that the original value (at the time of
the function call) of these variables are restored.
( The parameters are auto variables that are initialized to a value provided in the function call.)
Auto variables are sub-call accessable, i.e. if function A calls function B, B may access function A's auto variables, unless
function B has declared them auto variables.
Constants in the function body will be converted using the value of ibase at the time of the call.
Changes to ibase will be ignored during the execution of the function except for read(), which
will uses the current value of ibase for conversion of numbers.
Function definitions are "dynamic" i.e. a function is undefined until a definition is encountered (aka no forward referencing).
That definition is used until another definition function for name is encountered.
\ continutes a statment. expression |
| |||||||||||||||||||||||||||||||||||
Due to the fact that auto variables and parameters are pushed onto a stack, bc supports recursive functions.
--mathlib, a math library is loaded and
the default scale is set to 20. s (r) sine of r, r is in radians. c (r) cosine of r, r is in radians. a (x) arctangent of x, arctangent returns radians. l (x) natural logarithm of x. e (x) exponential function of raising e to the value x. j (n,x) bessel function of integer order n of x. $pi using acrtan of 1. pi=$(echo "4*a(1)" | /usr/bin/bc -l) # i.e. arcTan(1 radian) π/4 echo $pi 3.14159265358979323844
The exponential function used in the math library, written in POSIX bc.
scale = 20
/* Uses the fact that e^x = (e^(x/2))^2
When x is small enough, we use the series:
e^x = 1 + x + x^2/2! + x^3/3! + ...
*/ define e(x) {
auto a, d, e, f, i, m, v, z
/* Check the sign of x. */
if (x<0) {
m = 1
x = -x
}
/* Precondition x. */
z = scale;
scale = 4 + z + .44*x;
while (x > 1) {
f += 1;
x /= 2;
}
/* Initialize the variables. */
v = 1+x
a = x
d = 1
for (i=2; 1; i++) {
e = (a *= x) / (d *= i)
if (e == 0) {
if (f>0) while (f--) v = v*v;
scale = z
if (m) return (1/v);
return (v/1);
}
v += e
}
}
scale=2
print "\nCheck book program!\n"
print " Remember, deposits are negative transactions.\n"
print " Exit by a 0 transaction.\n\n"
print "Initial balance? "; bal = read()
bal /= 1
print "\n"
while (1) {
"current balance = "; bal
"transaction? "; trans = read()
if (trans == 0) break;
bal -= trans
bal /= 1
}
quit
Definition of the recursive factorial function.
define f (x) {
if (x <= 1) return (1);
return (f(x-1) * x);
}
a=(1/3)^-1
scale=4
a
3.0003
scale=5
a
3.00003
history is the number of lines of
history retained. The default, -1 unlimited, 0 disables the history.
A single process which parses and runs a byte code translation of the program. -c outputs the byte code, for debugging the parser and preparing the math library. | |||||||||||||
LANG | not conform POSIX processing of the LANG environment variable and all environment
variables starting with LC_. | ||||||||||||
| names | Multi-character names start with a letter and may contain letters, numbers and the underscore character. Traditional and POSIX bc have single letter names for functions, variables and arrays. | ||||||||||||
| Strings | Strings are not allowed to contain NUL characters. POSIX: all characters must be included in strings. | ||||||||||||
last | Value of the last output.
Some implementations use the period (.) >>> 9-3 6 >>> a=8-6 >>> . 6POSIX bc does not have a last variable.
| ||||||||||||
| comparisons |
POSIX allows comparisons only in the if and while
statements, and the second expression of the for statement. Only one relational operation is allowed in each of those statements. | ||||||||||||
if statement else clause POSIX bc does not have an else clause.
| |||||||||||||
INT signal (usually generated by the control-C character from the terminal)
will cause execution of the current execution block to be interrupted. ^C interrupt (type "quit" to exit
A "runtime" error indicating which function was interrupted is output. ready for more input will be output.INT terminates .
SIGQUIT (usually generated by the control-\ character from the terminal) outputs ^C ready for more input,
however when the next new line is entered bc exits with a retur code of
limits statment:
On MacBookPro OS version sonma
Umits
BC_LONG_BIT = 64
BC_BASE_DIGS = 9
BC_BASE_POW = 1,000,000,000
BC_OVERFLOW_MAX = 18,446,744,073,709,551,615 (0xFFFFFFFF FFFFFFFF)
BC_BASE_MAX = 1,000,000,000
BC_DIM_MAX = 18446744073709551614 (0xFFFFFFFFFFFFFFFe)
BC_SCALE_MAX = 18446744073709551614
BC_STRING_MAX = 18446744073709551614
BC_NAME_MAX = 18446744073709551614
BC_NUM_MAX = 18446744073709551614
BC_RAND_MAX = 18446744073709551615
MAX Exponent = 18446744073709551615
Number of vars = 18446744073709551614
BC_BASE_MAX |
$POSIXLY_CORRECT |
/usr/local/lib/libmath.b. may be /lib/libmath.b.)