xref: /petsc/src/sys/utils/mathinf.c (revision bae465764c59bc95a6a26ca54e06946e83261cad)
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 @*/
158b49ba18SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
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 
72*bae46576SBarry Smith /*@C
73*bae46576SBarry Smith       PetscIsNanReal - Returns an error code if the input double has a Not-a-number (Nan) value, otherwise 0.
74*bae46576SBarry Smith 
75*bae46576SBarry Smith     Input Parameter:
76*bae46576SBarry Smith .     a - the floating point number
77*bae46576SBarry Smith 
78*bae46576SBarry Smith      Notes: uses the C99 standard isinf() and isnan() on systems where they exist.
79*bae46576SBarry Smith       Otherwises uses ((a - a) != 0.0), note that some optimizing compiles compile
80*bae46576SBarry Smith       out this form, thus removing the check.
81*bae46576SBarry Smith 
82*bae46576SBarry Smith      Level: beginner
83*bae46576SBarry Smith @*/
84*bae46576SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
85*bae46576SBarry Smith PetscErrorCode PetscIsNanReal(PetscReal a)
86*bae46576SBarry Smith {
87*bae46576SBarry Smith   return isnanq(a);
88*bae46576SBarry Smith }
89*bae46576SBarry Smith #elif defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN)
90*bae46576SBarry Smith PetscErrorCode PetscIsNanReal(PetscReal a)
91*bae46576SBarry Smith {
92*bae46576SBarry Smith   return isnan(a);
93*bae46576SBarry Smith }
94*bae46576SBarry Smith #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN)
95*bae46576SBarry Smith #if defined(PETSC_HAVE_FLOAT_H)
96*bae46576SBarry Smith #include <float.h>  /* Microsoft Windows defines _finite() in float.h */
97*bae46576SBarry Smith #endif
98*bae46576SBarry Smith #if defined(PETSC_HAVE_IEEEFP_H)
99*bae46576SBarry Smith #include <ieeefp.h>  /* Solaris prototypes these here */
100*bae46576SBarry Smith #endif
101*bae46576SBarry Smith PetscErrorCode PetscIsNanReal(PetscReal a)
102*bae46576SBarry Smith {
103*bae46576SBarry Smith   return _isnan(a);
104*bae46576SBarry Smith }
105*bae46576SBarry Smith #else
106*bae46576SBarry Smith PetscErrorCode PetscIsNanReal(PetscReal a)
107*bae46576SBarry Smith {
108*bae46576SBarry Smith   return ((a - a) != 0);
109*bae46576SBarry Smith }
110*bae46576SBarry Smith #endif
111*bae46576SBarry Smith 
112