xref: /petsc/src/sys/utils/mathinf.c (revision 57622a8ec272e2944ba90348a8875f087fbb2bc6)
1 #include <petscsys.h>
2 /*@C
3       PetscIsNormal - Returns PETSC_TRUE if the input value satisfies isnormal()
4 
5     Input Parameter:
6 .     a - the PetscRealValue
7 
8      Notes: uses the C99 standard isnormal() on systems where they exist.
9       Uses isnormalq() with __float128
10       Otherwises always returns true
11 
12      Level: beginner
13 @*/
14 #if defined(PETSC_USE_REAL___FLOAT128)
15 PetscBool PetscIsNormalScalar(PetscScalar a)
16 {
17   return PETSC_TRUE;
18 }
19 PetscBool PetscIsNormalReal(PetscReal a)
20 {
21   return PETSC_TRUE;
22 }
23 #elif defined(PETSC_HAVE_ISNORMAL)
24 PetscBool PetscIsNormalScalar(PetscScalar a)
25 {
26   return isnormal(PetscAbsScalar(a)) ? PETSC_TRUE : PETSC_FALSE;
27 }
28 PetscBool PetscIsNormalReal(PetscReal a)
29 {
30   return isnormal(a) ? PETSC_TRUE : PETSC_FALSE;
31 }
32 #else
33 PetscBool PetscIsNormalScalar(PetscScalar a)
34 {
35   return PETSC_TRUE;
36 }
37 PetscBool PetscIsNormalReal(PetscReal a)
38 {
39   return PETSC_TRUE;
40 }
41 #endif
42 
43 /*@C
44       PetscIsInfOrNan - Returns an error code if the input double has an infinity for Not-a-number (Nan) value, otherwise 0.
45 
46     Input Parameter:
47 .     a - the floating point number
48 
49      Notes: uses the C99 standard isinf() and isnan() on systems where they exist.
50       Otherwises uses ((a - a) != 0.0), note that some optimizing compiles compile
51       out this form, thus removing the check.
52 
53      Level: beginner
54 @*/
55 #if defined(PETSC_USE_REAL___FLOAT128)
56 PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a)
57 {
58   return isinfq(PetscAbsScalar(a)) || isnanq(PetscAbsScalar(a));
59 }
60 PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
61 {
62   return isinfq(a) || isnanq(a);
63 }
64 #elif defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN)
65 PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a)
66 {
67   return isinf(PetscAbsScalar(a)) || isnan(PetscAbsScalar(a));
68 }
69 PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
70 {
71   return isinf(a) || isnan(a);
72 }
73 #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN)
74 #if defined(PETSC_HAVE_FLOAT_H)
75 #include "float.h"  /* Microsoft Windows defines _finite() in float.h */
76 #endif
77 #if defined(PETSC_HAVE_IEEEFP_H)
78 #include "ieeefp.h"  /* Solaris prototypes these here */
79 #endif
80 PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a)
81 {
82   return !_finite(PetscAbsScalar(a)) || _isnan(PetscAbsScalar(a));
83 }
84 PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
85 {
86   return !_finite(a) || _isnan(a);
87 }
88 #else
89 PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a)
90 {
91   return  ((a - a) != (PetscScalar)0);
92 }
93 PetscErrorCode PetscIsInfOrNanReal(PetscReal a)
94 {
95   return ((a - a) != 0);
96 }
97 #endif
98 
99