bc: A Handy Utility

Mr. McAndrew shows us how the bc command can be used for prototyping numerical algorithms.

Linux, as with almost all UNIX systems, contains a vast number of little utilities tucked away in such places as /usr/bin and /usr/local/bin. One of these is the GNU utility bc.

bc is an arbitrary precision calculator language. It can perform arithmetic (both integer and real) to arbitrary precision, and it supports simple programming. It is started by the command:

bc -l files

The optional -l flag loads a mathematics library, and files (also optional) is a list of files containing bc commands. There are some other flags, but they do not greatly change the functionality. The mathematics library makes the following functions available to bc:

  • s(x): the sine of x in radians

  • c(x): the cosine of x in radians

  • a(x): the inverse tangent of x (The result is returned in radians.)

  • l(x): the natural logarithm of x

  • e(x): the exponential function ex

  • j(n,x): the Bessel function of order n of x

I used version 1.04 of GNU bc to generate all the examples below. Other versions of bc may be restricted in their capabilities.

Basic Usage

Let's look at a few examples of bc in action, assuming it has been started with the -l flag:


The value scale is one of bc's internal variables: it gives the number of figures to the right of the decimal point. Other versions of bc do not allow arbitrary values for scale. We could easily use 1000 instead of 10 in the following example, if we wanted more decimal places.

On my computer, a Pentium 133, calculating pi to 1000 places takes about one and a half minutes to complete.

bc provides most of the standard arithmetic operations:


The period (.) is shorthand for the last result. The percentage sign % is the remainder function; it produces the standard integer remainder if scale is set to zero. When bc is invoked with the -l flag, the value of scale is set to 20.


Statements in bc are computed as quickly as possible. Thus, when using bc interactively, as shown above, statements are evaluated as soon as they are typed. A program in bc is simply a list of statements to be evaluated. The programming language provides loops, branches and recursion, and its syntax is similar to that of C. A simple example (from the man page) is the factorial function:

define f(x) {
if (x <= 1) return (1);
return (x*f(x-1));

It is convenient to place such definitions in a file (called, say things.b), and read them into bc with the command:

bc -l things.b
Then, the output from bc is:
We can easily write little programs to calculate binomial coefficients:
define b1(n,k) {
if (k==0 || k==n) return (1);
return (b1(n-1,k)+b1(n-1,k-1));
This is a rather inefficient program. The solution:
takes some time to compute. We can, of course, write a much faster program:
define b2(n,k) {
auto temp
if (k==0) return (1);
for(i=1; i<=k; i++) temp=temp*(n+1-i)/i;
return (temp);
Here auto is a list of variables which are local to the current program. It is instructive to play with these two implementations of computing binomial coefficients: b2 gives the result almost immediately, whereas b1 is very slow for all but very small values of n and k. bc also supports arrays; here we use arrays to compute the first 100 values of Hofstadter's chaotic function:
for (i=3;i<=100;i++)
We can then print out all these values:
for (i=1; i<=100; i++) {
print h[i],"       ";
if (i%10==0) print "\n;"
1    1    2    3    3    4    5    5    6    6
6    8    8    8    10   9    10   11   11   12
12   12   12   16   14   14   16   16   16   16
20   17   17   20   21   19   20   22   21   22
23   23   24   24   24   24   24   32   24   25
30   28   26   30   30   28   32   30   32   32
32   32   40   33   31   38   35   33   39   40
37   38   40   39   40   39   42   40   41   43
44   43   43   46   44   45   47   47   46   48
48   48   48   48   48   64   41   52   54   56
We see that bc is particularly well suited to prototyping simple numerical algorithms. To give two final examples: computing amicable numbers, and Simpson's rule for numerical integration. First, two integers are amicable if each is equal to the sum of the divisors of the other:
define sf(n) {
auto sum,s;
for (i=2;i<=s;i++)
  if (n%i==0) sum=sum+i+n/i;
if (s*s==n) sum=sum-s;
return (sum);
define amicable(m) {
for (j=1;j<=m;j++)
  if (sf(sf(j))==j && sf(j)!=j && j<sf(j)) print
        j,"        ",sf(j),"\n";
print "Done.\n";
Then, the command amicable(2000) will list all pairs of amicable numbers, at least one of which is below 2000.

Second, Simpson's rule for numerical integration:

define simpson(a,b,n) {
auto h,sum_even,sum_odd;
for (i=1;i<=n;i++) sum_odd=sum_odd+f(a+(2*i-1)*h);
for(i=1;i<n;i++) sum_even=sum_even+f(a+2*i*h);
return ((f(a)+f(b)+4*sum_odd+2*sum_even)*h/3);

Defining a function f(x) by, say:

define f(x) {
return (e(-(x^2)));
and then the command:
returns the result of Simpson's rule for the integral of f(x) between 0 and 1, using 20=2*10 subintervals. (The result is .74682418387591474980, which is correct to six decimal places.)



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

If you are running the

HL's picture

If you are running the "Hofstadter's chaotic function" with the remainder part, please notice that the BC-utility has a bug with the -l option. It works fine if you use only "bc"-command, instead of "bc -l".

Tested on Ubuntu 9.04.

Clarification about "bc -l"

HL's picture

If you try the code with "bc -l", please note that you must set "scale=0". Otherwise, things such as modulo may seem to work unexpectedly. More here, accessed 28th August 2009 10:14, http://en.wikipedia.org/wiki/Bc_programming_language#Standard_library_fu....

Small notice. When you

HL's picture

Small notice.

When you copy-paste the amicable-code, remove the enter like:

define amicable(m) {
for (j=1;j<=m;j++)
if (sf(sf(j))==j && sf(j)!=j && j

Small error in the code: for

Anonymous's picture

Small error in the code:

for (i=1; i<=100; i++) {
print h[i]," ";
if (i%10==0) print "\n;"

The "\n;" should be "\n"; .