-1

Possible Duplicate:
strange output in comparision of float with float literal

I can't understand this code. How can two same numbers be compared?

#include<stdio.h>

int main()
{
    float a=0.8;
    if(0.8>a)     //how can we compare same numbers?
        printf("c");
    else
        printf("c++");
    return 0;
}

How can this problem be solved?

Community
  • 1
  • 1
amol
  • 125
  • 1
  • 11
  • The 0.8 in `a` has lower precision than 0.8 in the `if` statement, so error in the promotion leads to unexpected result. – nhahtdh Aug 01 '12 at 12:13
  • i have read somewhere that default data type for floating point nos is double – amol Aug 01 '12 at 12:14
  • `float` is only 32-bit, and the `0.8` in `float a` is represented with 32-bit, while `0.8` in the `if` statement is `double` 64-bit. – nhahtdh Aug 01 '12 at 12:15
  • Are you sure this compiles properly. Because left side of > operator in if condition is a constant? Even though it compiles a<0.8 should be used. – Narendra Aug 01 '12 at 12:16
  • @rain it compiles properly both ways – amol Aug 01 '12 at 12:20
  • @rain, you're probably thinking of the coding practice of coding if (0.8 == a) instead of if (a == 0.8) where, if you omit the 2nd =, the code won't compile as you can't assigned to a constant. It does nothing for legibility though. – Tom Tanner Aug 01 '12 at 13:38
  • How about just writing, ``float a=0.8f;`` and comparing with ``0.8f>a`` and reducing chances of float-double effect and improving better readability? – askmish Aug 01 '12 at 13:45

2 Answers2

2

I do not understand why you ask whether the same two numbers can be compared. Why would you not expect a comparison such as 3 > 3 to work, even though 3 is the same as 3? The > operator returns true if the left side is greater than the right side, and it returns false otherwise. In this case, .8 is not greater than a, so the result is false.

Other people seem to have assumed that you are asking about some floating-point rounding issue. However, even with exact mathematics, .8 > .8 is false, and that is the result you get with the code you showed; the else branch is taken. So there is no unexpected behavior here to explain.

What is your real question?

In case you are asking about floating-point effects, some information is below.

In C source, the text “.8” stands for one of the numbers that is representable in double-precision that is nearest .8. (A good implementation uses the nearest number, but the C standard allows some slack.) In the most common floating-point format, the nearest double-precision value to .8 is (as a hexadecimal floating-point numeral) 0x1.999999999999ap-1, which is (in decimal) 0.8000000000000000444089209850062616169452667236328125.

The reason for this is that binary floating-point represents numbers only with bits that stand for powers of two. (Which powers of two depends on the exponent of the floating-point value, but, regardless of the exponent, every bit in the fraction portion represents some power of two, such as .5, .25, .125, .0625, and so on.) Since .8 is not an exact multiple of any power of two, then, when the available bits in the fraction portion are all used, the resulting value is only close to .8; it is not exact.

The initialization float a = .8; converts the double-precision value to single-precision, so that it can be assigned to a. In single-precision, the representable number closest to .8 is 0x1.99999ap-1 (in decimal, 0.800000011920928955078125).

Thus, when you compare “.8” to a, you find that .8 > a is false.

For some other values, such as .7, the nearest representable numbers work out differently, and the relational operator returns true. For example, .7 > .7f is true. (The text “.7f” in source code stands for a single-precision floating-point value near .7.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

0.8 is a double. When a is set to it, then it's converted into a float and at this point looses precision. The comparison takes the float and promotes it back to a double, so the value is for sure different.

EDIT: I can prove my point. I just compile and ran a program

float a = 0.8;
int b = a == 0.8 ? 1 : 0;
int c = a < 0.8 ? 1 : 0;
int d = a > 0.8 ? 1 : 0;

printf("b=%d, c=%d, d=%d, a=%.12f 0.8=%.12f \n", b, c, d, a, 0.8);
b=0, c=0, d=1, a=0.800000011921 0.8=0.800000000000

Notice how a now has some very small factional part, due to the promotion to double

David H
  • 40,852
  • 12
  • 92
  • 138