views:

633

answers:

7

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?
+16  A: 

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.

mquander
+1 for just using the whole floating point representation. Anything more is going to be truncated, anything less is silly - why introduce an intentional error for absolutely no benefit?
Eclipse
What if the decision to be made is between float and double?
Bill the Lizard
Then almost certainly pick float, because float is pretty precise, but you'll need to analyze the particular usage scenario (in the fashion you demonstrated in your answer) to determine if you actually need the precision of double.
mquander
A: 

It's not fully clear what you are doing. Are you looking up coordinates based on an angle? In this case the accuracy of Pi doesn't matter at all. Important is the size of the lookup table and you will of course precalculate it with the highest possible accuracy. Further accuracy doesn't cost additional execution time besides you are thinking about changing the data type from single to double.

Daniel Brückner
That's exactly what I'm doing. How does the accuracy of Pi not matter?
Kriem
+1  A: 

If you're precalculating it and storing it, you can just use the math library on your system to compute it one time as accurately as possible. A good option is:

double PI = (16.0 * atan(1/5)) - (4.0 * atan(1/239));

That will give you a fairly accurate value for PI which you can compute at startup and reuse as needed. It's difficult to get a more accurate version than that which is easily reusable.

Reed Copsey
Why not just hardcode it as a constant?
mquander
@mquander Just in case it changes =D
Oh, no problem -- just load the constant from an XML configuration file!
mquander
I've always seen apps calculate pi as double PI = atan2(-1, 0), which ideally evaluates to pi exactly. Do you know a reason to go with this over yours or vice versa?
Paul Fisher
If M_PI isn't accurate enough, file a bug report. :)
quinmars
@Paul Fisher: atan2 doesn't exist on some math libraries, particularly some runtimes for embedded devices. Otherwise, no, that's a good option, as well.
Reed Copsey
M_PI actually did have accuracy problems in older versions of Visual C++, which is why I didn't trust it previously. :) Also, it's been dropped from C99 (officially) - see http://www.velocityreviews.com/forums/t520104-how-to-use-pi-in-c99.html
Reed Copsey
A: 

If you are pre-calculating the value make it as accurate as feasible to store but to get the true feel of what accuracy in actually needed you have to look at your formulas and the accuracy of other components and than make sure the accuracy you have for pi matches that for other components. Numerical analysis will give you plaenty of clues.

mfloryan
+6  A: 

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 !

Magnus Skog
+3  A: 

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.

Bill the Lizard
That sounds reasonable.I think you first need to see the biggest number that appears in your calculations and then check the significant digits.
xxxxxxx
A: 

Bible says value of Pi is 3 (link). So of course you should just use that :)

Yogi
Did you even read that link? It pretty clearly debunks your statement.
Iceman
sorryhere is another one:http://gospelofreason.wordpress.com/2007/06/13/god-said-pi-3-stand-by-your-beliefs-dammit/
Yogi
BTW I hope you realize I am just joking.
Yogi