| 1 | #ifndef Py_PYMATH_H |
| 2 | #define Py_PYMATH_H |
| 3 | |
| 4 | /************************************************************************** |
| 5 | Symbols and macros to supply platform-independent interfaces to mathematical |
| 6 | functions and constants |
| 7 | **************************************************************************/ |
| 8 | |
| 9 | /* High precision definition of pi and e (Euler) |
| 10 | * The values are taken from libc6's math.h. |
| 11 | */ |
| 12 | #ifndef Py_MATH_PIl |
| 13 | #define Py_MATH_PIl 3.1415926535897932384626433832795029L |
| 14 | #endif |
| 15 | #ifndef Py_MATH_PI |
| 16 | #define Py_MATH_PI 3.14159265358979323846 |
| 17 | #endif |
| 18 | |
| 19 | #ifndef Py_MATH_El |
| 20 | #define Py_MATH_El 2.7182818284590452353602874713526625L |
| 21 | #endif |
| 22 | |
| 23 | #ifndef Py_MATH_E |
| 24 | #define Py_MATH_E 2.7182818284590452354 |
| 25 | #endif |
| 26 | |
| 27 | /* Tau (2pi) to 40 digits, taken from tauday.com/tau-digits. */ |
| 28 | #ifndef Py_MATH_TAU |
| 29 | #define Py_MATH_TAU 6.2831853071795864769252867665590057683943L |
| 30 | #endif |
| 31 | |
| 32 | /* Py_IS_NAN(X) |
| 33 | * Return 1 if float or double arg is a NaN, else 0. |
| 34 | * Caution: |
| 35 | * X is evaluated more than once. |
| 36 | * This may not work on all platforms. Each platform has *some* |
| 37 | * way to spell this, though -- override in pyconfig.h if you have |
| 38 | * a platform where it doesn't work. |
| 39 | */ |
| 40 | #ifndef Py_IS_NAN |
| 41 | #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L |
| 42 | #define Py_IS_NAN(X) isnan(X) |
| 43 | #else |
| 44 | #define Py_IS_NAN(X) ((X) != (X)) |
| 45 | #endif |
| 46 | #endif |
| 47 | |
| 48 | /* Py_IS_INFINITY(X) |
| 49 | * Return 1 if float or double arg is an infinity, else 0. |
| 50 | * Caution: |
| 51 | * X is evaluated more than once. |
| 52 | * This implementation may set the underflow flag if |X| is very small; |
| 53 | * it really can't be implemented correctly (& easily) before C99. |
| 54 | * Override in pyconfig.h if you have a better spelling on your platform. |
| 55 | */ |
| 56 | #ifndef Py_IS_INFINITY |
| 57 | # if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L |
| 58 | # define Py_IS_INFINITY(X) isinf(X) |
| 59 | # else |
| 60 | # define Py_IS_INFINITY(X) ((X) && ((X)*0.5 == (X))) |
| 61 | # endif |
| 62 | #endif |
| 63 | |
| 64 | /* Py_IS_FINITE(X) |
| 65 | * Return 1 if float or double arg is neither infinite nor NAN, else 0. |
| 66 | * Some compilers (e.g. VisualStudio) have intrinsics for this, so a special |
| 67 | * macro for this particular test is useful |
| 68 | */ |
| 69 | #ifndef Py_IS_FINITE |
| 70 | #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L |
| 71 | #define Py_IS_FINITE(X) isfinite(X) |
| 72 | #else |
| 73 | #define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X)) |
| 74 | #endif |
| 75 | #endif |
| 76 | |
| 77 | /* HUGE_VAL is supposed to expand to a positive double infinity. Python |
| 78 | * uses Py_HUGE_VAL instead because some platforms are broken in this |
| 79 | * respect. We used to embed code in pyport.h to try to worm around that, |
| 80 | * but different platforms are broken in conflicting ways. If you're on |
| 81 | * a platform where HUGE_VAL is defined incorrectly, fiddle your Python |
| 82 | * config to #define Py_HUGE_VAL to something that works on your platform. |
| 83 | */ |
| 84 | #ifndef Py_HUGE_VAL |
| 85 | #define Py_HUGE_VAL HUGE_VAL |
| 86 | #endif |
| 87 | |
| 88 | /* Py_NAN: Value that evaluates to a quiet Not-a-Number (NaN). The sign is |
| 89 | * undefined and normally not relevant, but e.g. fixed for float("nan"). |
| 90 | */ |
| 91 | #if !defined(Py_NAN) |
| 92 | # define Py_NAN ((double)NAN) |
| 93 | #endif |
| 94 | /* Return whether integral type *type* is signed or not. */ |
| 95 | #define _Py_IntegralTypeSigned(type) ((type)(-1) < 0) |
| 96 | /* Return the maximum value of integral type *type*. */ |
| 97 | #define _Py_IntegralTypeMax(type) ((_Py_IntegralTypeSigned(type)) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0) |
| 98 | /* Return the minimum value of integral type *type*. */ |
| 99 | #define _Py_IntegralTypeMin(type) ((_Py_IntegralTypeSigned(type)) ? -_Py_IntegralTypeMax(type) - 1 : 0) |
| 100 | /* Check whether *v* is in the range of integral type *type*. This is most |
| 101 | * useful if *v* is floating-point, since demoting a floating-point *v* to an |
| 102 | * integral type that cannot represent *v*'s integral part is undefined |
| 103 | * behavior. */ |
| 104 | #define _Py_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v <= _Py_IntegralTypeMax(type)) |
| 105 | |
| 106 | #endif /* Py_PYMATH_H */ |