What does positive and negative mean in ECMAScript? +0 and -0

I read the ECMAScript 5.1 specification . It says:

The slice method takes two arguments: start and end [...]. If the beginning is negative, it is considered as length + start, where length is the length of the array. If the end is negative, it is considered as length + end, the length of which is equal to the length of the array.

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

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

But what about +0 and -0 ? In mathematics, there is one 0 that is not positive and negative. I assumed in ECMAScript

  • +0 (aka positive zero) is positive.
  • -0 (negative zero aka) is negative.

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

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

Where is the positive or negative of a certain number? I did not find this in the ECMAScript specification. Is the definition inherited from IEEE 754?

+7
javascript language-lawyer specifications
source share
3 answers

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

8.5 Room Type

The type of number has exactly 18437736874454810627 (that is, 2 64 -2 53 +3) values ​​[...]

9007199254740990 (ie 2 53 -2) The IEEE "Not-a-Number" values ​​are presented in ECMAScript as the only special NaN value [...]

There are two other special meanings called positive infinity and negative infinity [...]

The other 18437736874454810624 (that is, 2 64 -2 53 ) values ​​are called finite numbers. Half of them are positive numbers , and half are negative numbers ; for each final positive value of the number there is a corresponding negative value having the same value.

Thus,

  • Since +0 and -0 are two of these finite numbers, each must either be positive or negative.
  • Since each finite positive number must have a negative analogue, either +0 positive and -0 negative or vice versa.
  • This would be too trollic if a positive zero were a negative number and a negative zero was positive. Thus, we can (possibly) assume that +0 is positive and -0 negative.

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

5.2 Algorithm Conventions

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

In fact, in most cases, the specification seems to distinguish the case where the variable is positive or negative from the case when it is zero. For example,

5.2 Algorithm Conventions

The sign of the mathematical function (x) gives 1 if x is positive and -1 if x is negative . The sign function is not used in this standard for cases where x is zero .

Therefore, the specification is inconsistent.

-one
source share

Your confusion in this part:

But what about +0 and -0 ? In mathematics, there is one 0 that is not positive and negative. I assumed in ECMAScript

  • +0 (aka positive zero) is positive.
  • -0 (negative zero aka) is negative.

+0 not positive; -0 not negative. It is understood that both represent the number zero or, when an overflow occurs, any number with a small value, to be represented with a finite number of bits available.

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

+3
source share

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 in the memory view, respectively, for:

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

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

If you see the literal -0 or +0 in the code, this will be interpreted as a real number 0, which is saved (like any fairly small, but not actually 0 real number), like Number -0 or +0 , depending on the circumstances.

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

+2
source share

All Articles