bc: A Handy Utility

December 1st, 1998 by Alasdair McAndrew in

Mr. McAndrew shows us how the bc command can be used for prototyping numerical algorithms.
Your rating: None Average: 5 (7 votes)

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:

2^400
2582249878086908589655919172003011874329705792829\
2235128306593565406476220168411946296453532801378\
31435903171972747493376
scale=50
pi=4*a(1)
e(pi*sqrt(163))
262537412640768743.999999999999250072597198185688\
78393709875517366778
scale=100
l(2)
.693147180559945309417232121458176568075500134360\
2552541206800094933936219696947156058633269964186\
875

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.

scale=10
4*a(1)
3.1415926532
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:

scale=0
920^17%2773
948
.^157%2773
920

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.

Programming

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:
f(150)
5713383956445854590478932865261054003189553578601\
1264182548375833179829124845398393126574488675311\
1453771078787468542041626662501986845044663559491\
9592206657494259209573577892932535729044496247240\
5416790722118445437122269675520000000000000000000\
000000000000000000
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:
b1(20,10)
184756
takes some time to compute. We can, of course, write a much faster program:
define b2(n,k) {
auto temp
temp=1;
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:
h[1]=1
h[2]=1
for (i=3;i<=100;i++)
h[i]=h[i-h[i-1]]+h[i-h[i-2]]
h[10]
6
h[50]
25
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:
scale=0
define sf(n) {
auto sum,s;
sum=1;
s=sqrt(n);
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;
h=(b-a)/(2*n);
sum_even=0;
sum_odd=0;
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:
simpson(0,1,10)
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.)

Conclusion

In my opinion, bc is a real find: it is small, efficient, self-contained and an extremely useful utility. It is not to be considered a replacement for a good fast programming language such as C, C++ or FORTRAN. But as a means for quickly prototyping numerical algorithms before coding them in a high-level language, it is excellent.

Resources

Alasdair McAndrew lives in Melbourne, Australia, with his wife, three young children and a grumpy cat. He is a Senior Lecturer at Victoria University of Technology, where he teaches mathematics and computing. He is an enthusiastic and satisfied user of Linux, and has been since kernel 0.99; currently he is running Linux on both a desktop and a laptop. He enjoys trawling the Internet for Linux software suitable for children, and when he has time, playing the viola da gamba.

__________________________


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

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

If you are running the

On August 18th, 2009 HL (not verified) says:

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.

HL's picture

Clarification about "bc -l"

On August 28th, 2009 HL (not verified) says:

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_functions.

HL's picture

Small notice. When you

On August 17th, 2009 HL (not verified) says:

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

Anonymous's picture

Small error in the code: for

On August 17th, 2009 Anonymous (not verified) says:

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"; .

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.







Read this issue