Java's Math.IEEERemainder function states:
The remainder value is mathematically equal to f1 - f2 × n, where n is the mathematical integer closest to the exact mathematical value of the quotient f1/f2, and if two mathematical integers are equally close to f1/f2, then n is the integer that is even
For the following:
double f1 = 0.1;
double f2 = 0.04;
System.out.println(Math.IEEEremainder(f1, f2));
The output is -0.019999999999999997
However, 0.1/0.04 = 2.5
which is equidistant from both the integers 2
and 3
. Shouldn't we pick n = 2
here, resulting in 0.1 - 0.04*2 = 0.02
, instead of -0.02
?
See: Is floating point math broken?
You would think that 0.1 / 0.04
would return exactly 2.5
, but that's not true. According to this article, 0.1
cannot be accurately represented using IEEE 754, and is actually represented as 0.100000000000000005551...
.
In this case, the quotient is slightly higher due to that minuscule offset, which results in a value of 3
for n
, as it's no longer equidistant between 2
and 3
.
Computing it results in the following:
0.1 - 0.04 * 3 = 0.1 - 0.12 = -0.02 ~= -0.019999999999999997