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 (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) end if if (abs(a - b) <= error_tolerance) then almost_equal = .true. return end if almost_equal = .false. end function almost_equal_real64