[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: In honor of Pi Day (3.14), approximate pi in C#

[In honor of Pi Day (3.14), approximate pi in C#]

There are several ways to calculate π. Many use an infinite series such as the following one.

This isn't necessarily the best series to use, but it is relatively simple.

When the program starts, it uses the following code to display the value of π stored in the Math.PI constant.

private void Form1_Load(object sender, EventArgs e) { txtActual.Text = Math.PI.ToString(); }

If you enter a number of terms and click Calculate, the following code uses the infinite series to approximate π.

// Calculate Pi. private void btnCalculate_Click(object sender, EventArgs e) { txtCalculated.Clear(); txtCalculated.Refresh(); double pi_over_4 = 0; int num_terms = int.Parse(txtNumTerms.Text); double sign = 1; for (int term = 0; term < num_terms; term++) { Console.WriteLine(sign + " / " + (term * 2 + 1) + " = " + (1.0 / (term * 2 + 1))); pi_over_4 += sign / (term * 2 + 1); sign *= -1; } // Display the result. double pi = 4 * pi_over_4; txtCalculated.Text = pi.ToString(); }

This code simply adds up the desired number of terms, multiplies the result by 4, and displays the answer.

The code itself is pretty simple, but it's interesting to ask how quickly the result approaches π. The code alternately adds and subtracts increasingly smaller values, so the approximation alternates between being too high and too low. Because each term moves the guess from too high to too low or vice versa, the error at any step must be smaller than the value of the next term.

For example, suppose you've added some terms to the sum so the next term you will add is 1 / 21. At that point, you know the approximation of the sum must be within 1 / 21 of the correct value. After you multiply the sum by 4 to get an approximation of π, the approximation must be within 4 / 21 of the true value.

Now suppose you want to approximate π to within 0.01. To do that, you need to find the term where 4 / (2 * term + 1) < 0.01. Rearranging a bit gives 2 * term + 1 > 4 / 0.01 so term > (4 / 0.01 - 1) / 2 = (400 - 1) / 2 = 399 / 2 = 199.5. That means the approximation should be within 0.01 of the true value for π if we take at least 200 terms. The following text shows the "true" value of π and the approximation given by the program with 200 terms.

"True" Value: 3.14159265358979
Approximation: 3.13659268483882
Difference:-0.0049999687509698632

So the approximation is within 0.01 of correct as desired.

What all this means is that the approximation approaches π relatively slowly. To get the value within 0.001 of the true value, you need to use 2,000 terms. To get it within 0.0001 of correct, you need to use 20,000 terms.

Of course the program is pretty fast so you can use a lot of terms. 20 million terms takes less than a second on my computer and gives an approximation that's within about 5 × 10-8 of the correct value, which is pretty good. If you use more terms, rounding errors start to add up so the new terms don't help as much as you might like.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.