1 #include <petscsys.h> 2 3 /*@C 4 PetscIsCloseAtTol - Returns whether the two floating point numbers 5 are close at a given relative and absolute tolerances. 6 7 Input Parameters: 8 + a - first floating point number 9 . b - second floating point number 10 . rtol - relative tolerance 11 - atol - absolute tolerances 12 13 Reference: 14 . * - https://www.python.org/dev/peps/pep-0485/ 15 16 Level: beginner 17 18 .seealso: `PetscEqualReal()`, `PetscEqualScalar()` 19 @*/ 20 PetscBool PetscIsCloseAtTol(PetscReal a, PetscReal b, PetscReal rtol, PetscReal atol) { 21 PetscReal diff; 22 /* NaN is not considered close to any other value, including NaN */ 23 if (PetscIsNanReal(a) || PetscIsNanReal(b)) return PETSC_FALSE; 24 /* Fast path for exact equality or two infinities of same sign */ 25 if (a == b) return PETSC_TRUE; 26 /* Handle two infinities of opposite sign */ 27 if (PetscIsInfReal(a) || PetscIsInfReal(b)) return PETSC_FALSE; 28 /* Cannot error if tolerances are negative */ 29 rtol = PetscAbsReal(rtol); 30 atol = PetscAbsReal(atol); 31 /* The regular check for difference within tolerances */ 32 diff = PetscAbsReal(b - a); 33 return ((diff <= PetscAbsReal(rtol * b)) || (diff <= PetscAbsReal(rtol * a)) || (diff <= atol)) ? PETSC_TRUE : PETSC_FALSE; 34 } 35