3

I want to get the fraction (modulo 1) of the quotient of two long values.

long dividend = 50L;
long divisor = 3L;
float n1 = (dividend % divisor) / (float) divisor;
float n2 = (dividend / (float) divisor) % 1;
System.out.println(n1);
System.out.println(n2);

The above prints the following.

0.6666667
0.66666603

I don't have very good knowledge of how floating point precision works.

Why is the second (probably clearer) implementation less accurate than the first?

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91
  • 3
    Because a `float` only has 6 to 9 decimal digits of precision. 50 / 3 is 16.666666, but when you then remove the integer part to get 0.666666, the inaccuracy of `float` beyond the last digit becomes apparent. By calculating first, then removing digits, you lost precision. E.g. see [Why Are Floating Point Numbers Inaccurate?](http://stackoverflow.com/q/21895756/5221149) – Andreas Mar 19 '17 at 07:15
  • Ooooh, I got it! Thank you! Also, thanks for the link to more reading. If you want to post an answer, I'll accept it; otherwise, I can write something up later. Thanks again! – Eric Cochran Mar 19 '17 at 07:32

1 Answers1

0

float has 6 to 9 decimal digits of precision.

In the first calculation, the float division happens last, so the full float precision is there.

In the second calculation, the float division occurs, and then the integer part is lobbed off, exposing the inaccuracy of the later digits.
The integer part was part of the precision, and now those digits are lost.

To see it better, try setting dividend to a larger number and see even less accuracy.

Thank you for the answer from @Andreas in the comments.

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91