xref: /petsc/src/sys/utils/mathinf.c (revision fbf9dbe564678ed6eff1806adbc4c4f01b9743f4)
1 #if !defined(PETSC_SKIP_COMPLEX)
2   #define PETSC_SKIP_COMPLEX
3 #endif
4 
5 #include <petscsys.h>
6 /*@C
7       PetscIsNormalReal - Returns `PETSC_TRUE` if the input value satisfies `isnormal()`
8 
9     Input Parameter:
10 .     a - the `PetscReal` Value
11 
12      Level: beginner
13 
14     Developer Notes:
15     Uses the C99 standard `isnormal()` on systems where they exist.
16 
17     Uses `isnormalq()` with `__float128`
18 
19     Otherwise always returns true
20 
21 .seealso: `PetscIsInfReal()`, `PetscIsNanReal()`
22 @*/
23 #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
24 PetscBool PetscIsNormalReal(PetscReal a)
25 {
26   return PETSC_TRUE;
27 }
28 #elif defined(PETSC_HAVE_ISNORMAL)
29 PetscBool PetscIsNormalReal(PetscReal a)
30 {
31   return isnormal(a) ? PETSC_TRUE : PETSC_FALSE;
32 }
33 #else
34 PetscBool PetscIsNormalReal(PetscReal a)
35 {
36   return PETSC_TRUE;
37 }
38 #endif
39 
40 /*@C
41       PetscIsInfReal - Returns whether the `PetscReal` input is an infinity value.
42 
43     Input Parameter:
44 .     a - the floating point number
45 
46      Level: beginner
47 
48     Developer Notes:
49     Uses the C99 standard `isinf()` on systems where it exists.
50 
51     Otherwise uses (a && a/2 == a), note that some optimizing compilers compile out this form, thus removing the check.
52 
53 .seealso: `PetscIsNormalReal()`, `PetscIsNanReal()`
54 @*/
55 #if defined(PETSC_USE_REAL___FLOAT128)
56 PetscBool PetscIsInfReal(PetscReal a)
57 {
58   return isinfq(a) ? PETSC_TRUE : PETSC_FALSE;
59 }
60 #elif defined(PETSC_HAVE_ISINF)
61 PetscBool PetscIsInfReal(PetscReal a)
62 {
63   return isinf(a) ? PETSC_TRUE : PETSC_FALSE;
64 }
65 #elif defined(PETSC_HAVE__FINITE)
66   #if defined(PETSC_HAVE_FLOAT_H)
67     #include <float.h> /* Microsoft Windows defines _finite() in float.h */
68   #endif
69   #if defined(PETSC_HAVE_IEEEFP_H)
70     #include <ieeefp.h> /* Solaris prototypes these here */
71   #endif
72 PetscBool PetscIsInfReal(PetscReal a)
73 {
74   return !_finite(a) ? PETSC_TRUE : PETSC_FALSE;
75 }
76 #else
77 PetscBool PetscIsInfReal(PetscReal a)
78 {
79   return (a && a / 2 == a) ? PETSC_TRUE : PETSC_FALSE;
80 }
81 #endif
82 
83 /*@C
84       PetscIsNanReal - Returns whether the `PetscReal` input is a Not-a-Number (NaN) value.
85 
86     Input Parameter:
87 .     a - the floating point number
88 
89      Level: beginner
90 
91     Developer Notes:
92     Uses the C99 standard `isnan()` on systems where it exists.
93 
94     Otherwise uses (a != a), note that some optimizing compilers compile
95     out this form, thus removing the check.
96 
97 .seealso: `PetscIsNormalReal()`, `PetscIsInfReal()`
98 @*/
99 #if defined(PETSC_USE_REAL___FLOAT128)
100 PetscBool PetscIsNanReal(PetscReal a)
101 {
102   return isnanq(a) ? PETSC_TRUE : PETSC_FALSE;
103 }
104 #elif defined(PETSC_HAVE_ISNAN)
105 PetscBool PetscIsNanReal(PetscReal a)
106 {
107   return isnan(a) ? PETSC_TRUE : PETSC_FALSE;
108 }
109 #elif defined(PETSC_HAVE__ISNAN)
110   #if defined(PETSC_HAVE_FLOAT_H)
111     #include <float.h> /* Microsoft Windows defines _isnan() in float.h */
112   #endif
113   #if defined(PETSC_HAVE_IEEEFP_H)
114     #include <ieeefp.h> /* Solaris prototypes these here */
115   #endif
116 PetscBool PetscIsNanReal(PetscReal a)
117 {
118   return _isnan(a) ? PETSC_TRUE : PETSC_FALSE;
119 }
120 #else
121 PetscBool PetscIsNanReal(PetscReal a)
122 {
123   return (a != a) ? PETSC_TRUE : PETSC_FALSE;
124 }
125 #endif
126