almost_equal_real64 Function

private pure elemental function almost_equal_real64(a, b, absolute_tolerance, relative_tolerance) result(almost_equal)

Uses

  • proc~~almost_equal_real64~~UsesGraph proc~almost_equal_real64 almost_equal_real64 iso_fortran_env iso_fortran_env proc~almost_equal_real64->iso_fortran_env

Test a and b for approximate equality, where a and b are both reals. (KCW, 2024-05-25)

Arguments

Type IntentOptional Attributes Name
real(kind=real64), intent(in) :: a
real(kind=real64), intent(in) :: b
real(kind=real64), intent(in), optional :: absolute_tolerance
real(kind=real64), intent(in), optional :: relative_tolerance

Return Value logical


Called by

proc~~almost_equal_real64~~CalledByGraph proc~almost_equal_real64 almost_equal_real64 interface~almost_equal almost_equal interface~almost_equal->proc~almost_equal_real64 proc~almost_divisible_real32 almost_divisible_real32 proc~almost_divisible_real32->interface~almost_equal proc~almost_divisible_real64 almost_divisible_real64 proc~almost_divisible_real64->interface~almost_equal proc~check_topography_data check_topography_data proc~check_topography_data->interface~almost_equal interface~almost_divisible almost_divisible interface~almost_divisible->proc~almost_divisible_real32 interface~almost_divisible->proc~almost_divisible_real64 proc~dyn_init dyn_init proc~dyn_init->proc~check_topography_data proc~dyn_mpas_init_phase4 mpas_dynamical_core_type%dyn_mpas_init_phase4 proc~dyn_init->proc~dyn_mpas_init_phase4 interface~dyn_init dyn_init interface~dyn_init->proc~dyn_init proc~dyn_mpas_init_phase4->interface~almost_divisible

Variables

Type Visibility Attributes Name Initial
real(kind=real64), private :: error_tolerance

Source Code

    pure elemental function almost_equal_real64(a, b, absolute_tolerance, relative_tolerance) result(almost_equal)
        use, intrinsic :: iso_fortran_env, only: real64

        real(real64), intent(in) :: a, b
        real(real64), optional, intent(in) :: absolute_tolerance, relative_tolerance
        logical :: almost_equal

        real(real64) :: error_tolerance

        if (a /= a .or. b /= b) then
            ! NaN is not equal to anything, including itself.
            almost_equal = .false.

            return
        end if

        if (abs(a) > huge(a) .or. abs(b) > huge(b)) then
            ! Infinities of the same sign are equal to each other.
            ! Infinities of different signs are not equal to each other.
            ! An infinity is not equal to anything that is finite.
            almost_equal = (a == b)

            return
        end if

        if (present(relative_tolerance)) then
            error_tolerance = relative_tolerance * max(abs(a), abs(b))
        else
            error_tolerance = epsilon(1.0_real64) * max(abs(a), abs(b))
        end if

        if (present(absolute_tolerance)) then
            error_tolerance = max(absolute_tolerance, error_tolerance)
        else
            error_tolerance = max(epsilon(1.0_real64), error_tolerance)
        end if

        almost_equal = (abs(a - b) <= error_tolerance)
    end function almost_equal_real64