为什么自动装箱/拆箱在这里失败?
在下面的程序中,结果是 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
}
}
In the program below, the result is that 0.0
is considered less than Double.MIN_VALUE
. Why?
We have a solution (work with Doubles
only and use compareTo
) and I want to understand why unboxing is failing here.
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 Javadocs :
换句话说,它大于0。
According to the Javadocs :
In other words, it is bigger than 0.
您应该阅读 Double.MIN_VALUE规格。
它是最小可能但正的 Double 值,这意味着它大于 0.0。
You should read the Double.MIN_VALUE specification.
It's a minimum possible but positive Double value which means it's larger than 0.0.
Double.MIN_VALUE = 4.9E-324
这仍然是一个正数。我认为您正在寻找min = - Double.MAX_VALUE
Double.MIN_VALUE = 4.9E-324
which is still a positive number. I think you are looking formin = - Double.MAX_VALUE
据我所知,自动装箱没有问题。
也许您只需要使用 Double.NEGATIVE_INFINITY 或 Double.POSITIVE_INFINITY 之类的东西,它们应该可以很好地与 < 配合使用。和>运营商。例如请注意
is true!
According to me autoboxing has no problems.
Perhaps you simply need to use something like Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY that should work well with the < and > operators. For example note that
is true!