xref: /petsc/src/sys/utils/mathinf.c (revision 9895aa37ac365bac650f6bd8bf977519f7222510)
1 #include <petscsys.h>
2 /*@C
3       PetscIsInfOrNan - Returns 1 if the input double has an infinity for Not-a-number (Nan) value, otherwise 0.
4 
5     Input Parameter:
6 .     a - the double
7 
8 
9      Notes: uses the C99 standard isinf() and isnan() on systems where they exist.
10       Otherwises uses ((a - a) != 0.0), note that some optimizing compiles compile
11       out this form, thus removing the check.
12 
13      Level: beginner
14 @*/
15 #if defined(PETSC_USE_REAL___FLOAT128)
16 PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a)
17 {
18   return isinfq(PetscAbsScalar(a)) || isnanq(PetscAbsScalar(a));
19 }
20 PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
21 {
22   return isinfq(a) || isnanq(a);
23 }
24 #elif defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN)
25 PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a)
26 {
27   return isinf(PetscAbsScalar(a)) || isnan(PetscAbsScalar(a));
28 }
29 PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
30 {
31   return isinf(a) || isnan(a);
32 }
33 #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN)
34 #if defined(PETSC_HAVE_FLOAT_H)
35 #include "float.h"  /* Microsoft Windows defines _finite() in float.h */
36 #endif
37 #if defined(PETSC_HAVE_IEEEFP_H)
38 #include "ieeefp.h"  /* Solaris prototypes these here */
39 #endif
40 PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a)
41 {
42   return !_finite(PetscAbsScalar(a)) || _isnan(PetscAbsScalar(a));
43 }
44 PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
45 {
46   return !_finite(a) || _isnan(a);
47 }
48 #else
49 PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a)
50 {
51   return  ((a - a) != (PetscScalar)0);
52 }
53 PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
54 {
55   return ((a - a) != 0);
56 }
57 #endif
58 
59