xref: /petsc/src/sys/utils/mathinf.c (revision c8c5c547f526914c69472d8cade615559dc64129)
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      Level: beginner
10 
11     Developer Notes:
12     Uses the C99 standard `isnormal()` on systems where they exist.
13 
14     Uses `isnormalq()` with `__float128`
15 
16     Otherwise always returns true
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 `PetscReal` input is an infinity value.
39 
40     Input Parameter:
41 .     a - the floating point number
42 
43      Level: beginner
44 
45     Developer Notes:
46     Uses the C99 standard `isinf()` on systems where it exists.
47 
48     Otherwise uses (a && a/2 == a), note that some optimizing compilers compile out this form, thus removing the check.
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 `PetscReal` input is a Not-a-Number (NaN) value.
82 
83     Input Parameter:
84 .     a - the floating point number
85 
86      Level: beginner
87 
88     Developer Notes:
89     Uses the C99 standard `isnan()` on systems where it exists.
90 
91     Otherwise uses (a != a), note that some optimizing compilers compile
92     out this form, thus removing the check.
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