Public only for the unit-test suite (no production module imports it);
ignore when developing production code in other modules.
cos(x) via the same Cody-Waite reduction; the quadrant
map is shifted by 1 vs sin:
k mod 4 = 0: cos(x) = cos(r) k mod 4 = 1: cos(x) = -sin(r) k mod 4 = 2: cos(x) = -cos(r) k mod 4 = 3: cos(x) = sin(r)
cos(0) == 1 exact.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(in) | :: | x |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | private | :: | k_int | ||||
| real(kind=wp), | private | :: | k_real | ||||
| integer, | private | :: | quadrant | ||||
| real(kind=wp), | private | :: | r |
elemental function safe_cos_polynomial(x) result(y) !! Public only for the unit-test suite (no production module imports it); !! ignore when developing production code in other modules. !! `cos(x)` via the same Cody-Waite reduction; the quadrant !! map is shifted by 1 vs sin: !! !! k mod 4 = 0: cos(x) = cos(r) !! k mod 4 = 1: cos(x) = -sin(r) !! k mod 4 = 2: cos(x) = -cos(r) !! k mod 4 = 3: cos(x) = sin(r) !! !! `cos(0) == 1` exact. real(wp), intent(in) :: x real(wp) :: y real(wp) :: k_real, r integer :: k_int, quadrant k_real = anint(x*INV_PI_OVER_2) k_int = nint(k_real) r = (x - k_real*PI_OVER_2_HI) - k_real*PI_OVER_2_LO quadrant = modulo(k_int, 4) select case (quadrant) case (0) y = cos_reduced(r) case (1) y = -sin_reduced(r) case (2) y = -cos_reduced(r) case default ! 3 y = sin_reduced(r) end select end function safe_cos_polynomial