Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add +0 to Array.prototype.indexOf to convert negative zero to positive zero #316

Closed
wants to merge 1 commit into from

Conversation

anba
Copy link
Contributor

@anba anba commented Jan 22, 2016

@bterlson
Copy link
Member

Chakra seems to do this, but no other impls I can test convert -0 to +0.

print("indexOf returns +0: " + (1/[true].indexOf(true, -0) > 0));
print("lastIndexOf returns +0: " + (1/[true].lastIndexOf(true, -0) > 0));

chakra

indexOf returns +0: true
lastIndexOf returns +0: true

spidermonkey

indexOf returns +0: false
lastIndexOf returns +0: false

d8

indexOf returns +0: false
lastIndexOf returns +0: false

node

indexOf returns +0: false
lastIndexOf returns +0: false

@anba
Copy link
Contributor Author

anba commented Jan 22, 2016

This is the fix discussed last October in tc39/test262#435 (comment).

JSC and Nashorn also return +0.

@@ -29809,7 +29809,8 @@
1. Let _n_ be ? ToInteger(_fromIndex_). (If _fromIndex_ is *undefined*, this step produces the value 0.)
1. If _n_ ≥ _len_, return -1.
1. If _n_ ≥ 0, then
1. Let _k_ be _n_.
1. Let _k_ be _n_ + (*+0*).
1. NOTE: Adding a positive zero converts *-0* to *+0*.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than a note, would it make more sense to just have "if n is -0, let k be +0, else let k be n"? Adding +0 to -0 to get +0 seems like an irrelevant implementation detail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly agree!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I'll also update 20.3.1.15 TimeClip to use an extra step.

@anba
Copy link
Contributor Author

anba commented Feb 3, 2016

Updated the PR per the review comments, negative zero is now handled with an explicit step.

@ljharb
Copy link
Member

ljharb commented Feb 3, 2016

thanks, much clearer imo, LGTM!

@bterlson bterlson closed this in 15ad138 Feb 4, 2016
ljharb added a commit to paulmillr/es6-shim that referenced this pull request Feb 16, 2016
@anba anba deleted the indexof-negative-zero branch March 1, 2016 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants