开发者

Adding two Double.NEGATIVE_INFINTIY objects in Java

Have a look at this snippet in Java:

double alpha = alphaFactors.get(0, q);
double beta = betaFactors.get(0, q);
if ((alpha + be开发者_开发问答ta) > 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?


So if alpha + beta is NaN, then ((alpha + beta) > Double.NEGATIVE_INFINITY) will be false

That's correct.

If you want to be explicit about it, you could add && !Double.isNaN(alpha + beta) (Keep in mind that alpha + beta != Double.NaN is true even though alpha + beta is indeed Double.NaN).


I would explicitly check for NaN anyway with isNaN(). Safer.


Perhaps the following is clearer even if it does the same thing.

if (alpha > Double.NEGATIVE_INFINITY && beta > Double.NEGATIVE_INFINITY)

Otherwise your logic appears to be correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜