π Day

Mathematic geeks of the world celebrate March 14 as “pi day.” The logic is that the date is 3/14, which are the first three digits of the value π, or 3.14159….∞

If you really want to geek out, I can recommend a book, the History of Pi by Petr Beckmann. I don’t consider myself a genius at math, but I enjoyed reading this book.

Due to edicts from the Technology Writing Guild, I must state that π is defined as the ratio of a circle’s diameter to its circumference. The value represents the number of times you can wrap the diameter around the circle, and it’s constant for all circles. It’s also irrational, which means it makes absurd political arguments but it also cannot be expressed as a ratio of two integer values. Further, π is transcendental, which means it cannot be otherwise expressed by using algebra. The digits after the decimal in π never repeat.

As you may guess, it’s possible to calculate the value of π by using a computer. The C language is one of the many tools you can use to do so. Before getting into the techniques, be aware that the value π is available as a constant defined in the math.h header file, M_PI. If you need to use π in your code, just include math.h and use the M_PI definition and you’re good.

As far as calculating the value of π, different programmers have their own techniques. Each is based on one of the many odd methods by which the Mentats and Vulcans calculate π. The method I chose to emulate in my code is called a generalized continuing fraction, illustrated in Figure 1.

Figure 1. One of the weird cascading fraction things used to calculate the value of π.

If you look at the calculation, you see that the numbers cascade in a neat pattern: The numerator is 1, 2, 3, 4 squared and so on. The denominator is 1, 3, 5, 7, 9, and so on.

While the fraction shown in Figure 1 is terrifying, it screams out “recursion!” to me. Having a fraction where the denominator keeps spiraling down and down is the same concept as recursion in a programming language. That, combined with the nifty manner in which the numbers increment, implies that you can craft C language code to calculate the value of π — which is what I did.

The code shown below is my attempt to emulate the continuing fraction (Figure 1) by using recursion in C. The recursive function is p(), which uses two float variables, a and b: a is the left-side, odd-number values shown in Figure 1; b is the right “squared” value.

#include <stdio.h>

float p(float a,float b)
{
    return((b<100.0)?a+(b*b/p(a+2.0,b+1.0)):b*b);
}

int main()
{
    printf("%.7f\n",4.0/p(1.0,1.0));
    return(0);
}

The equation starts with 4.0 at Line 10, inside the printf() statement. Values 1.0 and 1.0 are then sent to p().

Within p(), the return() statement handles the recursive return condition and the division calculation. The return condition is b<100. So the recursion continues as long as value b (on the top in the cascading fraction) is less than 100. Otherwise, the fraction is expressed by using this equation:

a+(b*b/p(a+2.0,b+1.0))

Here you see the value a, b2 and then the bottom part of the fraction is the recursion, calling the p() function again, but with new values: a increased by 2 and b increased by 1.

Here’s the output:

3.1415925

It’s accurate out to six digits. (The constant M_PI is equal to 3.14159265358979323846264338327950288.) That’s about as good as you can get with this code. You can mess with the recursion exit condition, b<100, though eventually recursion is going to overflow the stack.

The internet is brimming with various other examples of calculating π. And I know research teams at universities are constantly searching for that elusive “final digit.” It’s fun and distracting.

Enjoy π day!

4 thoughts on “π Day

  1. The situation with M_PI is rather confusing – I found a couple of links which might be helpful.

    https://stackoverflow.com/questions/29264462/m-pi-not-available-with-gcc-std-c11-but-with-std-gnu11

    https://ubuntuforums.org/showthread.php?t=583094

    I have calculating Pi on my blog to-do list, and when I started researching it I was surprised at how many different methods there were, including a few recently discovered ones. It’s such a basic and ancient field that you would assume it had all been sorted out centuries ago, but that’s not the case.

    The one I was thinking of implementing for a blog post was the Chudnovsky Algorithm:

    https://en.wikipedia.org/wiki/Chudnovsky_algorithm

  2. Good info on M_PI. I’ve gotten in trouble in the past writing about constants and functions that aren’t universally available. That’s one of the issues with C is that so many implementations exist without consistency.

    Let me know if you do you own π code. The one in this Lesson can’t get any more accurate without overflowing.

  3. Google’s banner graphic today is “30th Anniversary of Pi Day”.

    (In the UK we write dates D/M/Y so Pi Day doesn’t work. And we can’t use 3/14 because there isn’t a 14th month, and we can’t use 31/4 because April only has 30 days. Oh well!)

    I was thinking of buying History of Pi but noticed it was written in 1970 and must now be seriously “obsolete”.

  4. I didn’t think of that issue, but thanks for bringing it up. I typically write YYYY/MM/DD because it’s better to sort files that way.

    I would still recommend the book. It’s old, but it’s all mathematical concepts that ring true today. I think you’d enjoy it. At least check it out at a library before you buy.

Leave a Reply