BC Programming

[Return to Unix Programm]

BC is an arbitrary precision calculator language.

If you need to deal with Big Numbers the Unix BC is the tool. It available on almost all Unix, BSD, and Linux systems by default. It can perform math operations as large as the ram and swap space can hold. I have personaly done calucations as large as 2^100,000,000 in size!
bc provides a somewhat readable C like syntax which is compiled into dc "arbitrary precision postfix calculator".

An example here is a little factorial program.

 * Factorial function implemented by recursion.
define fact(n) {
   if (n <= 1) return (n);
      return (n * fact(n-1));
BC can do some other really cool things since it's interpreted and part of Unix is can be called from shells scripts to perform functions.
In /bin/sh, the following will assign the value of "pi" to the shell variable pi.
pi=$(echo "scale=10; 4*a(1)" | bc -l) 


From: Krishna K
Sent: Tuesday, November 18, 2003 6:56 PM
Subject: bc program

$ cat x.txt

How can I get the sum of column1( of course there is one column in file) in
text file x.txt? and how can I write program to compute sum of column1
on file x.txt?

Thanks in advance


I would use a shell script
awk '{ print "a = a + " $1 "; a" }' x.txt | bc -l | tail -1

BC related post of Church of BSD blog

BC - An Arbitrary Precision Desk-Calculator Language PDF Manual

Unix Man Page on BC

Computation of Bells Numbers using BC

BC number theory programs

GNU bc Manual

Main GNU BC Page

C-BC Mark Hopkins' extended BC language

Some BCMath/PHP number theory programs

Exploring RSA Encryption with BC

Gnu BC Directory of functions and functionality