Advertisement

How many digits of PI to use?

Started by October 19, 2000 02:52 PM
22 comments, last by Ridcully 24 years, 2 months ago
Legend goes that for the first rocket launches, computers were stupid and rockets crashed.

Yo dude, I compiled that code for the Gregorian series. That was 15 minutes ago. It still hasn''t finished, and, it''s majorly slowing down my system. Luckily, this is not my home computer. I think maybe I should have fiddled with the accuracy before jumping in.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Single-precision floating point numbers (i.e. C''s "float" type) have 1 sign bit, 8 bits of exponent, and 23 bits of mantissa. Double-precision floating point numbers (i.e. C''s "double" type) have 1 sign bit, 11 bits of exponent, and 52 bits of mantissa.

If you take these limits into account, it''s pretty evident that any precision of Pi beyond those mantissa bits will just be thrown away. If you want to make sure you use the maximum precision allowed without wasting effort on anything beyond it, take your extremely precise definition of Pi, drop it into a float and/or double variable, and dump out the resulting value in both cases. Whatever you get for double is the most significant you will need Pi to be for the foreseeable future.

The precision of floats should only amount to around six significant digits. Doubles will be more of course, but nowhere near 1000.
Advertisement
quote: Original post by capn_midnight

Yo dude, I compiled that code for the Gregorian series. That was 15 minutes ago. It still hasn''t finished, and, it''s majorly slowing down my system. Luckily, this is not my home computer. I think maybe I should have fiddled with the accuracy before jumping in.



Hehe, I tried it with an accuracy of 1.2E49 I thought that it would take a few hrs, I decided to work it out while I was waiting. I timed that loop on my PC and I found that I can get 1 million iterations in 444ms. (Crappy K6-2 300) That means that the loop with 1.2E49 iterations would have taken 1.7E35 years for the loop to finish! That means that our sun would have burned out long before that loop finished

The code I posted above would take about 20 mins on my PC






-- Kazan - Fire Mountain Games --
Well, if you really want exactly the right amount of accuracy for PI, you can ask your computer what it thinks PI is. (by atan2 of -1,0 for example) Once you get that value use the hex dump of the float or double as your constant.

ex:
const long int myPI_S   =  0x40490fdb;const long int myPI_D[] = {0x54442d18, 0x400921fb};#define PI_s (*((float  *)&myPI_S))#define PI_d (*((double *) myPI_D)) 


where PI_s is PI for single precision and PI_d is PI for double precision. I don''t think this particular code will work on non-x86 processors, however.
I think capn_midnight had the best idea: write a short program, put 100 digits or so into the float and/or double, and then print it out. I think you might have to mess with format specifiers to get it to show everything. Then just use that number.
Egads, this is terribly, terribly simple....

Use as many digits as your compiler''s standard headers provide. Why do that work when it''s been done for you?

Or is this simple a joke on me? D''oh!

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
Advertisement
Hehe, if you were in my math class, you would just use 3.14...
farmersckn
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
In the DirectX SDK samples they have this:
#define PI                  3.14159265358979323846f 

So they use 19(?) digits. They wrote the thing, so I would go with them.



"We are the music makers, and the dreamers of the dreams."
- Willy Wonka
Here is the answer everyone has been waiting for (including me):

I just write up a short program that used two values for PI, the really long one Ridcully gave, and a shorter version. I simply compared these two values for equality and printed a message accordingly to see how short I could get the constant value without the two numbers being different. Here are the results:

const double PI = 3.141592653589793;
const float PI = 3.14159265f;

Anything longer than than (in either a const or a #define) is simply overkill.

(You can actually get the float constant one digit shorter without losing any precision by dropping the 5 and rounding the 6 to a 7.)
IronBlayde writes:
quote:
You''re kidding, right? I hope?


Or course I''m NOT kidding...


-------------------------------
I'll screw up whoever screws around with the gamedev forum!

..-=gLaDiAtOr=-..

This topic is closed to new replies.

Advertisement