Java 如何测试双精度值是否为零?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18260213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:48:15  来源:igfitidea点击:

How to test if a double is zero?

java

提问by user291701

I have some code like this:

我有一些这样的代码:

class Foo {
    public double x;
}

void test() {
    Foo foo = new Foo();

    // Is this a valid way to test for zero? 'x' hasn't been set to anything yet.
    if (foo.x == 0) {

    }

    foo.x = 0.0;

    // Will the same test be valid?
    if (foo.x == 0) {

    }
}

I basically want to avoid a divide-by-zero exception in the future.

我基本上想避免将来出现被零除的异常。

Thanks

谢谢

采纳答案by William Morrison

Numeric primitives in class scope are initialized to zero when not explicitly initialized.

未显式初始化时,类范围内的数字原语初始化为零。

Numeric primitives in local scope (variables in methods) must be explicitly initialized.

本地范围内的数字原语(方法中的变量)必须显式初始化。

If you are only worried about division by zero exceptions, checking that your double is not exactlyzero works great.

如果您只担心除以零异常,检查您的 double 是否不完全为零非常有效。

if(value != 0)
    //divide by value is safe when value is not exactly zero.

Otherwisewhen checking if a floating point value like doubleor floatis 0, an error threshold is used to detect if the value is near 0, but not quite0.

否则,检查时,如果一个浮点值等doublefloat为0时,用于检测该值是否接近0的误差阈值,但不是0。

public boolean isZero(double value, double threshold){
    return value >= -threshold && value <= threshold;
}

回答by SLaks

Yes; all primitive numeric types default to 0.

是的; 所有原始数字类型默认为0.

However, calculations involving floating-point types (doubleand float) can be imprecise, so it's usually better to check whether it's closeto 0:

但是,涉及浮点类型(计算doublefloat)可以是不精确的,所以它通常是更好地检查它是否接近0

if (Math.abs(foo.x) < 2 * Double.MIN_VALUE)

You need to pick a margin of error, which is not simple.

您需要选择一个误差幅度,这并不简单。

回答by Xabster

Yes, it's a valid test although there's an implicit conversion from int to double. For clarity/simplicity you should use (foo.x == 0.0) to test. That will hinder NAN errors/division by zero, but the double value can in some cases be very very very close to 0, but not exactly zero, and then the test will fail (I'm talking about in general now, not your code). Division by that will give huge numbers.

是的,尽管存在从 int 到 double 的隐式转换,但它是一个有效的测试。为了清晰/简单起见,您应该使用 (foo.x == 0.0) 进行测试。这将阻碍 NAN 错误/除以零,但在某些情况下,double 值可能非常非常接近 0,但不完全为零,然后测试将失败(我现在谈论的是一般情况,而不是您的代码)。除以那个将给出巨大的数字。

If this has anything to do with money, do not use float or double, instead use BigDecimal.

如果这与金钱有关,请不要使用 float 或 double,而是使用 BigDecimal。

回答by Anshul

The safest way would be bitwise OR ing your double with 0. Look at this XORing two doubles in Java

最安全的方法是将双精度与 0 进行按位或运算。看看这个在 Java 中对两个双精度进行异或运算

Basically you should do if ((Double.doubleToRawLongBits(foo.x) | 0 ) )(if it is really 0)

基本上你应该这样做if ((Double.doubleToRawLongBits(foo.x) | 0 ) )(如果它真的是0)

回答by PenguinAgen

In Java, 0 is the same as 0.0, and doubles default to 0 (though many advise always setting them explicitly for improved readability). I have checked and foo.x == 0and foo.x == 0.0are both true if foo.xis zero

在 Java 中,0 与 0.0 相同,doubles 默认为 0(尽管许多人建议始终明确设置它们以提高可读性)。我已经检查过foo.x == 0,并foo.x == 0.0都为true,如果foo.x是零