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"); }
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"); }
Did you try to print the contents of your variable?
JimCode:#include <stdio.h> int main(void) { float a = 6.7; printf("%10f\n", a); return 0; }
yes output is 6.7000000
Try this:
Code:printf("%.10f\n", a);
output is 6.6999998093 but in my case it prints B tell me about that
Very good. Now look at the original program and see if you can understand why you get the output you're getting.
So does that answer your question? a is not equal to 6.7.
Jim
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:
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.Code:float a; a=6.7f; if(a==6.7f) printf("A"); else printf("B");
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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 ?
Ohhh!! YES now i understand it... so i have to declare it as 6.7f to comare it with float ....thx for ur replies
Yes.
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.
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.so i have to declare it as 6.7f to comare it with float
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
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 * 10^{23}) 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.