1. Unexpected output!!!!!!!!!

i know this is simple program but why output is B?
Code:
```void main(){
float a;
a=6.7;
if(a==6.7)
printf("A");
else
printf("B");
}```

2. Did you try to print the contents of your variable?

Code:
```#include <stdio.h>

int main(void)
{
float a = 6.7;

printf("%10f\n", a);

return 0;
}```
Jim

3. yes output is 6.7000000

4. Try this:

Code:
`printf("%.10f\n", a);`

5. output is 6.6999998093 but in my case it prints B tell me about that

6. Very good. Now look at the original program and see if you can understand why you get the output you're getting.

7. So does that answer your question? a is not equal to 6.7.

Jim

8. a is a float, 6.7 is a double. When you assign the double to the float you lose some precision. Then, you compare the float with a double which again have different levels of precision and you get a weird result. Try this and see if you get your expected result:
Code:
```float a;
a=6.7f;
if(a==6.7f)
printf("A");
else
printf("B");```
Bottom line is that some base-10 floating point values cannot be accurately represented in binary floating-point form and moreover aren't represented the same in their float/double variants.

9. u mean value of a is not 6.7 even i declare it as 6.7 then does value of a is 6.6999998093 ? so a!=6.7 so it prints B ?

10. Ohhh!! YES now i understand it... so i have to declare it as 6.7f to comare it with float ....thx for ur replies

11. Originally Posted by san12345
u mean value of a is not 6.7 even i declare it as 6.7 then does value of a is 6.6999998093 ? so a!=6.7 so it prints B ?
Yes.

Originally Posted by san12345
Ohhh!! YES now i understand it... so i have to declare it as 6.7f to comare it with float ....thx for ur replies
Well, not exactly. Floating point values are often approximations, since real numbers are continuous on the number line and the computer cannot represent infinite values. Thus, there are often inaccuracies when working with floating point values. Hence, using direct comparison (i.e. == ) on floats is not a good idea. I assume this was the lesson you were supposed to learn from the original code example.

12. so i have to declare it as 6.7f to comare it with float
But that won't always help. You should never compare floating point numbers with the operator== or operator!=. Floating point numbers are approximations and even using a long double will not always compare equal.

I would give you a link to read to help explain some of these problems but I doubt you would take the time to actually read the link so I won't bother.

Jim

13. Originally Posted by san12345
Ohhh!! YES now i understand it... so i have to declare it as 6.7f to comare it with float ....thx for ur replies
Well that all depends on the binary number itself. If 6.7 was actually expressed (closely) as 6.69... as a double, putting it in an even smaller memory space is not going to improve things.

To understand floating point you really need to understand a couple of things:
1. Floating point is scientific notation in binary base. Just as you can write a very large or small number, like say Avagadro's constant in this notation, (6.022 * 1023) floating point types use the same tactic to represent very large or small numbers in a finite, uniform set of bits.
2. Often what determines the chance of ever getting a true equality comparison depends on the nature of that notation. Did the number actually terminate, or is it approximate? Just like in decimal, there are repeating rational numbers that don't terminate in binary, or you might have simply run out of bits.

This ignores rounding errors too...

But for all of the above, comparing floating point for equality requires cleverness. Most people are not prepared for the work when they first encounter it.