bc is an arbitrary-precision mathematical calculator program with an input language similar to C. It supports both interactive, command-line user-interface and script processing.
Overview
A typical interactive usage is typing the command <code>bc</code> on a Unix command prompt and entering a mathematical expression, such as , whereupon will be output.
While bc can work with arbitrary precision, it defaults to zero digits after the decimal point, so the expression yields (results are truncated, not rounded). This can surprise new bc users. 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 of Bell Labs as a front end to dc, an arbitrary-precision calculator written by Robert Morris and Cherry. dc performed arbitrary-precision computations specified in reverse Polish notation. bc provided a conventional programming-language interface to the same capability via a simple compiler (a single yacc source file comprising a few hundred lines of code), which converted a C-like syntax into dc notation and piped the results through dc.
In 1991, POSIX rigorously defined and standardized bc. Four implementations of this standard survive today: The first is the traditional Unix implementation, a front-end to dc, which survives in Unix and Plan 9 systems. The second is the free software GNU bc, first released in 1991 by Philip A. Nelson. The GNU implementation has numerous extensions beyond the POSIX standard and is no longer a front-end to dc (it is a bytecode interpreter). The third is a re-implementation by OpenBSD in 2003. The fourth is an independent implementation by Gavin Howard that is included in Android (operating system), FreeBSD as of 13.3-RELEASE, and macOS as of 13.0.
Etymology
The original UNIX version 6 manual does not explain what “bc” stands for. Various sources over the years referred to it as “basic calculator”, “bench calculator” and “binary calculator”.
Implementations
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 (<code>if(cond)...</code>, <code>while(cond)...</code> and <code>for(init;cond;inc)...</code>) from C. Unlike C, an <code>if</code> clause may not be followed by an <code>else</code>.
Functions are defined using a <code>define</code> keyword, and values are returned from them using a <code>return</code> followed by the return value in parentheses. The <code>auto</code> 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 <code>scale</code> variable.
The numeric base of input (in interactive mode), output and program constants may be specified by setting the reserved <code>ibase</code> (input base) and <code>obase</code> (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 <code>/*</code> and <code>*/</code> (start and end comment) symbols.
Mathematical operators
Exactly as C
The following POSIX bc operators behave exactly like their C counterparts:
+ - * /
+= -= *= /=
++ -- < >
<nowiki>== != <= >=</nowiki>
( ) [ ] { }
Similar to C
The modulus operators, <code>%</code> and <code>%=</code> behave exactly like their C counterparts only when the global <code>scale</code> variable is set to 0, i.e. all calculations are integer-only. Otherwise the computation is done with the appropriate scale. <code>a%b</code> is defined as <code>a-(a/b)*b</code>. Examples:
<syntaxhighlight lang="console" highlight="6,8,10">
$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=0; 5%3
2
scale=1; 5%3
.2
scale=20; 5%3
.00000000000000000002
</syntaxhighlight>
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 <code>^</code> operator with negative numbers does not follow the C operator precedence. <code>-2^2</code> 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
The <code>sqrt()</code> function for calculating square roots is POSIX bc's only built-in mathematical function. Other functions are available in an external standard library.
The <code>scale()</code> function for determining the precision (as with the <code>scale</code> variable) of its argument and the <code>length()</code> 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, cosine, arctangent, natural logarithm, the exponential function and the two parameter Bessel function J. Most standard mathematical functions (including the other inverse trigonometric functions) can be constructed using these. See external links for implementations of many other functions.
{| class="wikitable"
|+The bc standard library
!bc command
!Function
!Description
|-
|<code>s(x)</code>
|Sine
|Takes x, an angle in radians
|-
|<code>c(x)</code>
|Cosine
|Takes x, an angle in radians
|-
|<code>a(x)</code>
|Arctangent
|Returns radians
|-
|<code>l(x)</code>
|Natural logarithm
|
|-
|<code>e(x)</code>
|Exponential function
|
|-
|<code>j(n,x)</code>
|Bessel function
|Returns the order-n Bessel function of x.
|}
The -l option changes the scale to 20,
