How to test if a double is zero?

前端 未结 5 994
别跟我提以往
别跟我提以往 2020-12-08 18:32

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\' ha         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-08 19:01

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

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

提交回复
热议问题