Monday, May 24, 2010

Does your compiler in C++ show the same result as mine?

I am using Bloodshed's Dev-C++ compiler, i tried to print this statement...


(int)(523.0/5.23)


It gives me a result of 99... But if the statement is only like this...


(523.0/5.23)


It gives a result of 100..


I wanted to convert the result of a double operation into integer... I was quite confused why when I convert the answer into int, it gives me a value of 99... I tried (int)(524.0/5.24) and (int)(525.0/5.25).. both gives me an integer answer of 100...


Actually, I tried more double numbers which I knew would give a result of 100... Converting them to integer using casting gives me a result of 100.. But why when I try converting the answer of 523.0/5.23 to integer, it gives me a result of 99.. I can't explain why.. Is this a serious mathematical operation bug in C++? or is it only a bug of the compiler I am using? I don't have other compilers aside from Dev-C, so I can't compare the results.. One more thing that keeps playing in my mind is that, does 523 have a huge significance in C++?

Does your compiler in C++ show the same result as mine?
Wow, you just discovered a serious bug that no one, for years of use in high performance apps and embedded apps has found. No…not really.





You need to understand how floating point works in computers. The representation may not be precise, and this introduces accuracy problems in floating point. It’s not a C++ problem. It’s how computers work, and you can’t dodge it. Here’s one technical article: http://docs.sun.com/source/806-3568/ncg_...





If you look up the wikipedia article on floating point, they talk about accuracy problems. You can just Google generally as well. It’s not a one line topic, so I urge you to read about it on Google. Any summary here will be oversimplified to the point of being dangerous.
Reply:No Nothing wrong. you have rounded your figure to int functions





524 / 5.25 = 99.809523809523809523809523809524





525 / 5.25 = 100
Reply:5.23 (decimal) does not have an exact representation in binary floating point. So when the computer does the divide 523/ 5.23, it gets 99.999....9. The (int) of that is rounded DOWN, to 99.





The print statement rounds to NEAREST integer, so print ... (523/5.23) shows up as 100.





The number 5.25 has an exact representation, so you get 100 all the time.





The number 5.24, I would bet, gives you 100.000....1





Try printing out the full hex value of the intermediate and final results.





double x1, x2, x3;


x1 = 5.23; x2 = 523 / x1;
Reply:So you are dividing? Visual C++ cannot divide integers


with the '/' key as it is equal to 'or', but yes the division


gets the same value of 99 over the highest integer


in the 16-bit code.
Reply:Run this: (Sorry if my code's messy, I'm not fluent in C++.)





#include %26lt;iostream%26gt;


int main () {


float i;


for (i=0;i%26lt;1000;i++) {


if ((int)(i/(i/100))==99) {


std::cout %26lt;%26lt; i;


std::cout %26lt;%26lt; "\n";


}


}


}





There appears to be more "magic numbers" than 523... However, I don't see a pattern right away.





Oh, yes it's that messy float problem.

floral design

No comments:

Post a Comment