-5

Why is the Output of This code , "BYE".. and not "HI"...

#include<stdio.h>
int main()
{
    float i=1.1;
    if(i==1.1)
        printf("HI");
    else
        printf("BYE");
    return 0;
}

As 1.1 is a float value so shouldn't it been "HI"

Sourabh
  • 1
  • 1

9 Answers9

2

floating points numbers cannot be represented precisely. so you cannot equate two float directly. it differs normally with epsilon.

if(fabs(floatVarialbe-expectedValue) < **FLT_EPSILON**)
{
 //statements to execute if they are equal
}

you should fabs function to get absolute value [remove negative sign], its defined in math.h library. FLT_EPSILON is a relative error and its defined in float.h library

Due to rounding errors, most floating-point numbers end up being slightly imprecise. As long as this imprecision stays small, it can usually be ignored. However, it also means that numbers expected to be equal (e.g. when calculating the same result through different correct methods) often differ slightly, and a simple equality test fails.

if you want to know more, What Every Computer Scientist Should Know About Floating-Point Arithmetic

Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
1

You shouldn't use == to check for equality of floats. Check this out. http://how-to.wikia.com/wiki/Howto_compare_floating_point_numbers_in_the_C_programming_language

Carth
  • 2,303
  • 1
  • 17
  • 26
1

1.1 is not a floating point number,1.1f is

DeanSinaean
  • 2,297
  • 4
  • 16
  • 19
0

Because you can't compare floating point numbers for equality. You need to ask "is i within an acceptable tolerance of 1.1?". People often name the tolerance "epsilon".

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
0

It's because of precision. 1.1 cannot be precisely represented using binary floating point numbers.

instead try if(i == 1.1f) or if(i == (float)1.1)

Udit Mukherjee
  • 687
  • 5
  • 20
0

because in your code

if(f == 1.1) the 1.1 is considering as a double. Try 1.1f to use this value treated as a float:

if(f == 1.1f)

hope it helps

Praveen Sharma
  • 4,326
  • 5
  • 25
  • 45
0

The reason that you are not getting the correct answer is because of the fact that 1.1 cannot be represented exactly as 1.1 in float (Due to the limited precision it has). In case you want to learn why the comparison and simple operations of + , - , *, / do not give correct answers many times then you can visit the below link for more information -

What Every Programmer Must Know About Floating Point

Now, you can modify your program to the below to get the correct result -

#include<stdio.h>
int main()
{
    float i=1.1;
    if(i==(float)1.1)
        printf("HI");
    else
        printf("BYE");
    return 0;
}

What this program does is that it type casts the comparison value and gives it precision of Float data type rather than the Double data type which is used by the compiler to represent value before assignment to any kind of variable (Int, Float, Double etc).

Pranav Jituri
  • 823
  • 1
  • 11
  • 25
0

Because, incredible as it may seem, 1.1 and 1.1 are not always the same number when they are converted to the floating-point representation used by the C compiler. Try this program:

#include<stdio.h>
int main()
{
    float i=1.1;
    printf("i=%0.20f   1.1=%0.20f\n", i, 1.1);
    return 0;
}

The short explanation is that floating-point representation is not precise for values that aren't exactly representable in base 2. For example, try the sample code with 1.5 instead of 1.1 - that should work. As already mentioned in GreenAsJade's answer, you don't test floating-point numbers for equality, you test to see if they are "close enough to be considered equal for the purposes of this application". And as DeanSinaean said, you didn't qualify your constants to be floating point with the trailing "f", so the first time it gets converted as a double. I'm guessing it's different the second time because the compiler converts "1.1" to float in order to compare to "i" which is of type float.

For more details of what's happening under the hood, head over to: How are floating point numbers are stored in memory?

Community
  • 1
  • 1
Captain Pedantic
  • 6,684
  • 1
  • 12
  • 5
-1

You are trying to compare float and double Try to typecast as below and see.

int main()
{
    float i=1.1;
    if(i==(float)1.1) //<------- to float
        printf("HI");
    else
        printf("BYE");
    return 0;
}

For details see this

Community
  • 1
  • 1
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31