[... -0 in string conversion ...]

   (-0).toFixed(0) -> "0"

Is this a bug in the spec for toFixed?

I would say it's deeper than that. It's about JavaScript's philosophy towards -0. Particularly as expressed in the ToString internal function (*). If ToString is fixed then single-element Array conversion and JSON stringification work right.

(*) I.e. steps 2 & 3 of ToString as shown in section 9.8.1 "ToString Applied to the Number Type".

If we accept the need for -0 -- and the precise & correct care taken in Math and arithmetic shows that we do -- then why go off the rails when it comes to string conversion?

In the specific case of Number.prototype.toFixed you could change step 6 to "If x is negative, then", providing the term "negative" is suitably defined. The same goes for Number.prototype.toExponential etc etc.

Note the broken symmetry:
ToString is surjective but not injective; ToNumber is both surjective and injective.
ToString and ToNumber are not inverses:
  ToNumber("-0")  -> -0
but
  ToString(-0)  -> "0"


Of course, in the language (-0 < 0) is false. See 5.2 Algorithm Conventions.

It is mathematically proper than (-0 < 0) is false. Only if one imposes a Total Order over the set of Number does that expression become true. (IEEE754 has a cmpTotalOrder function which does this, and includes normal and signaling NaNs, and can include NaN payloads if anybody cares).

Am I missing something: Does section 5.2 actually say anything worthwhile at all?
Flippantly, it seems to be no more than:
"Abstract mathematical arithmetic and functions have exact values, but a finite machine can only deal with floating-point approximations. This standard does limited-precision floating-point not impossible infinite-precision because we cannot wait an eternity for the result and there is not enough energy or matter in the Universe to compute or store an infinitely precise result. Intermediate results will be rounded. Buy a textbook on Numerical Analysis, or take your chances."


_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to