Determines if two REAL(wp) numbers are equal up to a given tolerance. Routine requires: x,y > tolerance
| Type | Intent | Optional | 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 |
(OUT) TRUE if x and y are closer than tolerance
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