xref: /petsc/src/sys/utils/mathinf.c (revision 0c567f5a2ae6a4c939c54a29bc221d28702c885a)
1*0c567f5aSSatish Balay #define PETSC_SKIP_COMPLEX
2532fbc7fSSatish Balay #include <petscsys.h>
3532fbc7fSSatish Balay /*@C
4*0c567f5aSSatish 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
33*0c567f5aSSatish 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