From 83ccd7c982d63b9c0ec45f43d4161dc825033d31 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sat, 5 Mar 2016 21:37:22 +0100 Subject: [PATCH] introduced (in)equality comparison for double precision --- code/prec.f90 | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/code/prec.f90 b/code/prec.f90 index 0f22cf649..129681fd5 100644 --- a/code/prec.f90 +++ b/code/prec.f90 @@ -172,9 +172,9 @@ end subroutine prec_init !-------------------------------------------------------------------------------------------------- !> @brief figures out if a floating point number is NaN -! basically just a small wrapper, because gfortran < 4.9 does not have the IEEE module +! basically just a small wrapper, because gfortran < 5.0 does not have the IEEE module !-------------------------------------------------------------------------------------------------- -logical elemental function prec_isNaN(a) +logical elemental pure function prec_isNaN(a) implicit none real(pReal), intent(in) :: a @@ -187,4 +187,30 @@ logical elemental function prec_isNaN(a) #endif end function prec_isNaN + + +!-------------------------------------------------------------------------------------------------- +!> @brief equality comparison for double precision +! replaces "==" but for certain (absolute) tolerance. Counterpart to dNeq +!-------------------------------------------------------------------------------------------------- +logical elemental pure function dEq(a,b,tol) + real(pReal), intent(in) :: a,b + real(pReal), intent(in), optional :: tol + real(pReal), parameter :: eps = 1.0e-15_pReal + dEq = merge(.True., .False.,abs(a-b) < merge(tol,eps,present(tol))) +end function dEq + + + +!-------------------------------------------------------------------------------------------------- +!> @brief inequality comparison for double precision +! replaces "!=" but for certain (absolute) tolerance. Counterpart to dEq +!-------------------------------------------------------------------------------------------------- +logical elemental pure function dNeq(a,b,tol) + real(pReal), intent(in) :: a,b + real(pReal), intent(in), optional :: tol + real(pReal), parameter :: eps = 1.0e-15_pReal + dNeq = merge(.True., .False.,abs(a-b) < merge(tol,eps,present(tol))) +end function dNeq + end module prec