safe_cos_polynomial Function

public 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.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x

Return Value real(kind=wp)


Calls

proc~~safe_cos_polynomial~~CallsGraph proc~safe_cos_polynomial safe_cos_polynomial proc~cos_reduced cos_reduced proc~safe_cos_polynomial->proc~cos_reduced proc~sin_reduced sin_reduced proc~safe_cos_polynomial->proc~sin_reduced

Variables

Type Visibility Attributes Name Initial
integer, private :: k_int
real(kind=wp), private :: k_real
integer, private :: quadrant
real(kind=wp), private :: r

Source Code

   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