atof question
-
char temp[] = "1.15"; double dbl = atof(temp); If I run this code in Visual Studio 6 dbl = 1.15 If I run this code in Visual Studio 2003 dbl = 1.1499999999999999 Can anyone explain this for me please
-
char temp[] = "1.15"; double dbl = atof(temp); If I run this code in Visual Studio 6 dbl = 1.15 If I run this code in Visual Studio 2003 dbl = 1.1499999999999999 Can anyone explain this for me please
Exactly what is telling you those values ? I am willing to bet that if you look at the 8-byte binary value they will be identical. It is the output display that is the only thing that differs. If you use a format specifier of "%.2f" with one of the printf family of functions (print, sprintf, or fprintf) I expect that you will see 1.15. It is important to note that it is virtually impossible to acquire exactly accurate floating point values. There are nearly always rounding errors involved that you must just deal with. __________________________________________ a two cent stamp short of going postal.
-
Exactly what is telling you those values ? I am willing to bet that if you look at the 8-byte binary value they will be identical. It is the output display that is the only thing that differs. If you use a format specifier of "%.2f" with one of the printf family of functions (print, sprintf, or fprintf) I expect that you will see 1.15. It is important to note that it is virtually impossible to acquire exactly accurate floating point values. There are nearly always rounding errors involved that you must just deal with. __________________________________________ a two cent stamp short of going postal.
Thanks for your reply I was getting the values from the quick-watch window displayed by the debugger. The 8-byte binary value displayed in the memory window IS the same. I have found that the real cause of my bug is the use of the pow() function from math.h. I had code like long rounding = 4; double result = pow(10, -rounding); which in vs6 returns result = 0.0001 and in vs2003 returns result = 0.0 these values ARE different in memory Looking at math.h I think the code above was calling long pow(long, long) resulting in 0 and auto casting the result to a double. I changed it to double result = pow(10.0, -rounding); and now it works. Why its different in vs6 and vs2003 I dont know
-
Thanks for your reply I was getting the values from the quick-watch window displayed by the debugger. The 8-byte binary value displayed in the memory window IS the same. I have found that the real cause of my bug is the use of the pow() function from math.h. I had code like long rounding = 4; double result = pow(10, -rounding); which in vs6 returns result = 0.0001 and in vs2003 returns result = 0.0 these values ARE different in memory Looking at math.h I think the code above was calling long pow(long, long) resulting in 0 and auto casting the result to a double. I changed it to double result = pow(10.0, -rounding); and now it works. Why its different in vs6 and vs2003 I dont know
As previously mentioned you should not expect an exact comparision when working with floating point numbers. To check equality you need to compare the differnce to epsilon. If the difference is < or = epsilon then the numbers are equal. Otherwise they are not. This is done as follows: take the absolute value of the difference of the absolute values of the two floating point numbers and compare the result to epsilon. If the difference is less than or equal epsilon then the numbers are equal. if( FLT_EPSILON =< fabsf( fabsf(f1) - fabsf(f2) ) ) printf( "numbers are equal\n" ); else printf( "numbers are not equal\n" ); NOTE: there are double precision variants of epsilon and fabsf. Sam Sam