Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do "positive" and "negative" mean in ECMAScript? +0 and -0

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?

like image 866
Oriol Avatar asked Feb 24 '15 18:02

Oriol


1 Answers

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.

like image 118
Timothy Shields Avatar answered Sep 16 '22 11:09

Timothy Shields