## Wednesday, October 04, 2006

### Taking Roots With the Unix Basic Calculator

Previously, we covered the Unix basic calculator (bc) program.  In that post, I mentioned that bc had the sqrt() function for finding square roots, but that I would later show how to calculate other roots, using the exponent and log functions, e() and l().

For the background on this, you can see my math blog post on logrithms and exponentials, part 2.

Finding roots of numbers is used in my post on using awk to find the average annual interest rate.  The example in that post was to calculate the average annual interest rate, given that our account increased from \$4,000 to \$5,200 in 6 years.

We will now do the example from that post, using bc:

[551]-> bc -l
e( l(5200/4000)/6)
1.04469750792327720815
qu

so we get the answer of 4.47%/year.

Notice that we wanted to raise (5200/4000) to the 1/6th power, which means we want to find the 6th root.

Unfortunately, unlike awk, bc can't handle non-integer exponents.  So, we need to use logrithms to find roots greater than 2.