The Complete Magazine on Open Source

Use the Bench Calculator to Program Mathematics

442 0


This third article in the mathematical journey through open source takes you through the functional power of the bench calculator.

After going through basic programming on the bench calculator, it’s time to explore its functional power. As mentioned earlier, you can do functions with the bench calculator (BC). Unlike C, it has built-in functions,  where as standard math functions, and user-defined functions are similar to C.

Built-in functions
The BC’s built-in functions are:
length(expr) returns the number of significant digits in expr.
read() reads a number from standard input in the base dictated by the ibase variable.
scale (expr) returns the number of digits after the decimal point in expr.
sqrt(expr) returns the positive square root of expr, given that expr is non-negative.

Here’s a sample execution of the above functions:

<pre>$ bc -ql
length(000023.450) # Number of significant digits
scale(000023.450) # Number of digits after the decimal
sqrt(2) # Square root of 2
sqrt(-1) # Square root of -1 is an error
Runtime error (func=(main), adr=4): Square root of a negative number
ibase=2 # Changing the input base to 2
x=read() # Wait to read the input in binary and then display
1100 # This is the input
x # Display the read value in the default output base 10
quit # Get out</pre>

Wait! Are those all the built-in functions? What about the previously used print? In BC, print is not a function; the missing () indicates it is actually a statement, like if, for, … and its syntax is print <list>, where <list> is a comma-separated list of strings and expressions. (If you have not yet got the hang of this word expression, it is a statement of numbers and variables operated with the various operators and functions.)

Standard math functions
When BC is invoked with the -l option, the math library is also loaded, making available for use the following six math functions:

  • s(x) – returns sine of x (x is radians)
  • c(x) – returns cosine of x (x is radians)
  • a(x) – returns arctangent (in radians) of x
  • l(x) – returns the natural logarithm (base e) of x
  • e(x) – returns the value of e raised to the power of x
  • j(n, x) – Bessel function of integer order n of x

All these functions operate with the scale dictated by the built-in variable scale, which is by default, set to 20. Here’s a sample execution:


$ bc -ql
scale # Show the current scale
pi=4*a(1) # Calculate pi as tan-1(1) is pi / 4
pi # Show the value approx. to 20 decimals
s(pi/3) # Calculate sine of 60° - should sqrt(3)/2
sqrt(3)/2 # value for comparison – note the approx. error
c(pi/3)  # Calculate sine of 60° - should be 0.5
l(1) # log(1)
e(1) # Value of e1 approx. to 20 decimals

If all this sounds too geeky and mathematical, let’s forget it, and do some simple stuff; let’s write simple user-defined functions. User-defined functions Here is how you write a user-defined function (to add two numbers) in BC:

$ bc -ql
define add(x, y) {
	return (x + y)
add(3, add(4, 5)) # Lets add 3 with the sum of 4 & 5

Given that, the factorial code from our previous learnings can be converted into a function as follows (say, in functions.bc):

define factorial(n) {
        product = 1
        for (current_num = 1; current_num <= n; current_num += 1)
                product *= current_num
        return product

And then, you can use that function, as follows:

$ bc -ql functions.bc # Load the functions while invoking bc
factorial(10) # Compute the factorial of 10

As you now have factorial, you can even calculate the series of e, i.e., 1 + 1/1! + 1/2! + …, say up to 1/20 for a good enough approximation. Here’s how it would go:

$ bc -ql functions.bc
for (i = 1; i <= 20; i++)
	exp += (1/factorial(i))
exp # Display the computed value of e
e(1) # Compare with the standard math function

And, as in C, if you need a function only to do actions and not return anything, void is the way:

$ bc -ql
define void designer_print(v) {
print "---{", v, "}---"
designer_print(100) # Print 100 with the designs

Having covered the fundamentals of these functions in BC, we will dive into its recursive functional power in the next issue.