# bc: A Handy Utility

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

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.1415926532On 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.

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.bThen, the output from bc is:

f(150)

5713383956445854590478932865261054003189553578601\ 1264182548375833179829124845398393126574488675311\ 1453771078787468542041626662501986845044663559491\ 9592206657494259209573577892932535729044496247240\ 5416790722118445437122269675520000000000000000000\ 000000000000000000We 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) 184756takes 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] 25We 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 56We 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.)

## Trending Topics

Linux Journal Ceases Publication | Dec 01, 2017 |

So Long, and Thanks for All the Bash | Dec 01, 2017 |

Banana Backups | Nov 21, 2017 |

Zentera Systems, Inc.'s CoIP Security Enclave | Nov 15, 2017 |

Sysadmin 101: Patch Management | Nov 14, 2017 |

pfSense: Not Linux, Not Bad | Nov 13, 2017 |

- Linux Journal Ceases Publication
- Understanding Firewalld in Multi-Zone Configurations
- The Weather Outside Is Frightful (Or Is It?)
- Simple Server Hardening
- Bash Shell Script: Building a Better March Madness Bracket
- IGEL Universal Desktop Converter
- Server Technology's HDOT Alt-Phase Switched POPS PDU
- From vs. to + for Microsoft and Linux
- Non-Linux FOSS: Control Web-Based Music!
- Nventify's Imagizer Cloud Engine

## Comments

## If you are running the

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"

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

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

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