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

Poor accuracy of cos just below pi with Double64 #164

Open
moble opened this issue Feb 20, 2023 · 3 comments
Open

Poor accuracy of cos just below pi with Double64 #164

moble opened this issue Feb 20, 2023 · 3 comments

Comments

@moble
Copy link

moble commented Feb 20, 2023

For values close to — but just below — pi, the result of cos is quite jittery and significantly less accurate than values just above pi. I actually don't understand how the latter are so accurate; it would be nice if the former could be so accurate as well. But the real problem is that the values just below pi actually exceed the bounds [-1,1] that one expects for cos. This causes me various DomainErrors down the line in tests that pass with Julia's built-in float types.

Looking at the code, it seems like this line is the relevant path. And from that I find very similar behavior for sin just below (but not above) pi/2. I wonder if more branches with qrtrpi and threeqrtrpi (using corresponding symmetries) would be better for these cases.

T = Double64
x = LinRange(T(π)-T(5e-16), T(π)+T(5e-16), 10_000)
y = cos.(x)

cos_theta_Double64

@JeffreySarnoff
Copy link
Member

fixed

@moble
Copy link
Author

moble commented Feb 9, 2024

@JeffreySarnoff I just installed main in a fresh project and got essentially the same result:

UpdatedDoubleFloatCosIssue

For reference, here's the complete reproducer for the plot above:

using Pkg
Pkg.activate(; temp=true)
Pkg.add(url="https://github.com/JuliaMath/DoubleFloats.jl#main")
Pkg.add("PythonPlot")

using PythonPlot
using DoubleFloats

T = Double64
x = LinRange(T(π)-T(5e-16), T(π)+T(5e-16), 10_000)
y = cos.(x)

plot(x.-pi, y.+1)
axhline(0, c="black")
xlabel(raw"$\theta - \pi$")
ylabel(raw"$\cos\theta + 1$")

@JeffreySarnoff
Copy link
Member

ok reopening

@JeffreySarnoff JeffreySarnoff reopened this Feb 9, 2024
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

No branches or pull requests

2 participants