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

RESOLVED FIXED in Firefox 47

RESOLVED FIXED
3 years ago
3 years ago

## Tracking

### ({dev-doc-complete})

unspecified
mozilla47
Points:
---
Dependency tree / graph

## Attachments

### (1 attachment)

 4.28 KB, patch jorendorff : review+ Details | Diff | Splinter Review
Reporter

### Description

3 years ago
```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.```
Assignee

### Comment 1

3 years ago
```Simple testcase:

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

### Comment 2

3 years ago
```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

### Updated

3 years ago
Assignee: nobody → jwalden+bmo
Status: NEW → ASSIGNED

### Updated

3 years ago
Keywords: dev-doc-needed
Reporter

### Comment 3

3 years ago
```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+

### Comment 4

3 years ago
`https://hg.mozilla.org/integration/mozilla-inbound/rev/aa58a439e77a`

### Comment 5

3 years ago
bugherder
`https://hg.mozilla.org/mozilla-central/rev/aa58a439e77a`
Status: ASSIGNED → RESOLVED
Last Resolved: 3 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla47

### Comment 6

3 years ago
bugherder
`https://hg.mozilla.org/mozilla-central/rev/aa58a439e77a`

### Comment 7

3 years ago
```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.