Dot Net Perls

Pi - C#

by Sam Allen

Problem

You want to see how pi can be computed in a C# program. Actually using pi in your program is not your main goal here, but translating Newton's formula into a C# program can expand your knowledge of both.

Solution: Newton's approximation in C#

Sir Isaac Newton spent months, if not years, calculating pi to 15 decimal places. The formula he devised looks like this:

You can see that the above equation defines half of pi as the sum of a fraction, expanded from 0 to infinity. [Numerical Approximations of Pi - Wikipedia]

The result of the formula becomes increasingly accurate the longer you calculate it. The main constraint Newton faced was time and error.

Figure 1: calculate pi in C#

I developed this program after researching the problem for several hours. This program is basically never useful to use in a real-world program. It has no advantage over using Math.PI.

using System;

class Program
{
    static void Main()
    {
        // Get PI from methods shown here
        double d = PI();
        Console.WriteLine("{0:N20}",
            d);

        // Get PI from the .NET Math class constant
        double d2 = Math.PI;
        Console.WriteLine("{0:N20}",
            d2);
    }

    static double PI()
    {
        // Returns PI
        return 2 * F(1);
    }

    static double F(int i)
    {
        // Receives the call number
        if (i > 60)
        {
            // Stop after 60 calls
            return i;
        }
        else
        {
            // Return the running total with the new fraction added
            return 1 + (i / (1 + (2.0 * i))) * F(i + 1);
        }
    }
}

The path the program takes is first the pi method is called. It calls the F method and multiplies the final result by 2. Here we calculate half of pi.

The F method receives an integer that corresponds to k in Newton's formula. It proceeds until it has been called 60 times, which is an arbitrary limit I imposed.

In Main, these methods are called and the result is written to the screen up to 20 digits. Finally, the const Math.PI is written in the same way to the screen.

Information: limitations of the method

The weakness of this method is primarily in its lack of precision. It calculates to 3.14159265358979000000, but the last 6 digits are not filled in. This is due to double's lack of precision. [Type: System.Double - columbia.edu]

To overcome the lack of precision, you would need a BigNum class, or a method that simply can find the decimal places one by one. There are methods that are successful here.

Information: how the calculation converges on pi

As I said, this method becomes closer and closer to pi the longer it runs, until it reaches the limit of precision on the numeric type. This chart shows how the method becomes closer to 3.14 as it is called 1 time to called 60 times.

We see that the result converges very rapidly up to 10 or 13 recursive calls, and then slowly becomes ever more accurate.

Information: more on pi

The sci-fi movies about calculating pi are not the most interesting material on this subject. I spent several hours just reading about pi. Many of the approximations are listed with detail at the site for Mathematica. [Pi Formulas - Wolfram MathWorld]

Summary: Newton's approximation of pi in C#

This research helped me learn about double precision, the string format patterns for decimal places, and how to translate a math formula into C# code.

Additionally, I learned how to use Microsoft Word to write equations, and found lots of interesting history about mathematics. However, the code as it appears here has no use in programs other than for learning or demonstration.

Dot Net Perls
About
Sitemap
Algorithms
Alphanumeric Sorting
Anagram Method
A-Star Pathfinding
BinarySearch Algorithm on List
Directed Acyclic Word Graph
New
Occurrence Count of String
StartsWith String Examples
© 2008 Sam Allen. All rights reserved.