It looks like I’ve missed Pi Day by some fraction of a day, but I can’t help but get a little excited when this special day rolls around — especially if pi turns into pie. And, while I can’t remember much past 3.14159 in my head, I know that calculating pi to some outrageous number of digits can be pretty exciting and that we can do that fairly easily on Unix systems.
Want to calculate pi to 1,000 digits? 314,159 digits? No problem. You can select just how many digits you want to see, plug that number into a calculation that I’m about to share, and … voila! OK, depending on how many digits you’ve selected, there may be quite some time between your hitting the enter key and your shouting "Voila!". But let’s take a look at what I should have explained yesterday.
First, what is pi? I have a bit of a hard time remembering what I learned in junior high school math, but pi is the ratio between the circumference of a circle and its diameter. We celebrate Pi Day because the date (03/14) corresponds to the first three digits. Some people are even suggesting that yesterday was "rounded up Pi Day" because 03/14/16 is like 3.14159 rounded up to the 10,000ths place.
There are several ways to calculate pi. Examples include:
pi = 3 + 4/(2x3x4) - 4/(4x5x6) + 4/(6x7x8) ... pi = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 ...
The more you string this out, the more precise a value you’ll get.
But translating these calculations into Unix commands or using your calculator would undoubtedly be a pain — and might even ruin the spirit of Pi Day for you. Instead, you can use a command called bc that you may or may not have run into in your many excursions into the wonders of Unix.
The bc (basic calculator) command provides a high precision calculator on the command line.
$ echo 11 + 7 | bc 18 $ echo 256 /* 256 | bc 65536 $ echo 921486914689 / 6 | bc 153581152448
Built into most Linux systems and complemented by a math library, bc can make quick work of calculating pi or, as I should say, quick work of entering the command. Ask for pi to 100 decimal places and it will run in a tiny fraction of a second. Ask for a million decimal places and you’re going to have to wait a while.
$ time echo "scale=100; 4*a(1)" | bc -l 3.141592653589793238462643383279502884197169399375105820974944592307/ 8164062862089986280348253421170676 real 0m0.003s user 0m0.000s sys 0m0.000s
Each of these bc commands is first setting the number of decimal places we want to see (with the scale setting), calling in the bc math library (with the -l option), and providing the seed values for going after pi. What isn’t immediately obvious is what a(1) has to do with calculating pi.
The a in this calculation represents the inverse tangent or "arctangent" — yet another way of computing pi.
arctan(x) = x − x3/3 + x5/5 − x7/7 + x9/9 − x11/11 + ...
For anyone who is mathematically inclined, it may be interesting that bc also includes a number of other useful functions.
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
You can also use bc for calculating square roots.
$ echo 'sqrt(16)' | bc 4 $ echo 'sqrt(176)' | bc 13 $ echo 'scale=10; sqrt(176)' | bc 13.2664991614
I’m sorry that I didn’t get this post up in time for the big day but, if you start now, you might have pi calculated to a billion digits by Pi Day of next year.
This article is published as part of the IDG Contributor Network.Want to Join?