EQUALTOTOLERANCE Function

public function EQUALTOTOLERANCE(x, y, tolerance) result(EqualToTolerance)

Determines if two REAL(wp) numbers are equal up to a given tolerance. Routine requires: x,y > tolerance

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x

(IN) first scalar to be compared

real(kind=wp), intent(in) :: y

(IN) second scalar to be compared

real(kind=wp), intent(in) :: tolerance

(IN) Tolerance to be checked against

Return Value logical

(OUT) TRUE if x and y are closer than tolerance


Source Code

FUNCTION EQUALTOTOLERANCE(x,y,tolerance)
IMPLICIT NONE
!----------------------------------------------------------------------------------------------------------------------------------
! INPUT/OUTPUT VARIABLES
REAL(wp),INTENT(IN) :: x                !! (IN)  first scalar to be compared
REAL(wp),INTENT(IN) :: y                !! (IN)  second scalar to be compared
REAL(wp),INTENT(IN) :: tolerance        !! (IN)  Tolerance to be checked against
LOGICAL         :: EqualToTolerance !! (OUT) TRUE if x and y are closer than tolerance
!-----------------------------------------------------------------------------------------------------------------------------------
! LOCAL VARIABLES
REAL(wp)            :: diff,maxInput
!===================================================================================================================================
EqualToTolerance = .FALSE.

maxInput = MAX(ABS(x),ABS(y))
diff = ABS(x-y)

! Test absolute error
IF (diff.LE.tolerance) THEN
  EqualToTolerance=.TRUE.
  RETURN
END IF

! Test relative error
IF(diff.LT.maxInput*tolerance) EqualToTolerance=.TRUE.

END FUNCTION EQUALTOTOLERANCE