7

I was reading the ECMAScript 5.1 spec. It says:

The slice method takes two arguments, start and end [...]. If start is negative, it is treated as length+start where length is the length of the array. If end is negative, it is treated as length+end where length is the length of the array.

What does "negative" mean? It makes sense that, like in math,

  • If num > 0, then num it is positive
  • If num < 0, then num is negative.

But what about +0 and -0? In math there is a single 0, which is not positive nor negative. My guess was that, in ECMAScript,

  • +0 (a.k.a. positive zero) is positive.
  • -0 (a.k.a. negative zero) is negative.

But I tried using -0 with slice, and browsers treat it as non-negative.

Then, are both +0 and -0 non-positive and non-negative, despite their names?

Where is the positiveness or negativeness of a number defined? I didn't find that defined in the ECMAScript spec. Is the definition inherited from IEEE 754?

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • Interestingly, `Math.abs(-0) === -0` is true (in Chrome). – ssube Feb 24 '15 at 18:53
  • 1
    @ssube So is `0 === -0`. – Alexander O'Mara Feb 24 '15 at 18:54
  • @ssube Yes, `0 === -0`. [The Strict Equality Comparison Algorithm](http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6) doesn't differentiate them. [The SameValue Algorithm](http://www.ecma-international.org/ecma-262/5.1/#sec-9.12) does. In ECMAScript 6, `Object.is(0, -0)` is `false`. – Oriol Feb 24 '15 at 18:55
  • Related: [Are +0 and -0 the same?](http://stackoverflow.com/questions/7223359/are-0-and-0-the-same) – Alexander O'Mara Feb 24 '15 at 18:56
  • 1
    An interesting question. I suppose `negative` is treated as `'less than zero'` in this case. But for `-0 * 0` (and similar arithmetic ops), it's clear that the sign does matter. ) – raina77ow Feb 24 '15 at 19:03

3 Answers3

3

Your confusion is in this part:

But what about +0 and -0? In math there is a single 0, which is not positive nor negative. My guess was that, in ECMAScript,

  • +0 (a.k.a. positive zero) is positive.
  • -0 (a.k.a. negative zero) is negative.

+0 is not positive; -0 is not negative. Conceptually they both represent the number zero or, when underflow occurs, any number with a magnitude too small to be represented with the finite number of bits available.

The decision to have +0 and -0 comes more from IEEE than from ECMA.

Community
  • 1
  • 1
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
2

Things can be confusing if you don't distinguish between the literals +0 and -0, which represent the mathematical value 0, and the values +0 and -0, which are the in memory representation, respectively, for:

  1. Any mathematical value from 0 to the smallest positive real number that can be stored in the double precision 64-bit data format
  2. Any mathematical value from the largest negative real number that can be stored in the double precision 64-bit data format to 0

If you have a variable containing the Number instance -0, this could be representing the real number 0 (which obviously has no sign), or it could be representing the real number 10^-10000.

If you see the literal -0 or +0 in code, this will be interpreted as the real number 0, which is stored (just like any sufficiently tiny but not actually 0 real number) as the Number -0 or +0, as the case may be.

Here are some relevant sections from the spec that will hopefully clarify things:

  1. Numeric literals
  2. The Number type
  3. Algorithm conventions
  4. Why -0===+0
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • Then, is the positiveness or negativeness of an ECMAScript number defined as the positiveness or negativeness of its mathematical value? So, since the value of both `+0` and `-0` is 0, they are not positive nor negative? – Oriol Feb 24 '15 at 19:13
  • @Oriol: Right below the cited section there is used the annotation "*… if x is negative (less than zero) …*". I would indeed conclude that `-0` and `+0` are neither negative nor positive, if they were concerned the spec used the explicit term "nonnegative" instead of "positive". – Bergi Feb 24 '15 at 19:27
  • @Oriol I've expanded my answer. Hopefully that will help explain things a bit better. – Asad Saeeduddin Feb 24 '15 at 19:41
  • Not sure if the difference between a literal and the memory representation matters. The literal `0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001` behaves exactly as `+0`, because they are both stored as `+0`. – Oriol Feb 25 '15 at 00:13
  • @Oriol The difference between a literal and the memory representation matters precisely because the that tiny literal is actually stored in memory as `+0`. That `+0` in memory represents the real number 10^-x, where x is however many decimal places there are there. If on the other hand you literally typed `+0` or `-0`, both of those represent a different real number, 0, yet are also stored in memory as `+0` or `-0`, respectively. At this point it isn't entirely clear what your question is: maybe it would be easier to explain if you restated what you're asking in more formal terms than +veness. – Asad Saeeduddin Feb 25 '15 at 01:01
0

The terms "positive number" and "negative number" are indeed defined in the ECMAScript spec:

8.5 The Number Type

The Number type has exactly 18437736874454810627 (that is, 264−253+3) values [...]

The 9007199254740990 (that is, 253−2) distinct “Not-a-Number” values of the IEEE Standard are represented in ECMAScript as a single special NaN value [...]

There are two other special values, called positive Infinity and negative Infinity [...]

The other 18437736874454810624 (that is, 264−253) values are called the finite numbers. Half of these are positive numbers and half are negative numbers; for every finite positive Number value there is a corresponding negative value having the same magnitude.

Therefore,

  • Since +0 and -0 are two of those finite numbers, each must either be positive or negative.
  • Since each finite positive number must have a negative counterpart, either +0 is positive and -0 is negative, or the opposite.
  • It would be too trollish if the positive zero was a negative number, and negative zero was a positive one. So we can (probably) assume that +0 is positive and -0 is negative.

However, according to the following, neither +0 nor -0 can be negative:

5.2 Algorithm Conventions

The mathematical function abs(x) yields the absolute value of x, which is −x if x is negative (less than zero) and otherwise is x itself.

In fact, in most cases the spec seems to differentiate the case when a variable is positive or negative from the case when it's zero. For example,

5.2 Algorithm Conventions

The mathematical function sign(x) yields 1 if x is positive and −1 if x is negative. The sign function is not used in this standard for cases when x is zero.

Therefore, the spec is contradictory.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • For `slice` (and all other sequence methods), the meaning of *less than zero* is used though. – Bergi Feb 25 '15 at 00:54
  • How did you conclude that your second quote implies that "neither `+0` nor `-0` can be negative"? The quote mentions nothing about `+0` or `-0`. There is no contradiction here. – Asad Saeeduddin Feb 25 '15 at 01:14
  • `-0` can represent numbers less than 0, (eg. `-1^1000000000`). In such a case, abs yields `+0`, which is perfectly consistent with what is described in the quote and everything else in the spec. – Asad Saeeduddin Feb 25 '15 at 01:17
  • @Asad The second quote says that negative numbers are less than zero. And both `+0` or `- 0` correspond to the math value zero, which is not less than zero. So they can't be negative. Are you saying that implementations can know if a `-0` in memory comes from a literal `-0` or a negative number very close to zero? Wouldn't that additional information need to be stored somewhere, and then require more than 64 bits? – Oriol Feb 25 '15 at 12:57
  • @Oriol Implementations **can't** know whether the `-0` in memory comes from a literal `-0` or from some really small number. So the `-0` in memory could represent a really small negative number, just as it could represent 0. – Asad Saeeduddin Feb 25 '15 at 16:02
  • Here's an example: The correct value for the abs function, when operating on a representation of a negative real number, is the representation of the corresponding positive real number. An example of this is the number -1e-10000 ∈R, which is represented in memory by `-0`. Using the abs function should return the representation of 1e-10000 ∈R, which is `+0`. This is exactly what happens. – Asad Saeeduddin Feb 25 '15 at 16:03