0

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;
Ehab Fawzy
  • 21
  • 4
  • 3
    [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Jesper Juhl Nov 10 '18 at 18:15
  • 3
    *"it's mean that one of them is greater than the other, and the biggest is the double variable"* - Why would this be always true? – Holt Nov 10 '18 at 18:23
  • 2
    Your hypothesis is incorrect: `the bigger is the double variable`. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia) – Fabio Nov 10 '18 at 18:25
  • 1
    `fractions (0.9) and (0.2)` -- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers. – PaulMcKenzie Nov 10 '18 at 19:12

1 Answers1

1

The literal 0.9 is a double precision value approximation to 9/10, and your declaration

float afloat = 0.9

truncates this value to single precision, thereby losing information. This code

float afloat = 0.9f;  

if      ( 0.9f - afloat >  0 ) cout << "Positive" << endl;
else if ( 0.9f - afloat == 0 ) cout << "Equal"    << endl;
else if ( 0.9f - afloat <  0 ) cout << "Negative" << endl;

will generate the output

Equal

even if 0.9f is not exactly equal to the fraction 9/10. The sign of the quantization error will differ for different fractions, and for different floating point formats.

sveinbr
  • 106
  • 5