Closed Opened 9 years ago Closed 8 years ago

# {Array,%TypedArray%}.prototype.{i,lastI}ndexOf should never return -0

Not set
normal

## Tracking

### ()

RESOLVED FIXED
mozilla47
Tracking Status
firefox47 --- fixed

## Attachments

### (1 file)

 4.28 KB, patch jorendorff : review+ Details | Diff | Splinter Review
```Section 5.2:

"Mathematical operations such as addition, subtraction, negation, multiplication, division, and the mathematical functions defined later in this clause should always be understood as computing exact mathematical results on mathematical real numbers, which unless otherwise noted do not include infinities and do not include a negative zero that is distinguished from positive zero."

"The mathematical function min(x1, x2, ..., xN) produces the mathematically smallest of x1 through xN."

22.1.3.15: https://tc39.github.io/ecma262/#sec-array.prototype.lastindexof

"If n ≥ 0, let k be min(n, len - 1)."

At this point, even if n is -0, k is set to 0. Our implementation could be fixed by adding

if (k === 0)
k = 0;

at the right point.

Pretty awful.```
```Simple testcase:

assertEq([0].lastIndexOf(0, -0), +0);```
```And also

js> assertEq([0].indexOf(0, -0), +0);
typein:2:1 Error: Assertion failed: got -0, expected 0
Stack:
@typein:2:1

...except, not.  That algorithm simply says, "If n >= 0, then let k be n."  That doesn't pass the number through a mathematical operation, so it never converts to +0.  Spec bug?  Seems like the two functions should be consistent in never returning -0.  And indeed, bterlson notes https://github.com/tc39/ecma262/pull/316 just now in #jslang.

Here's a patch making the methods consistent.  We could avoid adding 0 until very slightly later, for some of this stuff, but I think it makes things slightly less scrutable overall.```
Attachment #8711256 - Flags: review?(jorendorff)
Assignee: nobody → jwalden+bmo
Status: NEW → ASSIGNED
Keywords: dev-doc-needed
```Comment on attachment 8711256 [details] [diff] [review]
{Array,%TypedArray%}.prototype.{i,lastI}ndexOf should never return -0

Review of attachment 8711256 [details] [diff] [review]:
-----------------------------------------------------------------

All right!```
Attachment #8711256 - Flags: review?(jorendorff) → review+
`https://hg.mozilla.org/integration/mozilla-inbound/rev/aa58a439e77a`
`https://hg.mozilla.org/mozilla-central/rev/aa58a439e77a`
Status: ASSIGNED → RESOLVED
Closed: 8 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla47
`https://hg.mozilla.org/mozilla-central/rev/aa58a439e77a`
```Mentioned in Firefox 47 for devs:
https://developer.mozilla.org/en-US/Firefox/Releases/47#JavaScript

Small compat note on the reference pages:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/indexOf
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/lastIndexOf```
Summary: Array.prototype.lastIndexOf should not return -0 → {Array,%TypedArray%}.prototype.{i,lastI}ndexOf should never return -0
You need to log in before you can comment on or make changes to this bug.

Creator:
Created:
Updated:
Size: