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?
Kriem
  • 8,666
  • 16
  • 72
  • 120
  • 12
    I don't understand how the accuracy of PI will affect computation cycles, any operation with a double value will take the same length of time, regardless of how precise it is or isn't. What am I missing? Thanks. – Binary Worrier May 20 '09 at 16:04
  • 1
    I think maybe the questioner didn't understand that, and assumed that a really accurate value took longer to multiply or something. – mqp May 20 '09 at 16:06
  • 2
    @ mquander - You're right. I had no idea that it works that way. – Kriem May 20 '09 at 16:07
  • 1
    @Kriem: Remember that floating-point types don't bear any natural affinity to base 10. So if you for example defined a "less accurate" float pi = 3.14; then in binary that value (3.140000000...) is not in any sense a small "compact representation" of the number. It might not even be an exact value, because it might be a repeating decimal in binary. – mqp May 20 '09 at 16:18
  • 2
    It could still be an issue if you're deciding between storing pi as float or double. – Bill the Lizard May 20 '09 at 16:30
  • 1
    At least on x86, floats and doubles are both converted to the same even higher precision cpu-internal value before doing any computations with them, so that only matters for memory usage. And since you're only going to have one pi, it doesn't even matter much then :) – bdonlan May 20 '09 at 17:45

7 Answers7

18

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.

mqp
  • 70,359
  • 14
  • 95
  • 123
  • 3
    +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 May 20 '09 at 16:15
  • What if the decision to be made is between float and double? – Bill the Lizard May 20 '09 at 16:54
  • 1
    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. – mqp May 20 '09 at 17:20
6

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 !

rtn
  • 127,556
  • 20
  • 111
  • 121
3

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
  • 398,270
  • 210
  • 566
  • 880
  • 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 Sep 29 '09 at 05:57
1

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
  • 554,122
  • 78
  • 1,158
  • 1,373
  • @mquander Just in case it changes =D – DevinB May 20 '09 at 16:10
  • 3
    Oh, no problem -- just load the constant from an XML configuration file! – mqp May 20 '09 at 16:10
  • 2
    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 May 20 '09 at 16:16
  • 3
    If M_PI isn't accurate enough, file a bug report. :) – quinmars May 20 '09 at 16:52
  • @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 May 20 '09 at 17:03
  • 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 May 20 '09 at 17:14
0

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
  • 59,031
  • 16
  • 99
  • 143
0

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
  • 7,667
  • 4
  • 31
  • 44
-1

new formula for pi

Pi=2.m.sin(90/m)

Pi=3.m.sin(60/m)

Pi=4.m.sin(45/m)

Pi=5.m.sin(36/m)

For millions across the value of PI free calculator XP,XM,….. recommend m=1.0E+10000000 to the success of the calculator you need to install netframework2.0

http://harry-j-smith-memorial.com/index.html

arrowd
  • 33,231
  • 8
  • 79
  • 110