Skip to content

Instantly share code, notes, and snippets.

@certik
Created September 24, 2012 16:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save certik/3776847 to your computer and use it in GitHub Desktop.
Save certik/3776847 to your computer and use it in GitHub Desktop.
Donald Knuth has a proposal for a floating point comparison algorithm
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