public static double IEEEremainder(double f1, double f2) {
return StrictMath.IEEEremainder(f1, f2);
}
因此从StrictMath.IEEEremainder(f1, f2)
类调用Math.IEEEremainder
。
我从http://developer.classpath.org/doc/java/lang/StrictMath-source.html
找到了StrictMath.IEEEremainder(f1, f2)
代码是,我后来使用了这段代码:
public static double IEEEremainder(double x, double y) {
// Purge off exception values.
if (x == java.lang.Double.NEGATIVE_INFINITY || ! (x < java.lang.Double.POSITIVE_INFINITY) || y == 0 || y != y) return java.lang.Double.NaN;
boolean negative = x < 0;
x = Math.abs(x);
y = Math.abs(y);
if (x == y || x == 0) return 0 * x; // Get correct sign.
// Achieve x < 2y, then take first shot at remainder.
if (y < TWO_1023) x %= y + y;
// Now adjust x to get correct precision.
if (y < 4 / TWO_1023) {
if (x + x > y) {
x -= y;
if (x + x >= y) x -= y;
}
}
else {
y *= 0.5;
if (x > y) {
x -= y;
if (x >= y) x -= y;
}
}
return negative ? -x : x;
}
但问题是当我执行以下代码时
double d1 = 123.45 , d2 = 35.10;
1. System.out.println(StrictMath.IEEEremainder(d1, d2)); // -16.950000000000003
2. System.out.println(Math.IEEEremainder(d1, d2)); // -16.950000000000003
3. System.out.println(IEEEremainder(d1, d2)); // 18.150000000000002
第三输出与其他输出不同。对于第三个,我使用的代码来自http://developer.classpath.org/doc/java/lang/StrictMath-source.html
使用相同方法的不同输出的原因是什么?