xref: /petsc/src/sys/utils/mathinf.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
10c567f5aSSatish Balay #define PETSC_SKIP_COMPLEX
2532fbc7fSSatish Balay #include <petscsys.h>
3532fbc7fSSatish Balay /*@C
40c567f5aSSatish Balay       PetscIsNormalReal - Returns PETSC_TRUE if the input value satisfies isnormal()
5532fbc7fSSatish Balay 
6532fbc7fSSatish Balay     Input Parameter:
78b49ba18SBarry Smith .     a - the PetscReal Value
8532fbc7fSSatish Balay 
995452b02SPatrick Sanan      Notes:
1095452b02SPatrick Sanan     uses the C99 standard isnormal() on systems where they exist.
118b49ba18SBarry Smith       Uses isnormalq() with __float128
128b49ba18SBarry Smith       Otherwises always returns true
138b49ba18SBarry Smith 
148b49ba18SBarry Smith      Level: beginner
158b49ba18SBarry Smith @*/
16570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
17*9371c9d4SSatish Balay PetscBool PetscIsNormalReal(PetscReal a) {
188fa295daSBarry Smith   return PETSC_TRUE;
198b49ba18SBarry Smith }
208b49ba18SBarry Smith #elif defined(PETSC_HAVE_ISNORMAL)
21*9371c9d4SSatish Balay PetscBool PetscIsNormalReal(PetscReal a) {
22bb88209dSBarry Smith   return isnormal(a) ? PETSC_TRUE : PETSC_FALSE;
238b49ba18SBarry Smith }
248b49ba18SBarry Smith #else
25*9371c9d4SSatish Balay PetscBool PetscIsNormalReal(PetscReal a) {
268b49ba18SBarry Smith   return PETSC_TRUE;
278b49ba18SBarry Smith }
288b49ba18SBarry Smith #endif
298b49ba18SBarry Smith 
308b49ba18SBarry Smith /*@C
319f4f8022SLisandro Dalcin       PetscIsInfReal - Returns whether the input is an infinity value.
328b49ba18SBarry Smith 
338b49ba18SBarry Smith     Input Parameter:
348b49ba18SBarry Smith .     a - the floating point number
35532fbc7fSSatish Balay 
3695452b02SPatrick Sanan      Notes:
3795452b02SPatrick Sanan     uses the C99 standard isinf() on systems where it exists.
389f4f8022SLisandro Dalcin       Otherwises uses (a && a/2 == a), note that some optimizing compiles compile
39532fbc7fSSatish Balay       out this form, thus removing the check.
40532fbc7fSSatish Balay 
41532fbc7fSSatish Balay      Level: beginner
42532fbc7fSSatish Balay @*/
43532fbc7fSSatish Balay #if defined(PETSC_USE_REAL___FLOAT128)
44*9371c9d4SSatish Balay PetscBool PetscIsInfReal(PetscReal a) {
459f4f8022SLisandro Dalcin   return isinfq(a) ? PETSC_TRUE : PETSC_FALSE;
46532fbc7fSSatish Balay }
479f4f8022SLisandro Dalcin #elif defined(PETSC_HAVE_ISINF)
48*9371c9d4SSatish Balay PetscBool PetscIsInfReal(PetscReal a) {
499f4f8022SLisandro Dalcin   return isinf(a) ? PETSC_TRUE : PETSC_FALSE;
50532fbc7fSSatish Balay }
519f4f8022SLisandro Dalcin #elif defined(PETSC_HAVE__FINITE)
52532fbc7fSSatish Balay #if defined(PETSC_HAVE_FLOAT_H)
53aaa7dc30SBarry Smith #include <float.h> /* Microsoft Windows defines _finite() in float.h */
54532fbc7fSSatish Balay #endif
55532fbc7fSSatish Balay #if defined(PETSC_HAVE_IEEEFP_H)
56aaa7dc30SBarry Smith #include <ieeefp.h> /* Solaris prototypes these here */
57532fbc7fSSatish Balay #endif
58*9371c9d4SSatish Balay PetscBool PetscIsInfReal(PetscReal a) {
599f4f8022SLisandro Dalcin   return !_finite(a) ? PETSC_TRUE : PETSC_FALSE;
60532fbc7fSSatish Balay }
61532fbc7fSSatish Balay #else
62*9371c9d4SSatish Balay PetscBool PetscIsInfReal(PetscReal a) {
639f4f8022SLisandro Dalcin   return (a && a / 2 == a) ? PETSC_TRUE : PETSC_FALSE;
64532fbc7fSSatish Balay }
65532fbc7fSSatish Balay #endif
66532fbc7fSSatish Balay 
67bae46576SBarry Smith /*@C
683948c36eSLisandro Dalcin       PetscIsNanReal - Returns whether the input is a Not-a-Number (NaN) value.
69bae46576SBarry Smith 
70bae46576SBarry Smith     Input Parameter:
71bae46576SBarry Smith .     a - the floating point number
72bae46576SBarry Smith 
7395452b02SPatrick Sanan      Notes:
7495452b02SPatrick Sanan     uses the C99 standard isnan() on systems where it exists.
753948c36eSLisandro Dalcin       Otherwises uses (a != a), note that some optimizing compiles compile
76bae46576SBarry Smith       out this form, thus removing the check.
77bae46576SBarry Smith 
78bae46576SBarry Smith      Level: beginner
79bae46576SBarry Smith @*/
80bae46576SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
81*9371c9d4SSatish Balay PetscBool PetscIsNanReal(PetscReal a) {
823948c36eSLisandro Dalcin   return isnanq(a) ? PETSC_TRUE : PETSC_FALSE;
83bae46576SBarry Smith }
843948c36eSLisandro Dalcin #elif defined(PETSC_HAVE_ISNAN)
85*9371c9d4SSatish Balay PetscBool PetscIsNanReal(PetscReal a) {
863948c36eSLisandro Dalcin   return isnan(a) ? PETSC_TRUE : PETSC_FALSE;
87bae46576SBarry Smith }
883948c36eSLisandro Dalcin #elif defined(PETSC_HAVE__ISNAN)
89bae46576SBarry Smith #if defined(PETSC_HAVE_FLOAT_H)
903948c36eSLisandro Dalcin #include <float.h> /* Microsoft Windows defines _isnan() in float.h */
91bae46576SBarry Smith #endif
92bae46576SBarry Smith #if defined(PETSC_HAVE_IEEEFP_H)
93bae46576SBarry Smith #include <ieeefp.h> /* Solaris prototypes these here */
94bae46576SBarry Smith #endif
95*9371c9d4SSatish Balay PetscBool PetscIsNanReal(PetscReal a) {
963948c36eSLisandro Dalcin   return _isnan(a) ? PETSC_TRUE : PETSC_FALSE;
97bae46576SBarry Smith }
98bae46576SBarry Smith #else
99*9371c9d4SSatish Balay PetscBool PetscIsNanReal(PetscReal a) {
1003948c36eSLisandro Dalcin   return (a != a) ? PETSC_TRUE : PETSC_FALSE;
101bae46576SBarry Smith }
102bae46576SBarry Smith #endif
103