xref: /petsc/src/sys/utils/mathinf.c (revision 570b7f6d312edbd2962258513e9546951e9f0a9d)
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 
98b49ba18SBarry Smith      Notes: uses the C99 standard isnormal() on systems where they exist.
108b49ba18SBarry Smith       Uses isnormalq() with __float128
118b49ba18SBarry Smith       Otherwises always returns true
128b49ba18SBarry Smith 
138b49ba18SBarry Smith      Level: beginner
148b49ba18SBarry Smith @*/
15*570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
168b49ba18SBarry Smith PetscBool PetscIsNormalReal(PetscReal a)
178b49ba18SBarry Smith {
188fa295daSBarry Smith   return PETSC_TRUE;
198b49ba18SBarry Smith }
208b49ba18SBarry Smith #elif defined(PETSC_HAVE_ISNORMAL)
218b49ba18SBarry Smith PetscBool PetscIsNormalReal(PetscReal a)
228b49ba18SBarry Smith {
23bb88209dSBarry Smith   return isnormal(a) ? PETSC_TRUE : PETSC_FALSE;
248b49ba18SBarry Smith }
258b49ba18SBarry Smith #else
268b49ba18SBarry Smith PetscBool PetscIsNormalReal(PetscReal a)
278b49ba18SBarry Smith {
288b49ba18SBarry Smith   return PETSC_TRUE;
298b49ba18SBarry Smith }
308b49ba18SBarry Smith #endif
318b49ba18SBarry Smith 
328b49ba18SBarry Smith /*@C
330c567f5aSSatish Balay       PetscIsInfOrNanReal - Returns an error code if the input double has an infinity for Not-a-number (Nan) value, otherwise 0.
348b49ba18SBarry Smith 
358b49ba18SBarry Smith     Input Parameter:
368b49ba18SBarry Smith .     a - the floating point number
37532fbc7fSSatish Balay 
38532fbc7fSSatish Balay      Notes: uses the C99 standard isinf() and isnan() on systems where they exist.
39532fbc7fSSatish Balay       Otherwises uses ((a - a) != 0.0), note that some optimizing compiles compile
40532fbc7fSSatish Balay       out this form, thus removing the check.
41532fbc7fSSatish Balay 
42532fbc7fSSatish Balay      Level: beginner
43532fbc7fSSatish Balay @*/
44532fbc7fSSatish Balay #if defined(PETSC_USE_REAL___FLOAT128)
45f48dab2eSKarl Rupp PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
46f48dab2eSKarl Rupp {
47532fbc7fSSatish Balay   return isinfq(a) || isnanq(a);
48532fbc7fSSatish Balay }
49532fbc7fSSatish Balay #elif defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN)
50f48dab2eSKarl Rupp PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
51f48dab2eSKarl Rupp {
52532fbc7fSSatish Balay   return isinf(a) || isnan(a);
53532fbc7fSSatish Balay }
54532fbc7fSSatish Balay #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN)
55532fbc7fSSatish Balay #if defined(PETSC_HAVE_FLOAT_H)
56aaa7dc30SBarry Smith #include <float.h>  /* Microsoft Windows defines _finite() in float.h */
57532fbc7fSSatish Balay #endif
58532fbc7fSSatish Balay #if defined(PETSC_HAVE_IEEEFP_H)
59aaa7dc30SBarry Smith #include <ieeefp.h>  /* Solaris prototypes these here */
60532fbc7fSSatish Balay #endif
61f48dab2eSKarl Rupp PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
62f48dab2eSKarl Rupp {
63532fbc7fSSatish Balay   return !_finite(a) || _isnan(a);
64532fbc7fSSatish Balay }
65532fbc7fSSatish Balay #else
66f48dab2eSKarl Rupp PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
67f48dab2eSKarl Rupp {
68532fbc7fSSatish Balay   return ((a - a) != 0);
69532fbc7fSSatish Balay }
70532fbc7fSSatish Balay #endif
71532fbc7fSSatish Balay 
72bae46576SBarry Smith /*@C
73bae46576SBarry Smith       PetscIsNanReal - Returns an error code if the input double has a Not-a-number (Nan) value, otherwise 0.
74bae46576SBarry Smith 
75bae46576SBarry Smith     Input Parameter:
76bae46576SBarry Smith .     a - the floating point number
77bae46576SBarry Smith 
78bae46576SBarry Smith      Notes: uses the C99 standard isinf() and isnan() on systems where they exist.
79bae46576SBarry Smith       Otherwises uses ((a - a) != 0.0), note that some optimizing compiles compile
80bae46576SBarry Smith       out this form, thus removing the check.
81bae46576SBarry Smith 
82bae46576SBarry Smith      Level: beginner
83bae46576SBarry Smith @*/
84bae46576SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
85bae46576SBarry Smith PetscErrorCode PetscIsNanReal(PetscReal a)
86bae46576SBarry Smith {
87bae46576SBarry Smith   return isnanq(a);
88bae46576SBarry Smith }
89bae46576SBarry Smith #elif defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN)
90bae46576SBarry Smith PetscErrorCode PetscIsNanReal(PetscReal a)
91bae46576SBarry Smith {
92bae46576SBarry Smith   return isnan(a);
93bae46576SBarry Smith }
94bae46576SBarry Smith #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN)
95bae46576SBarry Smith #if defined(PETSC_HAVE_FLOAT_H)
96bae46576SBarry Smith #include <float.h>  /* Microsoft Windows defines _finite() in float.h */
97bae46576SBarry Smith #endif
98bae46576SBarry Smith #if defined(PETSC_HAVE_IEEEFP_H)
99bae46576SBarry Smith #include <ieeefp.h>  /* Solaris prototypes these here */
100bae46576SBarry Smith #endif
101bae46576SBarry Smith PetscErrorCode PetscIsNanReal(PetscReal a)
102bae46576SBarry Smith {
103bae46576SBarry Smith   return _isnan(a);
104bae46576SBarry Smith }
105bae46576SBarry Smith #else
106bae46576SBarry Smith PetscErrorCode PetscIsNanReal(PetscReal a)
107bae46576SBarry Smith {
108bae46576SBarry Smith   return ((a - a) != 0);
109bae46576SBarry Smith }
110bae46576SBarry Smith #endif
111bae46576SBarry Smith 
112