在 Java 中添加两个 Double.NEGATIVE_INFINTIY 对象
看一下 Java 中的这段代码:
double alpha = alphaFactors.get(0, q);
double beta = betaFactors.get(0, q);
if ((alpha + beta) > Double.NEGATIVE_INFINITY) {
initialDistributionStripe.put(new IntWritable(q),
new DoubleWritable(alpha + beta));
}
为了避免垃圾值,我想将总和 (alpha + beta) 添加到initialDistributionStripe 映射中,当且仅当它大于 Double.NEGATIVE_INFINITY 并且不等于 NaN 时。
我相信我所做的事情是正确的,我不需要显式检查“NaN”,因为根据 IEEE 754 和 Java 规范,任何与 NaN 的比较都会导致错误。因此,如果 alpha + beta 为 NaN,则 ((alpha + beta) > Double.NEGATIVE_INFINITY)
将为 false。
我的推理正确吗?
Have a look at this snippet in Java:
double alpha = alphaFactors.get(0, q);
double beta = betaFactors.get(0, q);
if ((alpha + beta) > Double.NEGATIVE_INFINITY) {
initialDistributionStripe.put(new IntWritable(q),
new DoubleWritable(alpha + beta));
}
To avoid garbage values, I want to add to the initialDistributionStripe map the sum (alpha + beta) if and only if it is larger than Double.NEGATIVE_INFINITY, and is not equal to NaN.
I believe what I am doing is correct and I don't need to explicitly check for 'NaN' because according to the IEEE 754 and Java spec, any comparisons against NaN result in false. So if alpha + beta is NaN, then ((alpha + beta) > Double.NEGATIVE_INFINITY)
will be false.
Is my reasoning correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是正确的。
如果你想明确说明这一点,你可以添加
&& !Double.isNaN(alpha + beta)
(请记住,alpha + beta != Double.NaN
甚至是true
尽管alpha + beta
确实是Double.NaN
)。That's correct.
If you want to be explicit about it, you could add
&& !Double.isNaN(alpha + beta)
(Keep in mind thatalpha + beta != Double.NaN
istrue
even thoughalpha + beta
is indeedDouble.NaN
).无论如何,我都会使用 isNaN() 显式检查 NaN。更安全。
I would explicitly check for NaN anyway with isNaN(). Safer.
即使它做了同样的事情,也许下面的内容会更清楚。
否则你的逻辑似乎是正确的。
Perhaps the following is clearer even if it does the same thing.
Otherwise your logic appears to be correct.