3

Why does the code given below Prints b on the Screen?

#include<stdio.h>
main()
{
    float a = 5.6;
    if(a == 5.6)
    {
        printf("a");
    }
    else
    {
        printf("b");    
    }
}
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • 1
    You can not compare with floating numbers in this way. – Abhishek Jan 06 '14 at 10:03
  • It is not a good idea to compare floats for exact equality. – Buddhima Gamlath Jan 06 '14 at 10:03
  • A decimal in C is by default a double and float has lower precision than a double.Here 'a' is float and hence when compared with the same valued double doesn't give what you expect.Infact, when you replace == with > in the program, still b will be printed on screen as double has higher precision. – Rafed Nole Jan 06 '14 at 12:16

3 Answers3

3

As floating point Numbers can't be matched exactly (because between each 2 numbers you choose are infinite other numbers). A machine can't represent them all and is forced to represent them with a moddel of only some floating point numbers it is able to represent.

So in your case, the system is probably not storing 5.6 because that's a number your machine doesn't want to represent. Instead it is storing something which is pretty close to 5.6 into the memory.

So if you do comparing with floating point numbers you never should check for equivalenz. Instead you should use the system C define FLT_EPSILON and check for

if (((a - 5.6) > -FLT_EPSILON) && ((a - 5.6) < FLT_EPSILON))
{
    ...
}

Where FLT_EPSILON is the smallest representable float type value. So if the difference from a to 5.6 is absolute smaller as EPSILON, you can be sure it WAS equal, but the machine has chosen the next number it knows instead of 5.6.

The same would be DBL_EPSILON for double type.

this types are defined in float.h

dhein
  • 6,431
  • 4
  • 42
  • 74
  • I would use `if ( fabs( a - 5.6 ) < E )` instead of the expanded version. – concept3d Jan 06 '14 at 10:42
  • @concept3d Yeah but if you want high performance, I wouldn't. If performance isn't the critical point fabs() is the better to read way, that I agree with so far. – dhein Jan 06 '14 at 10:48
  • I don't agree with the performance point, fabs will actually be reduced to a single instruction on most hardware. And even if it didn't the performance will hardly be any different (both version have two branches). – concept3d Jan 06 '14 at 10:49
  • @concept3d But the `&&` breaks allready if it is out of EPSILON space in first case. So anyway I will at least save the time of a single function call. And Even a function call takes time for stack pus and poping. – dhein Jan 06 '14 at 10:56
  • But as I already said: ofcourse `fabs()` is the prettyer way. – dhein Jan 06 '14 at 10:57
  • fabs will actually be reduced to a single instruction on most hardware. – concept3d Jan 06 '14 at 10:57
2

When you want a float value don't forget to add f

#include<stdio.h>
main()
{
    float a = 5.6f;
    if(a == 5.6f)
    {
        printf("a");
    }
    else
    {
        printf("b");    
    }
}

prints a as expected.

The problem was that both 5.6 are defined as double literals, and the a got converted into a float while in the if it's still comparing it to a double value so you get false.

Actually adding f only inside the if would be enough, but better safe then sorry.

MoonBun
  • 4,322
  • 3
  • 37
  • 69
  • 7
    Though this worked by eliminating the cast between float and double, but I don't think absolute equality comparison with non integers are valid. – TwilightSun Jan 06 '14 at 10:05
0

floating point number in c is double by default. If you want use as float you need to add f at end of the number. try below code it gives a

#include<stdio.h>
main()
{
    float a = 5.6;
    if(a == 5.6f)
    {
        printf("a");
    }
    else
    {
        printf("b");    
    }
Chinna
  • 3,930
  • 4
  • 25
  • 55