题
在下面的程序中,结果是 0.0
被认为小于 Double.MIN_VALUE
. 。为什么?
我们有一个解决方案(与 Doubles
仅使用 compareTo
),我想了解为什么在这里拆箱失败。
import java.util.Date;
import java.util.Calendar;
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) {
double max = 99999.9999;
double min = Double.MIN_VALUE;
Double test = 0.0;
System.out.println(max > test); // expect true; is true
System.out.println(test > min); // expect true; is false
}
}
解决方案
其他提示
你应该阅读 double.min_value 规格。这是最小可能但正值的最小值,这意味着它大于0.0。
A constant holding the smallest positive nonzero value of type double, 2-1074.
It is equal to the hexadecimal floating-point literal 0x0.0000000000001P-1022
and also equal to Double.longBitsToDouble(0x1L).
Double.MIN_VALUE = 4.9E-324
这仍然是一个正数。我想你在寻找 min = - Double.MAX_VALUE
据我说,自动氧化没有问题。也许您只需要使用double.negative_infinity或double.posive_infinity,它应该与<和>运算符配合使用。例如,请注意
-Double.MAX_VALUE > Double.NEGATIVE_INFINITY是真的!
不隶属于 StackOverflow