1

I have the following code
Case 1

float a=1.7; if(a==1.7) { printf("Equal\n"); } else if(a<1.7) { printf("less\n"); } else{ printf("Greater\n"); } 

Output

Greater 

Case 2

float a=1.3; if(a==1.3) { printf("Equal\n"); } else if(a<1.3) { printf("less\n"); } else{ printf("Greater\n"); } 

Output

Less 

Why the outputs are not as expected?
Why the outputs are different in both cases?
Can anyone explain how the values are stored and compared here
Thanks in advance

3
  • 1
    a is a float and 1.7 is double. Due to assignment to the nearest FP value, 1.7 to float and 1.7 to double, they differ. Commented Dec 5, 2014 at 4:37
  • When you are writing a=1.3, it's not exactly storing 1.3. It can be 1.3000000001 or also can be 1.29999999. It depends on compiler. So to check if two floating number is equal you have to use fabs(). You can write this, if(fabs(a-1.3)<1e-6) { printf("Equal\n"); } Commented Dec 5, 2014 at 4:37
  • Read floating-point-gui.de which explains very well all that. Commented Dec 5, 2014 at 5:20

3 Answers 3

2

a combination of things -

  • internally C uses double; float is a storage format.

  • 1.3 is an infinitely repeating decimal when expressed in binary

  • IEEE floating-point rounds the mantissa when storing it. 1.3 stored as a float gets rounded down, and 1.7 gets rounded up (by a tiny bit)

  • the float a when read will be converted to a double by adding extra digits of least significant bits set to 0's. Values that were rounded down are less than they were. Values that were rounded up, even though they end in all zeros, are greater.

  • and when comparing 1.xyzxyz000000 to 1.xyzxyzxyzxyz where not all of the xyz is 0, the one ending in zeros will be less.

Edit: missed the ieee rounding aspect earlier

For the curious, here is the actual value stored for 1.3 and 1.7 as 32-bit floats:
1.299999952316284179687500000000
1.700000047683715820312500000000

and as 64-bit floats:
1.30000000000000004440892098500626161694526672363281250000000000000000
1.69999999999999995559107901499373838305473327636718750000000000000000

Sign up to request clarification or add additional context in comments.

Comments

2

When you assign a float a literal and then compare the result to the same literal, you should see that the values are equal. The trouble is, in your code you are not comparing a float to float: it is a float to double comparison.

Constants 1.7 and 1.3 are of type double. When you compare them to a float, the value of float gets expanded to double before the comparison. This is the stage at which the values become non-equal: unless the constant is represented exactly (neither 1.7 nor 1.3 can be represented exactly in a floating point number) there will be a conversion error.

You can address this by comparing to a float constant: if you cast your constants to float before comparison, the equality checks would succeed:

float a=1.7; if(a==(float)1.7) { printf("Equal\n"); } else if(a<(float)1.7) { printf("less\n"); } else { printf("Greater\n"); } 

Demo (prints Equals).

Comments

1

In checking case it will consider an integer. so it will roundup the floating value to integer.

1.7 roundup as 2. 1.3 roundup as 1. 

so this is the reason for that answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.