I was reading the ECMAScript 5.1 spec. It says:
The
slicemethod 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,
num > 0, then num it is positivenum < 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?
Your confusion is in this part:
But what about
+0and-0? In math there is a single0, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With