Actual source code: mathinf.c

  1: #if !defined(PETSC_SKIP_COMPLEX)
  2:   #define PETSC_SKIP_COMPLEX
  3: #endif

  5: #include <petscsys.h>
  6: /*@C
  7:   PetscIsNormalReal - Returns `PETSC_TRUE` if the input value satisfies `isnormal()`

  9:   Input Parameter:
 10: . a - the `PetscReal` Value

 12:   Level: beginner

 14:   Developer Notes:
 15:   Uses the C99 standard `isnormal()` on systems where they exist.

 17:   Uses `isnormalq()` with `__float128`

 19:   Otherwise always returns true

 21: .seealso: `PetscIsInfReal()`, `PetscIsNanReal()`
 22: @*/
 23: #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
 24: PetscBool PetscIsNormalReal(PetscReal a)
 25: {
 26:   return PETSC_TRUE;
 27: }
 28: #elif defined(PETSC_HAVE_ISNORMAL)
 29: PetscBool PetscIsNormalReal(PetscReal a)
 30: {
 31:   return isnormal(a) ? PETSC_TRUE : PETSC_FALSE;
 32: }
 33: #else
 34: PetscBool PetscIsNormalReal(PetscReal a)
 35: {
 36:   return PETSC_TRUE;
 37: }
 38: #endif

 40: #if defined(PETSC_HAVE_NO_FINITE_MATH_ONLY)
 41:   #define PETSC_FORCE_NO_FINITE_MATH_ONLY __attribute__((optimize("no-finite-math-only")))
 42: #else
 43:   #define PETSC_FORCE_NO_FINITE_MATH_ONLY
 44: #endif

 46: /*@C
 47:   PetscIsInfReal - Returns whether the `PetscReal` input is an infinity value.

 49:   Input Parameter:
 50: . a - the floating point number

 52:   Level: beginner

 54:   Developer Notes:
 55:   Uses the C99 standard `isinf()` on systems where it exists.

 57:   Otherwise uses (a && a/2 == a), note that some optimizing compilers compile out this form, thus removing the check.

 59: .seealso: `PetscIsNormalReal()`, `PetscIsNanReal()`
 60: @*/
 61: #if defined(PETSC_USE_REAL___FLOAT128)
 62: PetscBool PetscIsInfReal(PetscReal a)
 63: {
 64:   return isinfq(a) ? PETSC_TRUE : PETSC_FALSE;
 65: }
 66: #elif defined(PETSC_HAVE_ISINF)
 67: PETSC_FORCE_NO_FINITE_MATH_ONLY PetscBool PetscIsInfReal(PetscReal a)
 68: {
 69:   return isinf(a) ? PETSC_TRUE : PETSC_FALSE;
 70: }
 71: #elif defined(PETSC_HAVE__FINITE)
 72:   #if defined(PETSC_HAVE_FLOAT_H)
 73:     #include <float.h> /* Microsoft Windows defines _finite() in float.h */
 74:   #endif
 75:   #if defined(PETSC_HAVE_IEEEFP_H)
 76:     #include <ieeefp.h> /* Solaris prototypes these here */
 77:   #endif
 78: PetscBool PetscIsInfReal(PetscReal a)
 79: {
 80:   return !_finite(a) ? PETSC_TRUE : PETSC_FALSE;
 81: }
 82: #else
 83: PetscBool PetscIsInfReal(PetscReal a)
 84: {
 85:   return (a && a / 2 == a) ? PETSC_TRUE : PETSC_FALSE;
 86: }
 87: #endif

 89: /*@C
 90:   PetscIsNanReal - Returns whether the `PetscReal` input is a Not-a-Number (NaN) value.

 92:   Input Parameter:
 93: . a - the floating point number

 95:   Level: beginner

 97:   Developer Notes:
 98:   Uses the C99 standard `isnan()` on systems where it exists.

100:   Otherwise uses (a != a), note that some optimizing compilers compile
101:   out this form, thus removing the check.

103: .seealso: `PetscIsNormalReal()`, `PetscIsInfReal()`
104: @*/
105: #if defined(PETSC_USE_REAL___FLOAT128)
106: PetscBool PetscIsNanReal(PetscReal a)
107: {
108:   return isnanq(a) ? PETSC_TRUE : PETSC_FALSE;
109: }
110: #elif defined(PETSC_HAVE_ISNAN)
111: PETSC_FORCE_NO_FINITE_MATH_ONLY PetscBool PetscIsNanReal(PetscReal a)
112: {
113:   return isnan(a) ? PETSC_TRUE : PETSC_FALSE;
114: }
115: #elif defined(PETSC_HAVE__ISNAN)
116:   #if defined(PETSC_HAVE_FLOAT_H)
117:     #include <float.h> /* Microsoft Windows defines _isnan() in float.h */
118:   #endif
119:   #if defined(PETSC_HAVE_IEEEFP_H)
120:     #include <ieeefp.h> /* Solaris prototypes these here */
121:   #endif
122: PetscBool PetscIsNanReal(PetscReal a)
123: {
124:   return _isnan(a) ? PETSC_TRUE : PETSC_FALSE;
125: }
126: #else
127: PetscBool PetscIsNanReal(PetscReal a)
128: {
129:   return (a != a) ? PETSC_TRUE : PETSC_FALSE;
130: }
131: #endif