Test a and b for approximate equality, where a and b are both reals.
(KCW, 2024-05-25)
| Type | Intent | Optional | 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 |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| real(kind=real64), | private | :: | error_tolerance |
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