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