Created
September 24, 2012 16:27
-
-
Save certik/3776847 to your computer and use it in GitHub Desktop.
Donald Knuth has a proposal for a floating point comparison algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
logical elemental function fequal(a, b, eps) result(r) | |
! Returns .true. if a==b (withing "eps"), otherwise .false. | |
real(dp), intent(in) :: a, b, eps | |
integer :: e | |
if (abs(a) > abs(b)) then | |
e = exponent(a) | |
else | |
e = exponent(b) | |
end if | |
if (e < minexponent(1._dp)) e = minexponent(1._dp) | |
r = abs(a-b) < eps * 2._dp**e | |
end function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment