Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1 | #ifndef Py_PYMATH_H |
| 2 | #define Py_PYMATH_H |
| 3 | |
| 4 | #include "pyconfig.h" /* include for defines */ |
| 5 | |
| 6 | #ifdef HAVE_STDINT_H |
| 7 | #include <stdint.h> |
| 8 | #endif |
| 9 | |
| 10 | /************************************************************************** |
| 11 | Symbols and macros to supply platform-independent interfaces to mathematical |
| 12 | functions and constants |
| 13 | **************************************************************************/ |
| 14 | |
| 15 | /* Python provides implementations for copysign, acosh, asinh, atanh, |
| 16 | * log1p and hypot in Python/pymath.c just in case your math library doesn't |
| 17 | * provide the functions. |
| 18 | * |
| 19 | *Note: PC/pyconfig.h defines copysign as _copysign |
| 20 | */ |
| 21 | #ifndef HAVE_COPYSIGN |
Benjamin Peterson | 4aeec04 | 2008-08-19 21:42:13 +0000 | [diff] [blame] | 22 | extern double copysign(double, double); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | #ifndef HAVE_ACOSH |
| 26 | extern double acosh(double); |
| 27 | #endif |
| 28 | |
| 29 | #ifndef HAVE_ASINH |
| 30 | extern double asinh(double); |
| 31 | #endif |
| 32 | |
| 33 | #ifndef HAVE_ATANH |
| 34 | extern double atanh(double); |
| 35 | #endif |
| 36 | |
| 37 | #ifndef HAVE_LOG1P |
| 38 | extern double log1p(double); |
| 39 | #endif |
| 40 | |
| 41 | #ifndef HAVE_HYPOT |
| 42 | extern double hypot(double, double); |
| 43 | #endif |
| 44 | |
| 45 | /* extra declarations */ |
| 46 | #ifndef _MSC_VER |
| 47 | #ifndef __STDC__ |
| 48 | extern double fmod (double, double); |
| 49 | extern double frexp (double, int *); |
| 50 | extern double ldexp (double, int); |
| 51 | extern double modf (double, double *); |
| 52 | extern double pow(double, double); |
| 53 | #endif /* __STDC__ */ |
| 54 | #endif /* _MSC_VER */ |
| 55 | |
| 56 | #ifdef _OSF_SOURCE |
| 57 | /* OSF1 5.1 doesn't make these available with XOPEN_SOURCE_EXTENDED defined */ |
| 58 | extern int finite(double); |
| 59 | extern double copysign(double, double); |
| 60 | #endif |
| 61 | |
| 62 | /* High precision defintion of pi and e (Euler) |
| 63 | * The values are taken from libc6's math.h. |
| 64 | */ |
| 65 | #ifndef Py_MATH_PIl |
| 66 | #define Py_MATH_PIl 3.1415926535897932384626433832795029L |
| 67 | #endif |
| 68 | #ifndef Py_MATH_PI |
| 69 | #define Py_MATH_PI 3.14159265358979323846 |
| 70 | #endif |
| 71 | |
| 72 | #ifndef Py_MATH_El |
| 73 | #define Py_MATH_El 2.7182818284590452353602874713526625L |
| 74 | #endif |
| 75 | |
| 76 | #ifndef Py_MATH_E |
| 77 | #define Py_MATH_E 2.7182818284590452354 |
| 78 | #endif |
| 79 | |
Mark Dickinson | 87ec085 | 2009-02-09 17:15:59 +0000 | [diff] [blame] | 80 | /* On x86, Py_FORCE_DOUBLE forces a floating-point number out of an x87 FPU |
| 81 | register and into a 64-bit memory location, rounding from extended |
| 82 | precision to double precision in the process. On other platforms it does |
| 83 | nothing. */ |
| 84 | |
| 85 | /* we take double rounding as evidence of x87 usage */ |
| 86 | #ifndef Py_FORCE_DOUBLE |
| 87 | # ifdef X87_DOUBLE_ROUNDING |
| 88 | PyAPI_FUNC(double) _Py_force_double(double); |
| 89 | # define Py_FORCE_DOUBLE(X) (_Py_force_double(X)) |
| 90 | # else |
| 91 | # define Py_FORCE_DOUBLE(X) (X) |
| 92 | # endif |
| 93 | #endif |
| 94 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 95 | /* Py_IS_NAN(X) |
| 96 | * Return 1 if float or double arg is a NaN, else 0. |
| 97 | * Caution: |
| 98 | * X is evaluated more than once. |
| 99 | * This may not work on all platforms. Each platform has *some* |
| 100 | * way to spell this, though -- override in pyconfig.h if you have |
| 101 | * a platform where it doesn't work. |
| 102 | * Note: PC/pyconfig.h defines Py_IS_NAN as _isnan |
| 103 | */ |
| 104 | #ifndef Py_IS_NAN |
Mark Dickinson | 6cb2bdd | 2009-01-04 17:02:56 +0000 | [diff] [blame] | 105 | #if defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 1 |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 106 | #define Py_IS_NAN(X) isnan(X) |
| 107 | #else |
| 108 | #define Py_IS_NAN(X) ((X) != (X)) |
| 109 | #endif |
| 110 | #endif |
| 111 | |
| 112 | /* Py_IS_INFINITY(X) |
| 113 | * Return 1 if float or double arg is an infinity, else 0. |
| 114 | * Caution: |
| 115 | * X is evaluated more than once. |
| 116 | * This implementation may set the underflow flag if |X| is very small; |
| 117 | * it really can't be implemented correctly (& easily) before C99. |
| 118 | * Override in pyconfig.h if you have a better spelling on your platform. |
Mark Dickinson | 87ec085 | 2009-02-09 17:15:59 +0000 | [diff] [blame] | 119 | * Py_FORCE_DOUBLE is used to avoid getting false negatives from a |
| 120 | * non-infinite value v sitting in an 80-bit x87 register such that |
| 121 | * v becomes infinite when spilled from the register to 64-bit memory. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 122 | * Note: PC/pyconfig.h defines Py_IS_INFINITY as _isinf |
| 123 | */ |
| 124 | #ifndef Py_IS_INFINITY |
Mark Dickinson | 87ec085 | 2009-02-09 17:15:59 +0000 | [diff] [blame] | 125 | # if defined HAVE_DECL_ISINF && HAVE_DECL_ISINF == 1 |
| 126 | # define Py_IS_INFINITY(X) isinf(X) |
| 127 | # else |
| 128 | # define Py_IS_INFINITY(X) ((X) && \ |
| 129 | (Py_FORCE_DOUBLE(X)*0.5 == Py_FORCE_DOUBLE(X))) |
| 130 | # endif |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 131 | #endif |
| 132 | |
| 133 | /* Py_IS_FINITE(X) |
| 134 | * Return 1 if float or double arg is neither infinite nor NAN, else 0. |
| 135 | * Some compilers (e.g. VisualStudio) have intrisics for this, so a special |
| 136 | * macro for this particular test is useful |
| 137 | * Note: PC/pyconfig.h defines Py_IS_FINITE as _finite |
| 138 | */ |
| 139 | #ifndef Py_IS_FINITE |
Mark Dickinson | 52144f5 | 2009-01-05 17:08:27 +0000 | [diff] [blame] | 140 | #if defined HAVE_DECL_ISFINITE && HAVE_DECL_ISFINITE == 1 |
| 141 | #define Py_IS_FINITE(X) isfinite(X) |
| 142 | #elif defined HAVE_FINITE |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 143 | #define Py_IS_FINITE(X) finite(X) |
| 144 | #else |
| 145 | #define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X)) |
| 146 | #endif |
| 147 | #endif |
| 148 | |
| 149 | /* HUGE_VAL is supposed to expand to a positive double infinity. Python |
| 150 | * uses Py_HUGE_VAL instead because some platforms are broken in this |
| 151 | * respect. We used to embed code in pyport.h to try to worm around that, |
| 152 | * but different platforms are broken in conflicting ways. If you're on |
| 153 | * a platform where HUGE_VAL is defined incorrectly, fiddle your Python |
| 154 | * config to #define Py_HUGE_VAL to something that works on your platform. |
| 155 | */ |
| 156 | #ifndef Py_HUGE_VAL |
| 157 | #define Py_HUGE_VAL HUGE_VAL |
| 158 | #endif |
| 159 | |
| 160 | /* Py_NAN |
| 161 | * A value that evaluates to a NaN. On IEEE 754 platforms INF*0 or |
| 162 | * INF/INF works. Define Py_NO_NAN in pyconfig.h if your platform |
| 163 | * doesn't support NaNs. |
| 164 | */ |
| 165 | #if !defined(Py_NAN) && !defined(Py_NO_NAN) |
| 166 | #define Py_NAN (Py_HUGE_VAL * 0.) |
| 167 | #endif |
| 168 | |
| 169 | /* Py_OVERFLOWED(X) |
| 170 | * Return 1 iff a libm function overflowed. Set errno to 0 before calling |
| 171 | * a libm function, and invoke this macro after, passing the function |
| 172 | * result. |
| 173 | * Caution: |
| 174 | * This isn't reliable. C99 no longer requires libm to set errno under |
| 175 | * any exceptional condition, but does require +- HUGE_VAL return |
| 176 | * values on overflow. A 754 box *probably* maps HUGE_VAL to a |
| 177 | * double infinity, and we're cool if that's so, unless the input |
| 178 | * was an infinity and an infinity is the expected result. A C89 |
| 179 | * system sets errno to ERANGE, so we check for that too. We're |
| 180 | * out of luck if a C99 754 box doesn't map HUGE_VAL to +Inf, or |
| 181 | * if the returned result is a NaN, or if a C89 box returns HUGE_VAL |
| 182 | * in non-overflow cases. |
| 183 | * X is evaluated more than once. |
| 184 | * Some platforms have better way to spell this, so expect some #ifdef'ery. |
| 185 | * |
| 186 | * OpenBSD uses 'isinf()' because a compiler bug on that platform causes |
| 187 | * the longer macro version to be mis-compiled. This isn't optimal, and |
| 188 | * should be removed once a newer compiler is available on that platform. |
| 189 | * The system that had the failure was running OpenBSD 3.2 on Intel, with |
| 190 | * gcc 2.95.3. |
| 191 | * |
| 192 | * According to Tim's checkin, the FreeBSD systems use isinf() to work |
| 193 | * around a FPE bug on that platform. |
| 194 | */ |
| 195 | #if defined(__FreeBSD__) || defined(__OpenBSD__) |
| 196 | #define Py_OVERFLOWED(X) isinf(X) |
| 197 | #else |
| 198 | #define Py_OVERFLOWED(X) ((X) != 0.0 && (errno == ERANGE || \ |
| 199 | (X) == Py_HUGE_VAL || \ |
| 200 | (X) == -Py_HUGE_VAL)) |
| 201 | #endif |
| 202 | |
| 203 | #endif /* Py_PYMATH_H */ |