Common block mystery

I have. The following is an almostEqual function patterned after a post on Intel Fortran Forum that uses SPACING. You could also use EPSILON (which is what I originally had). I use this in most places where I need to test for equlity (to at least a tolerance) of two floating point numbers. This version is for REAL32 but I also have one for REAL64. The SPACING_FACTOR is set to a constant 5 but thats just what the original post had. You could use 2 if you want a tighter tolerance.



  Elemental Function almostEqualR32(a, b) Result(aeqb)

! Function to test if two WP floating point numbers 
! almost equal to close to machine precision 

! Code taken from post on Intel Fortran Forum 11/15/2016

! Argument variables

    Real(REAL32), Intent(IN) :: a, b
    Logical              :: aeqb

    aeqb = ABS(a-b) < SPACING_FACTOR32*SPACING(MAX(ABS(a), ABS(b)))

  End Function almostEqualR32
2 Likes