C# Math.PI

Pi mathematical symbol

Pi is available in the Math.PI constant. Further, pi can be computed with an algorithm. 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.

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.

Using System
C# program that uses Math.PI

using System;

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

Output

3.14159265358979

Newton

Note

Next, we explore a pi algorithm. Sir Isaac Newton spent a long time calculating pi to 15 decimal places. You can see that the equation defines half of pi as the sum of a fraction, expanded from 0 to infinity.

Tip:This method is called Newton's approximation of pi. The C# method further down implements it.

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

Pi equation

Example 2

Method call

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. It shows the algorithm implementation.

C# program that computes 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);
	}
    }
}

Output

3.14159265358979000000
3.14159265358979000000
Note

In this example, the PI method is called. Then PI 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.

MultiplyDivideInt

And:It proceeds until it has been called 60 times, which is an arbitrary limit I imposed.

Next, we display results. 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.

Console.WriteLine

Weakness

Exception

The weakness of this method is primarily in its lack of precision. It calculates many digits but the last six are not filled. They remain zeros. This is due to double's lack of precision.

Tip:To overcome this, you would need a big number class, or a method that simply can find the decimal places one by one.

Double

Also, 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

We first looked at the useful Math.PI constant. We then saw a way to calculate the value of pi. We learned about double precision, the string format patterns for decimal places, and how to translate a math formula into code.


C#: Number: Math