\addtogroup SELF_SupportRoutines @{ \fn AlmostEqual Compares two floating point numbers and determines if they are equal (to machine precision).
This function is from Alg. 139 on pg. 359 of D.A. Kopriva, 2009, "Implementing Spectral Element Methods for Scientists and Engineers"
Logical :: AisB
REAL(prec) :: a, b
....
AisB = AlmostEqual( a, b )
in | a | REAL(prec) | scalar |
---|---|---|---|
in | b | REAL(prec) | scalar |
in | AisB | Logical |
.TRUE. IF a=b to machine precision .FALSE. otherwise |
@}
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=real64) | :: | a | ||||
real(kind=real64) | :: | b |
function AlmostEqual_r64(a,b) result(AisB)
implicit none
real(real64) :: a,b
logical :: AisB
if(a == 0.0_real64 .or. b == 0.0_real64) then
if(abs(a-b) <= epsilon(1.0_real64)) then
AisB = .true.
else
AisB = .false.
endif
else
if((abs(a-b) <= epsilon(1.0_real64)*abs(a)) .or. (abs(a-b) <= epsilon(1.0_real64)*abs(b))) then
AisB = .true.
else
AisB = .false.
endif
endif
endfunction AlmostEqual_r64