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

(-1) % 1 evaluates to +0, not -0 #2611

Closed
kurgm opened this issue Jan 8, 2022 · 4 comments · Fixed by #2617
Closed

(-1) % 1 evaluates to +0, not -0 #2611

kurgm opened this issue Jan 8, 2022 · 4 comments · Fixed by #2617

Comments

@kurgm
Copy link
Contributor

kurgm commented Jan 8, 2022

Description: The abstract operation Number::remainder(n, d) returns +0𝔽 when n and d are finite and non-zero and n is divisible by d, even if n is negative. For example, when n = -1 and d = 1, algorithm step 8 returns 𝔽(0), which is +0𝔽. Accordingly, the expression (-1) % 1 evaluates to +0𝔽. This behavior is different from that of ES6 (and JS engines) in which it evaluates to -0 by the following rule in 12.6.3.3 Applying the % Operator:

The sign of the result equals the sign of the dividend.

eshost Output:

$ eshost -s -e "Object.is(-0, (-1) % 1)"
#### ChakraCore, SpiderMonkey, V8
true
@ljharb
Copy link
Member

ljharb commented Jan 8, 2022

https://tc39.es/ecma262/#sec-numeric-types-number-remainder suggests to me that it would reach step 7 with n as -1𝔽 and d as 1𝔽.

It seems like you're saying that step 7 produces a mathematical zero, and that step 8, for the case when n and d have the opposite sign and when r is 0, it should explicitly return -0𝔽?

@kurgm
Copy link
Contributor Author

kurgm commented Jan 8, 2022

It seems like you're saying that step 7 produces a mathematical zero,

Yes exactly.

and that step 8, for the case when n and d have the opposite sign and when r is 0, it should explicitly return -0𝔽?

I think it's not when n and d have the opposite sign, but when n is negative (and when r is zero).

Thank you for the quick reply!

@michaelficarra
Copy link
Member

This was accidentally introduced in #2189. It looks like we should insert a step after 7 like

1. If _r_ is 0 and _n_ is negative, return *-0*<sub>𝔽</sub>.

@kurgm Would you like to submit a PR?

ljharb added a commit to ljharb/es-abstract that referenced this issue Jan 10, 2022
@kurgm
Copy link
Contributor Author

kurgm commented Jan 11, 2022

Yes, I'll do.

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 a pull request may close this issue.

3 participants