strange float vs double comparison result

float a = 1.2;
and you know c and c++ says that
a is not equal to 1.2 Why ???

i.e following condition results false

if(a == 1.2)

why a != 1.2 whereas I put a = 1.2 before.

please give details on this topic not just basic reason like "conversion from double to float losses precision"

my point is no matter how many zeros you put after 1.2 it should be equal to 1.2

and What the heck I checked it with 1.5 replacing each 1.2 then the result is true, this is driving me nuts

use 1.2 gives false
use 1.5 gives true
Last edited on
i.e following condition results false
Your statement is false. 1.5 is perfectly representable in IEEE floating point format. And that boolean expression results in true:
http://ideone.com/M4aFec
1
2
3
4
5
6
7
#include <iostream>

int main()
{
    float a = 1.5;
    std::cout << (a == 1.5);
}
1
Last edited on
I'm so sorry MiiNiPaa actually I write it in a hurry but now I have edited the OP

with 1.2 it's false
with 1.5 it's true

What's going on?
Ok. I do not know how much do you know about binary, so I would give examples in decimal.
Computer does not have infinite memory. In fact each ty has a specific size which it cannot exceed.
For example lets state that our "decimal float" is 2 digits and "decimal double" is 5 digits.
so we can store sat 1/4 in them:
float representaion: 0.25
double representation: 0.25000
When we compare them (0.25 == 0.25000) float gets promoted to double (adding several zeroes in the end) (0.25000 == 0.25000 == true)
That what is happens for 1.5 in your case.

Now continue our example and try to store 1/3 here. Ooops, 1/3 is not representable as a finite decimal. Best we can do is store approximate value:
float = 0.33
double = 0.33333
Notice that double is able to store more digits here.
Compare them (0.33 == 0.33333). Promoting... (0.33000 == 0.33333 == false)
That what happens for 1.2

Note that you can explicitely compare values as floats to avoid double promotion:
1
2
3
4
5
6
7
#include <iostream>

int main()
{
    float a = 1.2;
    std::cout << (a == 1.2f);
}
1
http://ideone.com/Whrne1
Last edited on
1.2 is not representable as float, it lies between the floats 1.19999992847442626953125 and 1.2000000476837158203125. Since it's a little closer to the second value, when you write
float a = 1.2; the C (and C++) compiler puts in the compiled executable float a = 1.2000000476837158203125;

1.2 is also not representable as double. It lies between the doubles 1.199999999999999955591079014993738383 and 1.200000000000000177635683940025046468. It is a little closer to the first one, so when you write 1.2, the compiler puts in the compiled executable the value 1.199999999999999955591079014993738383

Needless to say, 1.2000000476837158203125 does not equal 1.199999999999999955591079014993738383

You could use if(a == 1.2f) to compare float with float

And, as noted, 1.5 is a valid float, and is a valid double, so 1.5 is what's compiled in both cases
Last edited on
my point is no matter how many zeros you put after 1.2 it should be equal to 1.2

That's true if the numbers are expressed in decimal notation. However the double and float types each use binary notation to store the values.

http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html

1.5 decimal = binary 1.1000000000000000000000
1.2 decimal = binary 1.0011001100110011001100

And as you already pointed out, float has less precision than double, so the comparison with decimal 1.2 would be something like (binary)
1.00110011 vs. 1.0011001100110011001100


OMG! guys I love you so much, I love ya all. Thankyou tooooooo much for helping and teaching me in this nice way, I'm sooooooo much thankful to you guys.
Topic archived. No new replies allowed.