tags:

views:

268

answers:

8

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 manipulates 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

+13  A: 

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.

nos
Ok. Hmm that answers well but is it a rule in C++/C that when being compared number will be automatically be promoted to double?
Shubham
I think my doubt is cleared.
Shubham
@Shubham A plain decimal number is always double; you should add `f` to make it a float
Amarghosh
+2  A: 

Here is some reading material What Every Computer Scientist Should Know About Floating-Point Arithmetic

PoweRoy
+1 I like this doc.
Bart
Somewhat less intimidating: http://floating-point-gui.de/
Michael Borgwardt
A: 

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

rano
A: 

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.

Alexandre C.
A: 

http://support.microsoft.com/kb/69333

:)

FractalizeR
A: 

float me = 1.7f;

Meir Assayag
+1  A: 

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

Joe
+1  A: 

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.

FredOverflow