Skip to content

Sine and cosine return incorrect intervals on negative intervals #40

@alexandervandenbroucke

Description

@alexandervandenbroucke

Description

On some negative intervals, sin and cos produce an invalid interval.

Example

>  cos $ (-pi/4) ... (-pi/8)
0.92387953251128685 ... 0.70710678118654746      

Note how the lower bound is greater than the upper bound!

Expected behaviour

The computed interval should be the same as for the positive version of the same interval

> cos (pi/8) ... (pi/4)
0.39269908169872414 ... 0.78539816339744828

Root cause

The algorithm for cos is the following (sin is implemented in terms of cos):

cos x
    | width t >= pi = negate 1 ... 1
    | inf t >= pi = negate $ cos (t - pi)
    | sup t <= pi = decreasing cos t
    | sup t <= 2 * pi = negate 1 ... cos (((pi * 2 - sup t) `min` coerce (inf t)))
    | otherwise = negate 1 ... 1
    where
      t = fmod x (pi * 2)

Note that $\cos(x)$ is increasing over $[-\pi/4, -\pi/8]$ whereas the code assumes that $\cos(x)$ is decreasing over anything with a supremum $\leq \pi$ is decreasing. Perhaps it assumes that fmod will always return a value $0 \leq x &lt; 2\pi$?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions