Math Methods for Pi

Pi: the ratio of the circumference to the diameter of a circle. The number in decimal form apparently has no end and never repeats. It can only be approximated.

One can approximate to four decimal places (PI=3.1416 ) or to 8 decimal places PI=3.14159265 . The first approximation uses a denominator of 10000 and the second a denominator of 100,000,000

PI ~= 31416 / 10000 = 3.1416

PI ~= 314159265359 / 100000000000 = 3.14159265359

The denominator for the approximation does not have to be a power of 10. 22/7 is a good approximation; the denominator is 7. 355/113 is another good one.

PI ~= 22 / 7 = 3.142857142857

or

PI ~= 355 / 113 = 3.141592920354

355 / 113 is correct to six decimal places. PI can actually be approximated using any number in the denominator, and in general a larger number in the denominator allows you to make a better approximation.

For a computer, it is easist to work in powers of two. Using a denominator of 256 or 65536, the size of byte or word variables, simplifys the calculations required. The best approximations to PI with 65536 as the denominator is,

PI ~= 205888 / 65536 = 3 + 9280 / 65536 = 3.1416015625

which is 8.90891e-06 too high, or

PI ~= 205887/65536 = 3 + 9279/65536 = 3.141586303711

which is 6.349879e-06 too low, and therefore a little bit better. These are not as good an approximation as 355/113, but still good to several decimal places.

With a denominator of 256 the best we can hope for is:

PI ~= 804 / 256=3 + 36 / 256 = 3.140625

The point here is that we can divide by 256 by simply dropping the last byte of our value. We can divide by 65535 by dropping the last two bytes. So, if we have a math library that can multiply two 16 bit integer numbers and return a 32 bit integer result, we can find X*Pi by simply finding (X*3) + (X*9279) / 65535. Trying to find (X*205887)/65535 will not work because 205887 is larger than 16 bits. Trying to find X * (205887/65535) will not work because only integer math is involved; we would effectivly be asking for X * 3.

Of course, you can do the same thing with many other fractions or values having a fractional part.

Also: