*This second article of the mathematical journey through open source, takes you through the basics of bench calculator.*

Faced with the limitations of the shell command *expr* and other shell constructs, here we are all set to explore the powerful command *bc*. *bc* stands for bench calculator. It is not just a command or tool but a complete language in itself. And its power lies in its arbitrary precision-ness with not only integers but with real number. Wondering what that means? It just means that its computation is mostly not limited by size of the integer or real number types, unlike in most programming languages. Thus, this is more closer to our day to day dealing of mathematics, keeping away the internal details of computer’s precision blah blah. So, let’s get started with the first usual maths. And then we will move onto more involved one with variables, conditionals, and later on with functions and recursion.

**Basic operations**

For integer-only math, you may invoke the bench calculator as *bc*. For a full-fledged real number math, you may invoke it as *bc -l*. Once invoked, it will print a welcome message and then wait for you to just type your math statements and press *Enter* to get your answer. For quitting the bench calculator, you need to press *Ctrl-D* (*^D*), on an empty line. All the basic arithmetic operations: addition (+), subtraction (-), multiplication (*), quotient (/), remainder (%), power (^), brackets (()) are just there – with the C-like precedence & associativity rules. An example with all of them in use:

```
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2 + 2 * 3 - 5 + 21 / 4 * 6 # A basic maths statement
33
(2 ^ 2) ^ 3 # Another one with power & bracket
64
^D
```

Yes, you guessed it right. # starts a one-line comment as in shell, or like // in C++. For a multi-line comment, you may use /* */ as in C/C++. You may want that in case you are writing a complete program in *bc*. Yes, you can even do that. How? You may put your math statements (each one on a line by itself) in a file, say in *prog.bc*, as follows:

```
2 + 2 * 3 - 5 + 21 / 4 * 6 # A basic maths statement
(2 ^ 2) ^ 3 # Another one with power & bracket
quit # This will complete the program and not wait for more input
```

And then execute – yes I mean execute, you do not need to compile it – as follows:

```
$ bc prog.bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
33
64
$
```

Aha! You actually got the welcome message from *bc* and then your results. To avoid the welcome message, you may add *-q* option to *bc*, as follows:

```
$ bc -q prog.bc
33
64
$
```

You may also try out the difference in the output of the same program with a *-l* option, i.e. with real numbers. Then, / would be treated as a complete division, not just a quotient provider. Here’s what you would get:

```
$ bc -ql prog.bc
34.50000000000000000000
64
$
```

**Programming with bc**

As soon as we say programming, the first thing flashing to your mind might be variables. Yes, so they are there. All the variables must start with a small letter alphabet and then may contain numbers or small letter alphabets. Yes, you read it right – only small letter alphabets (a,b,c,…,z). This is because, capital letters (A,B,C,…) are used to represent numbers in other bases greater than 10. Yes, *bc* have support for various bases from 2 to 16, and two variables associated with them: 1) *ibase*: defines the base for input; 2) *obase*: defines the base for output. By default, both are set to 10, as in our day-to-day usual maths. But can be modified, for more fancier base conversions. Here’s a snippet:

```
$ bc -ql
ibase # Show the current input base
10
obase # Show the current output base
10
obase=16 # Set the output base to 16
108 # Input in base 10; Output should be in base 16
6C
obase=10 # Set the output base back to 10
ibase=16 # Set the input base to 16
11 # Input now in base 16; Output should be in base 10
17
ibase=10 # Set the input base to 16. 10 is 16 in input base 16.
ibase=A # Set the input base to 10.
obase=2 # Set the output base to 2, i.e. binary
x = 2 * 5 - 1 # Set the variable x to 9 (input base 10)
x # Display the value of x (in output base 2)
1001
x * 2 # This should display 18 in base 2, but x is still 9
10010
obase=10
x++ # Post increment: Display the current value of x and then increment it
9
x # Display the incremented value
10
--x # Pre decrement: Decrement x and then display its value
9
^D
```

From the demo above, you might have already observed that there is nothing like declaring variable type or so – just assign them using = and then use them. Moreover, *bc* also have basic conditional and loop constructs: *if*, *for*, *while*, *break*, *continue*. And along with are the usual C-like relational (<, <=, >, >=, ==, !=), logical (!, ||, &&), and the operation-assignment (-=, +=, *=, /=, %=, ^=) operators. Note of caution: Their precedence and associativity rules may not be as in C. If you do not understand that, forget about it – just make sure to use brackets for whatever you want to operate first. Here goes two simple programs to demonstrate: 1) Computing the sum of the first *n* numbers (*sum**.bc*); 2) Computing the product of the first *n* numbers, i.e. factorial of *n* (*factorial.bc*)

```
-------------------------------- file: sum.bc -----------------------------
print "Enter a positive number: "
num = read()
sum = 0
current_num = 1
while (current_num <= num)
{
sum += current_num
current_num += 1
}
print "Sum of first ", num, " numbers is: ", sum, "\n"
quit
```

```
-------------------------------- file: factorial.bc -----------------------------
print "Enter a positive number: "
num = read()
product = 1
for (current_num = 1; current_num <= num; current_num += 1)
{
product *= current_num
}
print "Product of first ", num, " numbers is: ", product, "\n"
quit
```

The above programs can be tried out by issuing shell commands *bc -ql sum.bc* and *bc -ql factorial.bc*, respectively. But, what are those two words: *print* & *read*, doing there in the code. What else; they are just two built-in functions, displaying the message to the user and taking a number from the user, respectively. Functions? Yes, *bc* can do functions as well. That’s what would be our next camping. So, right now, just go ahead and try the above programs.

Pingback: Mathematics through the Shell | Playing with Systems

Pingback: Bench Calculator to Program Mathematics | Playing with Systems

Ejikeme PrincelyThis is a very nice article on bench calculator. i like your article.

Anil PugaliaThanks Ejikeme for reading & liking the article.

Vivek SrimathiBrilliant introduction to bench calculator. Very informative too. Thanks for such a step by step introduction to bc.

Anil PugaliaThanks Vivek for reading & appreciating my article.