Is -0.0 equal to 0.0

On my Mac if I perform certain numerical computation in Scala, I get a result of -0.0.
Is this a Scala feature, or a feature of the particular jvm I’m using, or is this a feature of the
processor the Mac is using for floating point computation?

Am I safe on any architecture to suppose that -0.0 == 0.0? It seems anecdotally to be the case for me.

It depends on the representation used for binary numbers. As far as I know it happens in sign and mantissa, one’s complement and IEEE 754 floating point. Regarding the latter, the equality test is defined to be true but they are different numbers which can lead to surprises during some calculations. For the JVM more info can be found here Chapter 2. The Structure of the Java Virtual Machine.

1 Like