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