--- In [email protected], Uday Oberio <uday_obe...@...> wrote:
>
> i have one question that is 
> 
> float a=1.1;
> double b=1.1;
> if(a==b)
> printf("hello");
> else
> printf("how are u ");
> 
> now tell me which part will be executed and why ?

Paul's link gives the full answer, but in simple terms there are 2 problems:

1) Physically, 1.1 cannot be represented exactly in binary.
2) Logcally, real numbers are 'analogue' rather than 'digital' so (as Paul 
said) it doesn't make sense to test whether one is 'equal' to another.

As a result of 1), assuming float a is 4 bytes and double b is 8 bytes, all 8 
bytes in b are being used to hold the value 1.1. So say 1.1 decimal in binary 
is 1.101010101010... Then float a holds (say) binary 1.1010 and b holds (say) 
binary 1.10101010. These aren't 'equal'.

Because of 2), what you should do is calculate the difference between a and b 
and check whether it is less than some (presumably small) value which you must 
decide on. Probably best to read the article to find out how to do that in 
general terms, or find a good maths library.

Reply via email to