-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add exact results for trig fcns on π #42595
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
Conversation
For reference, here's the current behavior: julia> sin(π)
1.2246467991473532e-16
julia> sincos(π)
(1.2246467991473532e-16, -1.0)
julia> sinpi(1)
0.0
julia> sincospi(1)
(0.0, -1.0) and here's the new one: julia> sin(π)
0
julia> sincos(π)
(0, -1)
julia> sinpi(1)
0
julia> sincospi(1)
(0, -1) |
Given the concerns expressed in #35823 it would not be crazy to make these return We could mark the |
You have your reasons for I'm thinking could the float version be changed slightly (its polynomials) to return exact float values, for:
and some few other exact constants, e.g. -π, and possibly only additionally -2π and 2π? As those are very commonly used. I realize that's what sinpi is for, so the counterargument would be it could lead people into false sense of security for integer multiples of π. I'm just thinking is it (easily) possible to put in constraints to calculate to correct polynomials (without sacrificing accuracy for other values), and might it be a replacement for your use case. |
There is |
The problem is that making |
base/special/trig.jl
Outdated
sinpi(x::Integer) = x >= 0 ? zero(float(x)) : -zero(float(x)) | ||
cospi(x::Integer) = isodd(x) ? -one(float(x)) : one(float(x)) | ||
sinpi(i::Integer) = 0 | ||
cospi(i::Integer) = iseven(i) ? 1 : -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Triage says 👎 because this is breaking and has been rejected before. But at least it should be in a separate PR if it's going to be considered.
Triage thinks we can add the methods for pi, but they should return Float64 for minimal breakage. That can easily be justified, since then it's just a matter of returning a more correct answer. Note |
That's a good triage outcome (what I expected & like). |
Since Julia has already gone to the trouble to encode π exactly, and trig functions are defined in terms of π, they should give exact answers for this particular constant.
Since Julia has gone to the trouble to encode π exactly, and trig functions are defined in terms of π, they should give exact answers for this particular constant.
Since Julia has gone to the trouble to encode π exactly, and trig functions are defined in terms of π, they should give exact answers for this particular constant.
The argument in favor here is that since Julia has already gone to the
trouble to encode π exactly, and trig functions are defined in terms of
π, they should give exact answers for this particular constant.
log(ℯ)
has a similar definition and test that
log(ℯ) === 1
. I decided herenot to require a specific return type as long as it was numerically
exact. (Arguably, we could also have chosen
log(::typeof(ℯ)) = true
.)I chose
Int
because there is no obvious reason to impose a particularprecision.
Potential concerns: how this will play with autodiff. But the same issue applies to
log(ℯ)
,and my guess is that it will be a non-issue.