Note: I'm currently working in C++11 and using GCC to compile.
I'm dealing with a situation where the result varies between the below two calculations:
value1 = x * 6.0;
double six = 6.0;
value2 = x * six;
value1 != value2
Where all variables above are of type double
.
Essentially, I wrote a line of code that gives me an incorrect answer when I use 6.0 in the actual calculation. Whereas, if I assign 6.0 to a variable of type double first then use the double in the calculation I receive the correct result.
I understand the basics of floating point arithmetic, and I guess it's obvious that something is happening to the bits of 6.0 when it is assigned to the double type.
Question: Why are the above two code segments producing (slightly) different results?
Note: I'm not simply comparing the two numbers with ==
and receiving false
. I've just been printing them out using setprecision
and checking each digit.
Aucun commentaire:
Enregistrer un commentaire