-1

The following documentation pages on the MDN suggest that JavaScript differentiates between the values 0 and -0.

  1. Equality Comparisons and Sameness
  2. Object.is()

My questions about this are:

  1. Most computer systems follow two's complement, which does not have two different representations of zero. So, how and where does JavaScript keep track of the sign of the value 0?

  2. In what contexts does the differentiation assume importance?

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • 1
    Two's complement applies to integers. JS doesn't have integers. – melpomene Jun 28 '18 at 10:06
  • @melpomene That does throw some light on my question. You seem to suggest that `Number` is an implementation detail, possibly a `struct` in the JS engines that use C/C++ style languages. And underlying operations are obviously converted into integers for integrals. – Water Cooler v2 Jun 28 '18 at 10:09
  • @melpomene The dupe you linked only states what is; it doesn't say why. – Water Cooler v2 Jun 28 '18 at 10:11
  • Uh, what? JS uses double-precision floating point numbers. – melpomene Jun 28 '18 at 10:11
  • @melpomene I said *for integrals*. – Water Cooler v2 Jun 28 '18 at 10:12
  • "*underlying operations are obviously converted into integers for [integral values]*"? What do you mean by that? How do you convert an operation into an integer? – melpomene Jun 28 '18 at 10:15
  • Your questions are 1. How / where? and 2. In what contexts?, not "why?". – melpomene Jun 28 '18 at 10:16
  • I meant that he seemed to suggest that `Number` holds it all like so: `struct Number { DWORD hiDwordOfSignedLong; DWORD loDWordOfSignedLong; bool signed; bool isFloat; DWORD characteristic; DWORD mantissa; ... }`. And if the underlying value happened to be an integral value, i.e it happened not to have any fraction/decimal part, then it would be arithmetically operated upon using two's complement. – Water Cooler v2 Jun 28 '18 at 10:23
  • I don't know where you're getting this from. It's just `double number;`. – melpomene Jun 28 '18 at 10:27

1 Answers1

1

The MDN documentation page about numbers explains:

In JavaScript, all numbers are implemented in double-precision 64-bit binary format IEEE 754 (i.e. a number between -(253-1) and 253-1). There is no specific type for integers.

The underlying implementation of double-precision floating-point format IEEE 754 is the one that provides two representations for zero (the two representations are also known as signed zero). The presence of two different encodings for 0 is useful for some numerical algorithms.

However, for the general use their value is the same, even if they are different objects:

console.log('Object.is(-0, +0):', Object.is(-0, +0))
console.log('-0 ==  +0:', (-0 ==  +0))
console.log('-0 === +0:', (-0 === +0))

Object.is()

Object.is() works with any values, not only with numbers. Its purpose is to check if two variables reference the same object, not different objects that, when compared using the comparison operators, look identical.

Object.is() is useful to detect aliased objects. For example:

let a = { i: 0 }
let b = a

console.log(Object.is(a, b))
// true

a.i = 1
console.log(b.i)
// 1

The code changes the properties of a but the properties of b are also affected because both variables a and b refer to the same object.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • So, the division is only for the purposes of abiding by the floating point spec? Why was `Object.is()` added then? – Water Cooler v2 Jun 28 '18 at 10:36
  • About `Object.is()`, read the updated answer. I would say that the presence of `+0` and `-0` in JavaScript is a side-effect of storing all numbers using the IEEE 754 floating-point format, not a deliberate decision of the JavaScript implementors or designers. – axiac Jun 28 '18 at 10:56
  • But both, the *abstract equality comparison algorithm* (`==`) and the *strict equality comparison algorithm* (`===`) also do exactly that for objects, that is, they check object references (unless one of the comparand happens to be a primitive). – Water Cooler v2 Jun 28 '18 at 11:00