Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the accuracy of Pi (π)

Optimizing a game we're developing, we're running into the phase where every CPU cycle won counts. We using radians for position calculations of objects circling around other objects and I want to cut the needless accuracy in my lookup tables. For that, we make heavy use of a predefined Pi. How accurate should this Pi be?

So, my question is:

  • How accurate is accurate enough?
  • Or even better, how to determine the needed accuracy?
like image 630
Kriem Avatar asked May 20 '09 15:05

Kriem


3 Answers

You might as well just make it as accurate as whatever floating-point representation you can store is. It won't take longer to perform calculations using a more accurate floating-point number of the same type.

Accuracy is generally measured as number of significant digits; you'll need to decide for yourself how many digits of accuracy you're interested in. If you use a less accurate value for pi, that value's inaccuracy will propagate to the other calculations it's in.

like image 65
mqp Avatar answered Sep 21 '22 11:09

mqp


As a comparision: I think NASA use pi with 7 decimals accuracy to be able to dock into space.

First of all we need to determine how accurate your position calculations need to be, i.e. how accurate the formulas in your program which depends on pi have to be. We need to start there in order to know how accurate pi you need to achive this.

Once that has been determined, you can probably use more or less straight forward numerical analysis to determine how good accuracy you need for pi. I can help you with that, but I need the position formulas to do that :)

Edit: I suspect that your formulas are linearly dependent on pi, i.e. you aren't using some obscure function f(x,y,z,pi) where pi is squared or similar. In that case the accuracy of your formula is a factor times the pi accuracy, e.g. k*eps(pi). Otherwise it's basically a factor times the derivative of f with respect to pi. Not counting the accuracy of all other parameters f depends on !

Cheers !

like image 23
ralphtheninja Avatar answered Sep 22 '22 11:09

ralphtheninja


It depends on how many significant digits you have in your calculation. Given the formula

C = pi * d

if you want to know how many inches in the circumference of a circle one mile in diameter, you'd need six digits of pi to keep the accuracy you want, since there are 63,360 inches in a mile, and there would be 199,051 inches in the circumference. Since there are six significant digits in the answer, I need six digits of pi to calculate it to the needed accuracy.

3.14 * 63,360 = 198950.4

3.142 * 63,360 = 199077.12

3.1416 * 63,360 = 199051.776

3.14159 * 63,360 = 199051.1424

As you can see, I got the right answer in this case with only 5 digits of pi, but that's not always going to be the case. You need at least as many digits of pi as you have significant digits to ensure you have enough precision.

like image 42
Bill the Lizard Avatar answered Sep 19 '22 11:09

Bill the Lizard