C# Math.PI Constant

Pi mathematical symbol

Pi is available in the Math.PI constant. Further, pi can be computed with an algorithm. Actually using pi in your program is not your main goal here, but finding ways to acquire its value is useful. Here we look at ways to get pi in the C# programming language.

This C# article shows the Math.PI constant from the .NET Framework. PI equals 3.14159265358979.

Example

Let's begin with a simple example of how you can access the double PI. Include the System namespace and then call the Math.PI composite name. Math.PI here returns a double equal to 3.14.

Program that uses Math.PI [C#]

using System;

class Program
{
    static void Main()
    {
	double pi = Math.PI;
	Console.WriteLine(pi);
    }
}

Output

3.14159265358979

Newton's approximation

Note

Next, let's explore a pi algorithm. Sir Isaac Newton spent a long time calculating pi to 15 decimal places. You can see that the above equation defines half of pi as the sum of a fraction, expanded from 0 to infinity. The result of the formula becomes increasingly accurate the longer you calculate it. The main constraint Newton faced was time and error.

Note: My main constraint would be lack of brainpower.

Pi equation

Calculate pi. I developed this program after researching the problem. This program is basically never useful in a real-world program; it has no advantage over using Math.PI.

Program that computes pi [C#]

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);
	}
    }
}

Output

3.14159265358979000000
3.14159265358979000000
Note

Control flow. 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.

Explanation of F method. 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.

Limitations

Warning

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. To overcome this, you would need a big number class, or a method that simply can find the decimal places one by one.

Double

More on pi

Programming tip

The science fiction movies about calculating pi are not the most interesting material on this subject. You can spend a lot of time just reading about pi. Many of the approximations are listed with detail at the site for Mathematica.

Wolfram MathWorld reference

Summary

The C# programming language

We first looked at the useful Math.PI constant, and then saw a way to calculate the value of pi to a certain accuracy using the C# programming language. This research helps teach us about double precision, the string format patterns for decimal places, and how to translate a math formula into C# code.

Math Class
.NET