Bug in Vicual C++ or programming mistake ?
-
long h3 double h2; float h4; h4=4.35*100*150/100.0; //float h3=(long)(h4+0.5); //=653 ! h2=4.35*100*150/100.0; //double h3=(long)(h2+0.5);//=652 ! What is wrong in the source sample ? Bug casting ? Illegal casting ? Tanks for your answers.
What version of VC++ are you using? Is this all the relevant code? On VC++ 5.0 your code compiles fine (after adding a missing ';' after
long h3
) and I only get a warning message in the assignment toh4
:warning C4305: '=' : truncation from 'const double' to 'float'
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
What version of VC++ are you using? Is this all the relevant code? On VC++ 5.0 your code compiles fine (after adding a missing ';' after
long h3
) and I only get a warning message in the assignment toh4
:warning C4305: '=' : truncation from 'const double' to 'float'
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
What version of VC++ are you using? Is this all the relevant code? On VC++ 5.0 your code compiles fine (after adding a missing ';' after
long h3
) and I only get a warning message in the assignment toh4
:warning C4305: '=' : truncation from 'const double' to 'float'
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
What version of VC++ are you using? Is this all the relevant code? On VC++ 5.0 your code compiles fine (after adding a missing ';' after
long h3
) and I only get a warning message in the assignment toh4
:warning C4305: '=' : truncation from 'const double' to 'float'
Joaquín M López Muñoz Telefónica, Investigación y DesarrolloI have installed a VC 5 : same problem (652 vs 653) Can you test this : h4=2.67*100*150/100.0; //float h3=(long)(h4+0.5); //= 401 ??? h2=2.67*100*150/100.0; //double h3=(long)(h2+0.5);//= 401 ??? I suspect an error on some type of configuration... Thank your for your answer... Seb.
-
long h3 double h2; float h4; h4=4.35*100*150/100.0; //float h3=(long)(h4+0.5); //=653 ! h2=4.35*100*150/100.0; //double h3=(long)(h2+0.5);//=652 ! What is wrong in the source sample ? Bug casting ? Illegal casting ? Tanks for your answers.
The double h2=0.5+ (4.35*100*150/100.0); isn't actually 653 but looks like it is 652.9999999999999 , hence the cast truncate returns 652. printf and the IDE debugger don't show this. we can see by adding this code if ((653.0-h2)==0) printf ("equal\n"); also by adding .0000000000001 to h2 before the cast, it then becomes 653 as expected. The float value must be truncating somewhere and avoiding the rounding error. . (guessing)
-
Are we talking about the same thing ? I have no problem to compile this code. What I am searching is why the result is different between the 2 lines (652 != 653)...
Oh, I completely misunderstood your first point. Sorry. There's nothing wrong with your code. The strange behavior is due to rounding errors inherent to floating point arithmetic. These numbers are stored in a internal format like this: s*mantissa*2^exponent Where s is the sign (+1 or -1) and mantissa is a number between 1 and 2, with a fixed number of decimals. Due to the limited amount of decimals (binary decimals, to be exact) in mantissa, some numbers that in base 10 have a finite number of decimals can only be approximated by a
float
or adouble
. Also, these two types reserve differents amounts of memory for representation:float
s are 4 bytes wide anddouble
s take 8 bytes, so these latter are more precise although this is not always the case --keep on reading. In your specific case, I've taken a look at the internal representation ofh4
andh2
and the exact values are: h4 = 10690560 * 2^-14 = 652.5 h2 = 5739450696990719 * 2^-43 ~ 652.49999999999988631316227838... and from this is obvious to deduce why you obtain different results when adding 0.5 and truncating tolong
. If you want to know more about floating point arithmetic, do a search on Internet for "IEEE 754" (the standard most microprocessors are based upon for this kind of things). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo