10 & 10/3*3 are not equal ! ! ! WHY?
-
Hello all and congratulate for new theme of site.
float f1,f2;
bool b;
float f1 = 10.0;
float f2 = f1 / 3;
b = f1 == (f2 * 3.0);WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .
-
Hello all and congratulate for new theme of site.
float f1,f2;
bool b;
float f1 = 10.0;
float f2 = f1 / 3;
b = f1 == (f2 * 3.0);WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .
Because when you deal with floating point values, equality cannot be expressed. because one of the number cannot be accurately be represented as a binary number thus leading to rounding errors that should be taken into account by the developers. see this : http://docs.sun.com/source/806-3568/ncg_goldberg.html[^]
Watched code never compiles.
-
Hello all and congratulate for new theme of site.
float f1,f2;
bool b;
float f1 = 10.0;
float f2 = f1 / 3;
b = f1 == (f2 * 3.0);WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .
-
Hello all and congratulate for new theme of site.
float f1,f2;
bool b;
float f1 = 10.0;
float f2 = f1 / 3;
b = f1 == (f2 * 3.0);WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .
i remember,when i was a student, my c language teacher told me , if i compare two "float" or "double", i should check it between a very little number. such as , const float c_tmp = 0.000001; bool b; float f1 = 10.0; float f2 = f1 / 3; b = (f1 >= (f2 * 3.0) - c_tmp) && (f1 <= (f2 * 3.0) + c_tmp);
-
i remember,when i was a student, my c language teacher told me , if i compare two "float" or "double", i should check it between a very little number. such as , const float c_tmp = 0.000001; bool b; float f1 = 10.0; float f2 = f1 / 3; b = (f1 >= (f2 * 3.0) - c_tmp) && (f1 <= (f2 * 3.0) + c_tmp);
-
Hello all and congratulate for new theme of site.
float f1,f2;
bool b;
float f1 = 10.0;
float f2 = f1 / 3;
b = f1 == (f2 * 3.0);WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .
10 divided by 3 is 3.333 recurring in decimal and in binary it's something like: 11.01010101 (can't be bothered working it out any further). Say you have a computer which stores floats as 6 bits for the non-exponent chunk of the number. It'd truncate the value of 10/3 to 11.0101. When you multiply it by three you get: 1001.1111 which is truncated to 1001.11 which is 9.75 in decimal. While real double precision numbers use oodles more bits to represent the non-exponential part you get the same problem. Note that you've got a similar problem with order of evaluation: (A * B ) / C for a computer using a finite number of bits is not necessarily equal to ( A / C ) * B. Fortunately most real world uses of computers don't have these problems - it's really only a concern to people doing simulation or some science. The rest of us can use integers most of the time and get away with it. Cheers, Ash
-
Because when you deal with floating point values, equality cannot be expressed. because one of the number cannot be accurately be represented as a binary number thus leading to rounding errors that should be taken into account by the developers. see this : http://docs.sun.com/source/806-3568/ncg_goldberg.html[^]
Watched code never compiles.
-
Hello all and congratulate for new theme of site.
float f1,f2;
bool b;
float f1 = 10.0;
float f2 = f1 / 3;
b = f1 == (f2 * 3.0);WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .
In the code, you do this:
float f2 = f1 / 3;
I believe you're looking for this:fload f2 = f1 / 3.0;
I'm not sure it's going to set b = true, however, it's more correct nontheless.Josh Davis
This is what plays in my head when I finish projects.