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