Lex,
Just playing around with some sample code and comparing it to the real IEEEremainder results, I think I've come up with a function that correctly implements IEEEremainder() using '%',
can anyone spot the flaw?
public static double IEEEremainder(double f1, double f2) {
double r = f1 % f2;
if(Double.isNaN(r) || r == f2 || r <= f2 / 2.0) {
return r;
}
else {
return r - f2;
}
}
Essentially, if the remainder is greater than half the modulus, what happens with the real IEEEremainder function is that it wants to bump the quotient by +1.
As an example. 8.2 % 3.2 returns = 1.799999. That's because 8.2 can be written as 3.2 * q + r = 8.2, where q=2, and r = 1.799999. That is, 3.2 evenly divides 6.4 2 times. But
IEEEremainder prefers q=3, r=-1.4000000000, or, 3.2 divides 9.6 with a remainder of -1.40000, we get 8.4.
As we can see, 8.2 is closer to 9.6 than to 6.4, so IEEEremainder would prefer that the numerator is 'rounded up' to 9.6, but another way to look at it is whether the remainder is greater than 1/2 the modulus. If so, it means that the numerator is closest to the next higher multiple of the modulus.
Thus, we simple check if (f1 % f2) > f2 / 2. If so, we pretend that the q=q+1, and recompute r.
If you think that this logic is right, I will go ahead and resubmit another patch, with implementations of both IEEEremainder and getExpontent.
-Ray