xref: /petsc/src/sys/utils/mathinf.c (revision 3948c36e7ef471ce44627da1072d09bee4c7b8f5)
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 @*/
15570b7f6dSBarry 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
33*3948c36eSLisandro Dalcin       PetscIsInfOrNanReal - Returns whether the input is an infinity or Not-a-Number (NaN) value.
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)
45*3948c36eSLisandro Dalcin PetscBool PetscIsInfOrNanReal(PetscReal a)
46f48dab2eSKarl Rupp {
47*3948c36eSLisandro Dalcin   return isinfq(a) || isnanq(a) ? PETSC_TRUE : PETSC_FALSE;
48532fbc7fSSatish Balay }
49532fbc7fSSatish Balay #elif defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN)
50*3948c36eSLisandro Dalcin PetscBool PetscIsInfOrNanReal(PetscReal a)
51f48dab2eSKarl Rupp {
52*3948c36eSLisandro Dalcin   return isinf(a) || isnan(a) ? PETSC_TRUE : PETSC_FALSE;
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
61*3948c36eSLisandro Dalcin PetscBool PetscIsInfOrNanReal(PetscReal a)
62f48dab2eSKarl Rupp {
63*3948c36eSLisandro Dalcin   return !_finite(a) || _isnan(a) ? PETSC_TRUE : PETSC_FALSE;
64532fbc7fSSatish Balay }
65532fbc7fSSatish Balay #else
66*3948c36eSLisandro Dalcin PetscBool PetscIsInfOrNanReal(PetscReal a)
67f48dab2eSKarl Rupp {
68*3948c36eSLisandro Dalcin   return ((a - a) != 0) ? PETSC_TRUE : PETSC_FALSE;
69532fbc7fSSatish Balay }
70532fbc7fSSatish Balay #endif
71532fbc7fSSatish Balay 
72bae46576SBarry Smith /*@C
73*3948c36eSLisandro Dalcin       PetscIsNanReal - Returns whether the input is a Not-a-Number (NaN) value.
74bae46576SBarry Smith 
75bae46576SBarry Smith     Input Parameter:
76bae46576SBarry Smith .     a - the floating point number
77bae46576SBarry Smith 
78*3948c36eSLisandro Dalcin      Notes: uses the C99 standard isnan() on systems where it exists.
79*3948c36eSLisandro Dalcin       Otherwises uses (a != a), 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)
85*3948c36eSLisandro Dalcin PetscBool PetscIsNanReal(PetscReal a)
86bae46576SBarry Smith {
87*3948c36eSLisandro Dalcin   return isnanq(a) ? PETSC_TRUE : PETSC_FALSE;
88bae46576SBarry Smith }
89*3948c36eSLisandro Dalcin #elif defined(PETSC_HAVE_ISNAN)
90*3948c36eSLisandro Dalcin PetscBool PetscIsNanReal(PetscReal a)
91bae46576SBarry Smith {
92*3948c36eSLisandro Dalcin   return isnan(a) ? PETSC_TRUE : PETSC_FALSE;
93bae46576SBarry Smith }
94*3948c36eSLisandro Dalcin #elif defined(PETSC_HAVE__ISNAN)
95bae46576SBarry Smith #if defined(PETSC_HAVE_FLOAT_H)
96*3948c36eSLisandro Dalcin #include <float.h>  /* Microsoft Windows defines _isnan() 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
101*3948c36eSLisandro Dalcin PetscBool PetscIsNanReal(PetscReal a)
102bae46576SBarry Smith {
103*3948c36eSLisandro Dalcin   return _isnan(a) ? PETSC_TRUE : PETSC_FALSE;
104bae46576SBarry Smith }
105bae46576SBarry Smith #else
106*3948c36eSLisandro Dalcin PetscBool PetscIsNanReal(volatile PetscReal a)
107bae46576SBarry Smith {
108*3948c36eSLisandro Dalcin   return (a != a) ? PETSC_TRUE : PETSC_FALSE;
109bae46576SBarry Smith }
110bae46576SBarry Smith #endif
111