1

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

Here is the code

#include<stdio.h>
int main()
{
 float a=0.3;
 if(a==0.3)
  printf("Hello World!");
 else
  printf("Stack Overflow");
 return 0;
}

I expected output as "Hello World". But i got "Stack overflow". Why I didnt get "Hello World"?

Is there anything wrong in the if condition?

Community
  • 1
  • 1
Dinesh
  • 16,014
  • 23
  • 80
  • 122
  • Not done `C` but i'll guess it should be float `a==0.3f`; – Ash Burlaczenko Nov 25 '11 at 09:20
  • 1
    In this case, `a` is being promoted to `double` since `0.3` is a double literal. Since `0.3` is not exactly representable the comparison fails. – David Heffernan Nov 25 '11 at 09:25
  • 4
    What David says: `(double)(float)(0.3)` isn't equal to `0.3`, because the former has been rounded off to `float` precision along the way, while the latter retains `double` precision. For an analogy in base 10, suppose I take `1/3` and represent it to 3 significant figures: `0.333`. That's the "float" `a`. Now convert the value of `a` to a value with 6 significant figures (a "double"): `0.333000`. That's not equal to `0.333333` (the value of 1/3 as a "double"). – Steve Jessop Nov 25 '11 at 10:10

6 Answers6

6

Comparing floating point numbers

Floating point math is not exact. Simple values like 0.2 cannot be precisely represented using binary floating point numbers, and the limited precision of floating point numbers means that slight changes in the order of operations can change the result. Different compilers and CPU architectures store temporary results at different precisions, so results will differ depending on the details of your environment. If you do a calculation and then compare the results against some expected value it is highly unlikely that you will get exactly the result you intended.

Try this way:

#include<stdio.h>
int main()
{
 float a=0.3;
 float acceptedDiff = 0.0000001;
 if(fabsf(a-0.3) < acceptedDiff)
  printf("Hello World!");
 else
  printf("Stack Overflow");
 return 0;
}
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
4

Try that :)

if (a == 0.3f)

or

if (a == (float)0.3)
ablm
  • 1,265
  • 10
  • 19
0

Floating point values shall not be compared using either the == or != operators. Most floating point values have no exact binary representation and have a limited precision. See what you can do.

Community
  • 1
  • 1
Petr Abdulin
  • 33,883
  • 9
  • 62
  • 96
0

It is because values like 0.3 cannot be precisely represented using binary floating point numbers. Temporary values are stored differently and thus cannot be compared liike this. The value for a and 0.3 are thus stored differently and you cannot rely on such a direct comparison.

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
0

It is because of precision issue..This tutorial might interest you..

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
0

Yes as far as i know while the IF statement compare the variables and values it goes into the machine level information and storage of the value.

very first line you declared a = 0.3 as float type and so it will be stored in the binary format in the memory with the extra padding of the bits. and when you compare it with Exact "0.3" it will always fail.

Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47