Possible Duplicate:
strange output in comparision of float with float literal
#include<stdio.h>
int main()
{
float me = 1.7;
if(me==1.7)
printf("C");
else
printf("C++");
}
Output: C++
Now the reason for this behaviour is said that many floating point numbers cant be represented with absolute precision in binary.
My question is that - If computer thinks and manipula开发者_Go百科tes in binary. Any uncertanity in representation of me
would be same for 1.7
when compared. So both should be equal.
ALso how typecasting solves this problem? (float)1.7
You are comparing a float to a double. the literal 1.7
is a double.
You've stored that in a float, which might have less precision than a double, thus the me == 1.7
is comparing 1.7 as a float(promoted to a double) to 1.7 as a double.
In this case, me == 1.7f
should make them compare as equal, or changing me
to a double double me = 1.7;
In the general case though, you'd want equality to be compared using an epsilon as @duffymo shows.
Also, Obligatory read.
Here is some reading material What Every Computer Scientist Should Know About Floating-Point Arithmetic
The closest representation of 1.7 is different for float and double, so casting to a float should usually result in the same number.
One of the main reasons you can't compare floating point numbers is that identities that work for real numbers and integers don't necessarily work for floating point because of rounding - i.e. (x+y)+z and x+(y+z) can often be different (note that writing them that way will often not change a compiler's behavior, but you can induce the order by doing something the compiler wont optimize around).
For instance, (100 - (100 - .0000000001)) != .00000000001 using IEEE-754 doubles, even though math says they should be equal. So the computation that should be producing .00000000001 will be slightly off. This is especially a problem with more complicated calculations, such as linear algebra, where the answer can be the result of thousands of additions and subtractions, each of which can add to floating point rounding error.
IEEE-754 floating point can be very tricky, if you don't really understand what's going on.
I recommend the excellent "What every Computer Scientist Should Know About Floating-Point Arithmetic": http://docs.sun.com/source/806-3568/ncg_goldberg.html
Assuming an IEEE-754 representation, the literals 1.7f and 1.7 stand for the following values:
1.7f == 1.7000000476837158203125
1.7 == 1.6999999999999999555910790149937383830547332763671875
Clearly, these are not the same value, and thus they compare as false.
float me = 1.7f;
It is a problem of representation concerning numerical analysis
, since you have a limited amount of bit to represent the number what is represented is not exactly equal to the number you meant, it is just a close approximation to the nearest number representable with the bit you have.
Take a read here
The 1.7 is converted automatically to double in the comparison. So you're comparing 1.7 with 1.7d, which probably differ by some (double) machine epsilons.
精彩评论