Overview
A typical interactive usage is typing the commandbc
on a Unix command prompt and entering a mathematical expression, such as , whereupon will be output. While bc can work with arbitrary precision, it actually defaults to zero digits after the decimal point, so the expression yields (results are truncated, not rounded). This can surprise new bc users unaware of this fact. The option to bc sets the default ''scale'' (digits after the decimal point) to 20 and adds several additional mathematical functions to the language.
History
bc first appeared in Version 6 Unix in 1975. It was written by Lorinda Cherry ofImplementations
POSIX bc
The POSIX standardized bc language is traditionally written as a program in the dc programming language to provide a higher level of access to the features of the dc language without the complexities of dc's terse syntax. In this form, the bc language contains single-letter variable, array and function names and most standard arithmetic operators, as well as the familiar control-flow constructs (if(cond)...
, while(cond)...
and for(init;cond;inc)...
) from C. Unlike C, an if
clause may not be followed by an else
.
Functions are defined using a define
keyword, and values are returned from them using a return
followed by the return value in parentheses. The auto
keyword (optional in C) is used to declare a variable as local to a function.
All numbers and variable contents are arbitrary-precision numbers whose precision (in decimal places) is determined by the global scale
variable.
The numeric base of input (in interactive mode), output and program constants may be specified by setting the reserved ibase
(input base) and obase
(output base) variables.
Output is generated by deliberately not assigning the result of a calculation to a variable.
Comments may be added to bc code by use of the C /*
and */
(start and end comment) symbols.
Mathematical operators
=Exactly as C
= The following POSIX bc operators behave exactly like their C counterparts: + - * / += -= *= /= ++ -- < >=Similar to C
= The modulus operators,%
and %=
behave exactly like their C counterparts only when the global scale
variable is set to 0, i.e. all calculations are integer-only. Otherwise the computation is done with the appropriate scale. a%b
is defined as a-(a/b)*b
. Examples:
=Conflicting with C
= The operators ^ ^= superficially resemble the C bitwise exclusive-or operators, but are in fact the bc integer exponentiation operators. Of particular note, the use of the^
operator with negative numbers does not follow the C operator precedence. -2^2
gives the answer of 4 under bc rather than −4.
="Missing" operators relative to C
= The bitwise, Boolean and conditional operators: & , ^ && , , &= , = ^= &&= , , = << >> <<= >>= ?: are not available in POSIX bc.Built-in functions
Thesqrt()
function for calculating square roots is POSIX bc's only built-in mathematical function. Other functions are available in an external standard library.
The scale()
function for determining the precision (as with the scale
variable) of its argument and the length()
function for determining the number of significant decimal digits in its argument are also built-in.
Standard library functions
bc's standard math library (defined with the -l option) contains functions for calculating sine,bc -l
and then the command print 3%2
outputs 0. But writing scale=0
after bc -l
and then the command print 3%2
will output 1.
Plan 9 bc
Plan 9 bc is identical to POSIX bc but for an additionalprint
statement.
GNU bc
GNU bc derives from the POSIX standard and includes many extensions. It is entirely separate from dc-based implementations of the POSIX standard and is instead written in C. Nevertheless, it is fully backwards compatible as all POSIX bc programs will run unmodified as GNU bc programs. GNU bc variables, arrays and function names may contain more than one character, some more operators have been included from C, and notably, anif
clause may be followed by an else
.
Output is achieved either by deliberately not assigning a result of a calculation to a variable (the POSIX way) or by using the added print
statement.
Furthermore, a read
statement allows the interactive input of a number into a running calculation.
In addition to C-style comments, a #
character will cause everything after it until the next new-line to be ignored.
The value of the last calculation is always stored within the additional built-in last
variable.
Extra operators
The following logical operators are additional to those in POSIX bc: && , , ! They are available for use in conditional statements (such as within anif
statement). Note, however, that there are still no equivalent bitwise or assignment operations.
Functions
All functions available in GNU bc are inherited from POSIX. No further functions are provided as standard with the GNU distribution.Example code
Since the bc^
operator only allows an integer power to its right, one of the first functions a bc user might write is a power function with a floating-point exponent. Both of the below assume the standard library has been included:
A "power" function in POSIX bc
Calculating π to 10000 digits
Calculate pi using the builtin arctangent function, :A translated C function
Because the syntax of bc is similar to that of C, published numerical functions written in C can often be translated into bc quite easily, which immediately provides the arbitrary precision of bc. For example, in the Journal of Statistical Software (July 2004, Volume 11, Issue 5), George Marsaglia published the following C code for the cumulative normal distribution:Using bc in shell scripts
bc can be used non-interactively, with input through a pipe. This is useful inside shell scripts. For example:See also
* dc programming language * C programming language * hoc programming languageReferences
*External links