Skip to content Skip to sidebar Skip to footer

Differences Between 0, -0 And +0

I never understood why -0 treated separately than 0. The interesting fact is that 0 is equal with -0 > 0 === -0 true Then, the question is: why is -0 treated separately than 0

Solution 1:

"Branch Cuts for Complex Elementary Functions or Much Ado About Nothing's Sign Bit " addresses the reasons for signed zeros. That kind of analysis informed IEEE-754 which is the basis for most modern instruction sets and programming languages' floating point number behavior.

In brief, many common numeric functions can be continuous with signed zeroes at places where they cannot be with an unsigned zero leading to fewer NaN values and fewer special cases. Division is one such function.


Then, the question is: why is -0 treated separately than 0 and +0?

Just to be clear, there are only two zero values. -0 and +0. The tokens (0) can be substituted for the tokens (+0) wherever they occur without changing semantics.


The interesting fact is that 0 is equal with -0

0 === -0
true

This behavior is mandated by IEEE-754.

To test whether two numeric values are the "same":

function same(x, y) {
  if (x === y) {
    if (x !== 0) {
      returntrue;  // Non-zero values.
    } else {
      return (1/x === 1/y);  // Test signed-ness of zeroes.
    }
  } else {
    return x !== x && y !== y;  // Treat NaNs the same
  }
}

Post a Comment for "Differences Between 0, -0 And +0"