JuliaMath / JuliaMath/DoubleFloats.jl

Poor accuracy of `cos` just below pi with Double64

Open
#164 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
172
Forks
37
PR merge metrics
No merged PRs in 30d

Description

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 `DomainError`s down the line in tests that pass with Julia's built-in float types.

Looking at the code, it seems like [this line](https://github.com/JuliaMath/DoubleFloats.jl/blob/ef689ccbab37d84943e2533309d34c6665229cab/src/math/elementary/trig.jl#L195) 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.

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

![cos_theta_Double64](https://user-images.githubusercontent.com/1470769/220192778-ec4a1122-4445-4e17-9dcd-b5f441fa5a50.png)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.