fractions (0.9) and (0.2) make a truncation error when representing in float or double so if we represent 0.9 or 0.2 two times:
- the first one in float variable
- the second one in double variable
- it's mean that one of them is greater than the other, and the biggest is the double variable
so if we subtract the double from the float, it is expected that the result will be positive
why this code printed ( "Positive" )
float afloat = 0.9;
if ( 0.9 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9 - afloat < 0 ) cout << "Negative" << endl;
while this code printed ("Negative") ??
float afloat = 0.2;
if ( 0.2 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.2 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.2 - afloat < 0 ) cout << "Negative" << endl;