From e97f3a788d0bc42f5579f8fdbcef6677a6684eae Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 30 Jun 2016 10:30:40 +0200 Subject: [PATCH] added special function for often used comparison to 0 --- code/prec.f90 | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/code/prec.f90 b/code/prec.f90 index bf9ac7124..1ab87e6ac 100644 --- a/code/prec.f90 +++ b/code/prec.f90 @@ -199,6 +199,39 @@ logical elemental pure function dNeq(a,b,tol) dNeq = merge(.False., .True.,abs(a-b) <= merge(tol,eps,present(tol))*maxval(abs([a,b]))) end function dNeq + +!-------------------------------------------------------------------------------------------------- +!> @brief equality to 0comparison for float with double precision +! replaces "==0" but for certain (relative) tolerance. Counterpart to dNeq0 +! http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm +!-------------------------------------------------------------------------------------------------- +logical elemental pure function dEq0(a,tol) + + implicit none + real(pReal), intent(in) :: a + real(pReal), intent(in), optional :: tol + real(pReal), parameter :: eps = 2.220446049250313E-16 ! DBL_EPSILON in C + + dEq0 = merge(.True., .False.,abs(a) <= merge(tol,eps,present(tol))*abs(a)) +end function dEq0 + + +!-------------------------------------------------------------------------------------------------- +!> @brief inequality comparison to 0 for float with double precision +! replaces "!=0" but for certain (relative) tolerance. Counterpart to dEq0 +! http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm +!-------------------------------------------------------------------------------------------------- +logical elemental pure function dNeq0(a,tol) + + implicit none + real(pReal), intent(in) :: a + real(pReal), intent(in), optional :: tol + real(pReal), parameter :: eps = 2.220446049250313E-16 ! DBL_EPSILON in C + + dNeq0 = merge(.False., .True.,abs(a) <= merge(tol,eps,present(tol))*abs(a)) +end function dNeq0 + + !-------------------------------------------------------------------------------------------------- !> @brief equality comparison for complex with double precision ! replaces "==" but for certain (relative) tolerance. Counterpart to cNeq