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