archives

« Bugzilla Issues Index

#3581 — 7.1.4 ToInteger(-0.2) does not clearly produce -0 according to the algorithm


Step 5 of the ToInteger abstract operation reads:

5. Return the result of computing sign(number) × floor(abs(number)).

--

First interpretation: × is a mathematical operator:

sign, floor, and abs, as well as the multiplication, operate on mathematical finite real number (according to 5.2 Algorithm Conventions), where +0 = -0 = 0. Thus, when number = -0.2, the output of the mathematical expression is simply 0.

Then, there is an implicit operation in this step, which is "taking the Number value of the result"; and for 0, it is +0.

--

Second interpretation: × is part of the representation of a Number value as
s × m × 2^e:

In that interpretation the result is correct; but the fact that the m × 2^e part is expressed as a result of a mathematical expression and the use of "computing" in that step don't favor that interpretation.

--

It should be made clear both the fact we return a Number value (rather than a mathematical number), and (more importantly) that the sign is preserved even if the result is zero. For example:

5. Return the Number value of same sign as number and of magnitude floor(abs(number)).

--

(There is a similar issues for ToInt32, etc.; I'm opening another bug for those.)


Oh, this is very old text, probably going all the way back to ES1

It actually isn't obvious to me that that the intent was to return -0 for ToInteger(-0.2) as I think your first interpretation is quite plausable .

Also, based upon a quick can of all usages to ToInteger (especially the ones that date to ES3 or earlier) it isn't clear that anything in the spec. is actually sensitive to whether 0 or -1 is returned for the this case.

However, given that it is clearly returning number values (including -0) in other cases, I think it is probably correct to assume that ToInteger(-0.2) should return -0

so, fixed in rev32 editor's draft


fixed in rev32 draft