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

Math.atan2() should not return "inverse tangent of ny / nx" #2897

Closed
Josh-Cena opened this issue Sep 5, 2022 · 4 comments · Fixed by #3170
Closed

Math.atan2() should not return "inverse tangent of ny / nx" #2897

Josh-Cena opened this issue Sep 5, 2022 · 4 comments · Fixed by #3170

Comments

@Josh-Cena
Copy link
Contributor

Josh-Cena commented Sep 5, 2022

Description: Math.atan2(1, -1), according to the spec, should be equivalent to Math.atan(1 / -1), because it falls into step 12:

  1. Return an implementation-approximated Number value representing the result of the inverse tangent of the quotient (ny) / (nx).

"Inverse tangent" is commonly defined to have range $\displaystyle \left[-\frac{\pi}{2},\frac{\pi}{2}\right]$, which is also consistent with the behavior of Math.atan() (which is specified with the exact same wording). However, for x < 0, Math.atan2() does not use this range, and thus should be specified separately.

eshost Output:

$ eshost -s -e "Math.atan2(1, -1)"
#### JavaScriptCore, SpiderMonkey, V8
2.356194490192345
@jmdyck
Copy link
Collaborator

jmdyck commented Sep 10, 2023

Step 12 was introduced by PR #2122. Before that, the specification of Math.atan2 was:

Returns an implementation-approximated value representing the result of the arc tangent of the quotient y / x of the arguments y and x, where the signs of y and x are used to determine the quadrant of the result. [...] The result is expressed in radians and ranges from -π to +π.

(plus a bunch of edge-cases for which particular values were required).

PR #2122 should perhaps have used wording closer to that for step 12.

@Josh-Cena
Copy link
Contributor Author

Returns an implementation-approximated value representing the result of the arc tangent of the quotient y / x of the arguments y and x, where the signs of y and x are used to determine the quadrant of the result. [...] The result is expressed in radians and ranges from -π to +π.

I find this quite vague, isn't it? It doesn't tell me which quadrant the result is in, for each sign combination.

@jmdyck
Copy link
Collaborator

jmdyck commented Sep 10, 2023

Isn't it obvious? E.g., if y is positive and x is negative, then it's in the quadrant defined by y > 0 and x < 0.

@Josh-Cena
Copy link
Contributor Author

Ah, I see what it means. It still feels weird, though, because that deviates from what "arc tangent" usually means in terms of a well-defined function. I wonder if it isn't better to simply transform the values using $\pi - \arctan\frac{y}{x}$.

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.

2 participants