Content-Length: 321867 | pFad | https://github.com/w3c/csswg-drafts/issues/4101

40 [css-values] Does tan(90deg) return +∞, −∞ or NaN? · Issue #4101 · w3c/csswg-drafts · GitHub
Skip to content

[css-values] Does tan(90deg) return +∞, −∞ or NaN? #4101

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

Closed
Loirooriol opened this issue Jul 10, 2019 · 10 comments
Closed

[css-values] Does tan(90deg) return +∞, −∞ or NaN? #4101

Loirooriol opened this issue Jul 10, 2019 · 10 comments
Labels
css-values-4 Current Work

Comments

@Loirooriol
Copy link
Contributor

From https://drafts.csswg.org/css-values/#funcdef-tan,

tan() can return any number between +∞ and −∞.

But it's not clear to me what tan(90deg) is supposed to return. Mathematically, the limit from the left is +∞ and the limit from the right is −∞, so the limit doesn't exist. Does this mean NaN? But this case is not covered in https://drafts.csswg.org/css-values/#trig-infinities, which only says

In sin(A), cos(A), or tan(A), if A is infinite, the result is NaN.

For the inverse functions:

In atan(A), if A is +∞, the result is 90deg; if A is -∞, the result is -90deg.

So I guess this should round-trip and tan(90deg) should be +∞ and tan(-90deg) be -∞?

ECMAScript doesn't define this case either, but I guess it can be handwaved due to floating-point precision problems, e.g. Math.tan(Math.PI / 2) returns 16331239353195370 because π/2 can't be stored exactly. But in CSS, deg is the canonical unit for <angle>, and 90 is an integer, so I think there shouldn't be any precision problem.

@Loirooriol Loirooriol added the css-values-4 Current Work label Jul 10, 2019
@AmeliaBR
Copy link
Contributor

So I guess this should round-trip and tan(90deg) should be +∞ and tan(-90deg) be -∞?

I would argue strongly for a solution that supports round-tripping, so that atan(tan(90deg)) returns 90deg. If we can't do that, might as well give up on propagating the infinities through the nested functions at all.

And being approximately consistent with JS implementations is good, too. Even if the JS results are a side effect of numerical precision limits.

@Loirooriol
Copy link
Contributor Author

I think it's also good to preserve tan(x) = sin(x) / cos(x).

So assuming cos(90deg) = cos(-90deg) = +0 (not -0), then IEEE-754 says

tan(+90deg) = +1 / +0 = +∞
tan(−90deg) = −1 / +0 = −∞

@Loirooriol Loirooriol changed the title Does tan(90deg) return +∞, −∞ or NaN? [css-values] Does tan(90deg) return +∞, −∞ or NaN? Jul 11, 2019
@Loirooriol
Copy link
Contributor Author

I would argue strongly for a solution that supports round-tripping, so that atan(tan(90deg)) returns 90deg

IMO atan(tan(90deg)) = 90deg should just be a consequence, not what we want to enforce.

Note that atan ∘ tan is not the identity, e.g. atan(tan(2π)) is not (given the usual definition of atan). The identity is tan ∘ atan, so if we want to preserve this in CSS, what we actually need is tan(atan(+∞)) = +∞. And since atan(+∞) = 90deg, then tan(90deg) = +∞

@AmeliaBR
Copy link
Contributor

what we actually need is tan(atan(+∞)) = +∞

Ah, good point. The input to tan() can be an arbitrary angle, but the output from atan will always be restricted to the base circle. But yes, once we reverse the nesting, we still want the consistent behavior.

@tabatkins
Copy link
Member

Interesting question!

I agree that, at minimum, tan(90deg) should equal +inf. Since JS's Math.PI slightly undershoots the precise value, it happens to be true that Math.tan(Math.PI/2 * N) is always a very large (near infinite) positive value when N is a positive odd number, and a very large negative value when N is a negative odd. (But of course, adding a very tiny additional nudge will shift it over the threshold and swap the sign.)

So at least it's consistent with naive usage of JS to say that positive angles give +inf and negative give -inf. ^_^ And then yeah, that gives you roundtripping of either nesting (within the [-90deg, 90deg] range).

@Loirooriol
Copy link
Contributor Author

Um, I only mentioned 90deg and -90deg because I assumed we would preserve the 360deg periodicity.

Sure, saying tan(90deg) != tan(-90deg) already implies that we loose the 180deg periodicity, but the 360deg one is like very intrinsic to trigonometry. I'm not convinced that we should drop it just to match some floating-point precision problems.

This seems strange to me:

tan(-90deg) = -∞
tan(-90deg + 360deg) = +∞

@tabatkins
Copy link
Member

Interesting. I had not realized that was a direction you might be wanting. That makes some sense, tho.

@AmeliaBR
Copy link
Contributor

I had also assumed that keeping a consistent behavior for all equivalent angles (mod 360deg) was implied.

I know there are other places where we use a towards/away from zero rule, but for trigonometry having -90deg match 270deg (and so on) is more important.

tabatkins added a commit that referenced this issue Jul 12, 2019
@Loirooriol
Copy link
Contributor Author

Loirooriol commented Jul 15, 2019

@tabatkins There is some problem with your patch, ''tan(atan(1/0))'' and ''tan(atan(-1/0))'' are rendered as ‘0))’. I guess you should escape the slash.

@tabatkins
Copy link
Member

Ah, thanks, fixed. (It was the autolinker misfiring on the division sign, thinking it was a ''for/term'' autolink.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css-values-4 Current Work
Projects
None yet
Development

No branches or pull requests

3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/w3c/csswg-drafts/issues/4101

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy