How to test if a double is zero?

Java

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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 exactly zero works great.

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

Otherwise when checking if a floating point value like double or float is 0, an error threshold is used to detect if the value is near 0, but not quite 0.

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

Solution 2 - Java

Yes; all primitive numeric types default to 0.

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

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

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

Solution 3 - Java

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 == 0 and foo.x == 0.0 are both true if foo.x is zero

Solution 4 - Java

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.

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

Solution 5 - Java

The safest way would be bitwise OR ing your double with 0. Look at this https://stackoverflow.com/questions/4211095/xoring-two-doubles-in-java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser291701View Question on Stackoverflow
Solution 1 - JavaWilliam MorrisonView Answer on Stackoverflow
Solution 2 - JavaSLaksView Answer on Stackoverflow
Solution 3 - JavaPenguinAgenView Answer on Stackoverflow
Solution 4 - JavaXabsterView Answer on Stackoverflow
Solution 5 - JavaAnshulView Answer on Stackoverflow