It is currently Mon Apr 24, 2017 12:24 am

All times are UTC - 5 hours

 Page 1 of 1 [ 1 post ] 
Author Message
 Post subject: PI In Programming.
PostPosted: Mon Oct 17, 2011 4:58 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
Im using a 32bit system, and using windows with vs 2010c++.

I did a bit of research on the float and double data types and how their bit representation are stored in memory in binary representation.

I noticed a unique behavoir with the compiler and the value of pi with its precision. I will be working with doubles here for 15 bit precision and floats will be found below.

~= Approximate True Value

PI-Actual ~= 3.1415926535897932
PI-Stored ~= 3.1415926535897931

If I set PI equal to the first value, wether I use [ double dPI = ...; or use #define PI .... ]
and I use the printf fucntion with %1.16f It displays the second value regardless.

The actual 16 digit after the decimal is showed on the screen however if I append a few more digits to the end and increase %f to say 20 instead of 16 the 1 is their and rest are 0s so any digit after the 1 is truncated. And the 1, the least significant digit is a round off error. So it could be safe to say that,
PI = 3.141592653589793_2 means nothing towards the compiler it is virtually the same as 3.1...._1

The question comes in mind how much precision do you want PI to have, as well as the over head of using floats vs doubles.

Now when I do the same as above but use floats PI now equals this vs double
double: 3.1415926535897931
float: 3.1415927410125732

which is an interesting observation.

This is the value of PI Marek Is Using Before He updates the mathLib and creates a class and uses trig functions to evaluate PI

the last 2 digits the two least most significant the ...23f doesn't even come into play, atleast with displaying values on the screen using printf or cout functions, don't know if they hold values in their perspective variables when doing operations on them.

I just thought that this would be an interesting read. This same thing happens with e^, logorithms and square roots. Or you could just say irrational numbers.

If anyone is new to programming or just starting out, I hope this is a good tip for you to be aware of, If you need more help on why this is you can look it up on wikipedia, or maybe just ask Marek, but I am sure he is busy. I can answer to many questions about this matter, I am stilling learning to hone my programming skills and being aware of the many pitfalls do to truncations and roundoff errors. How to check for them, implement safegaurd measures, and how to structure your code to avoid them. I hope this helps.

Display posts from previous:  Sort by  
 Page 1 of 1 [ 1 post ] 

All times are UTC - 5 hours

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to: