我认为要检查的直接情况是0,+无穷大,-无穷大和NaN:
static void check(double x) {
double a, b;
System.out.printf(
"%9s %9s %23s %5s%n",
x, a = x - Math.ulp(x), b = Math.nextAfter(x, Double.NEGATIVE_INFINITY), a == b);
System.out.printf(
"%9s %9s %23s %5s%n",
x, a = x + Math.ulp(x), b = Math.nextAfter(x, Double.POSITIVE_INFINITY), a == b);
System.out.println();
}
public static void main(String[] args) throws java.lang.Exception {
check(0);
check(Double.POSITIVE_INFINITY);
check(Double.NEGATIVE_INFINITY);
check(Double.NaN);
}
Ideone demo
输出:
0.0 -4.9E-324 -4.9E-324 true
0.0 4.9E-324 4.9E-324 true
Infinity NaN 1.7976931348623157E308 false
Infinity Infinity Infinity true
-Infinity -Infinity -Infinity true
-Infinity NaN -1.7976931348623157E308 false
NaN NaN NaN false
NaN NaN NaN false
在这种情况下,表达式不相等并不奇怪(根据NaN的定义);但这些表达式对于 +无穷大和 -无穷大也不是真的(请参阅最后一列)。NaN
此答案并非旨在提供有问题的值的详尽列表,而是表明确实存在一些有问题的值。