clamp1 Subroutine

private pure subroutine clamp1(lo, hi, a, b)

Shift interval [a, b] into [lo, hi], keeping its span.

Intervals wider than [lo, hi] become [lo, hi].

Arguments

Type IntentOptional Attributes Name
real(kind=real64), intent(in) :: lo

Lower bound.

real(kind=real64), intent(in) :: hi

Upper bound.

real(kind=real64), intent(inout) :: a

Lower end of interval.

real(kind=real64), intent(inout) :: b

Upper end of interval.


Source Code

   pure subroutine clamp1(lo, hi, a, b)
      !! Shift interval [a, b] into [lo, hi], keeping its span.
      !!
      !! Intervals wider than [lo, hi] become [lo, hi].
      ! Arguments
      real(real64), intent(in)    :: lo
         !! Lower bound.
      real(real64), intent(in)    :: hi
         !! Upper bound.
      real(real64), intent(inout) :: a
         !! Lower end of interval.
      real(real64), intent(inout) :: b
         !! Upper end of interval.
      real(real64) :: d
      if (b - a >= hi - lo) then
         a = lo; b = hi
      else if (a < lo) then
         d = lo - a; a = a + d; b = b + d
      else if (b > hi) then
         d = b - hi; a = a - d; b = b - d
      end if
   end subroutine clamp1