Meaning of Double.doubleToLongBits(x)

前端 未结 3 528
傲寒
傲寒 2020-12-18 22:02

I am writing a class Vec2D, representing a 2 dimensional vector. I store x and y in doubles.

When asked to genera

3条回答
  •  清酒与你
    2020-12-18 23:00

    Because == and != follow IEEE-754 semantics for doubles, Double.NaN != Double.NaN and 0.0 == -0.0. These behaviors may not be what you want, so Double.doubleToLongBits() converts the 64 bits of double data to 64 bits of long data so that operations like bit shifts and XOR work.

    Honestly, though, I would say that the use of doubleToLongBits is a bug here, since if you care about exact equality you should be using Double.doubleToRawLongBits() (which does not perform any translations on the double data at all) instead.

提交回复
热议问题