Chris@87: #ifndef Py_PYMATH_H Chris@87: #define Py_PYMATH_H Chris@87: Chris@87: #include "pyconfig.h" /* include for defines */ Chris@87: Chris@87: /************************************************************************** Chris@87: Symbols and macros to supply platform-independent interfaces to mathematical Chris@87: functions and constants Chris@87: **************************************************************************/ Chris@87: Chris@87: /* Python provides implementations for copysign, round and hypot in Chris@87: * Python/pymath.c just in case your math library doesn't provide the Chris@87: * functions. Chris@87: * Chris@87: *Note: PC/pyconfig.h defines copysign as _copysign Chris@87: */ Chris@87: #ifndef HAVE_COPYSIGN Chris@87: extern double copysign(double, double); Chris@87: #endif Chris@87: Chris@87: #ifndef HAVE_ROUND Chris@87: extern double round(double); Chris@87: #endif Chris@87: Chris@87: #ifndef HAVE_HYPOT Chris@87: extern double hypot(double, double); Chris@87: #endif Chris@87: Chris@87: /* extra declarations */ Chris@87: #ifndef _MSC_VER Chris@87: #ifndef __STDC__ Chris@87: extern double fmod (double, double); Chris@87: extern double frexp (double, int *); Chris@87: extern double ldexp (double, int); Chris@87: extern double modf (double, double *); Chris@87: extern double pow(double, double); Chris@87: #endif /* __STDC__ */ Chris@87: #endif /* _MSC_VER */ Chris@87: Chris@87: #ifdef _OSF_SOURCE Chris@87: /* OSF1 5.1 doesn't make these available with XOPEN_SOURCE_EXTENDED defined */ Chris@87: extern int finite(double); Chris@87: extern double copysign(double, double); Chris@87: #endif Chris@87: Chris@87: /* High precision defintion of pi and e (Euler) Chris@87: * The values are taken from libc6's math.h. Chris@87: */ Chris@87: #ifndef Py_MATH_PIl Chris@87: #define Py_MATH_PIl 3.1415926535897932384626433832795029L Chris@87: #endif Chris@87: #ifndef Py_MATH_PI Chris@87: #define Py_MATH_PI 3.14159265358979323846 Chris@87: #endif Chris@87: Chris@87: #ifndef Py_MATH_El Chris@87: #define Py_MATH_El 2.7182818284590452353602874713526625L Chris@87: #endif Chris@87: Chris@87: #ifndef Py_MATH_E Chris@87: #define Py_MATH_E 2.7182818284590452354 Chris@87: #endif Chris@87: Chris@87: /* On x86, Py_FORCE_DOUBLE forces a floating-point number out of an x87 FPU Chris@87: register and into a 64-bit memory location, rounding from extended Chris@87: precision to double precision in the process. On other platforms it does Chris@87: nothing. */ Chris@87: Chris@87: /* we take double rounding as evidence of x87 usage */ Chris@87: #ifndef Py_FORCE_DOUBLE Chris@87: # ifdef X87_DOUBLE_ROUNDING Chris@87: PyAPI_FUNC(double) _Py_force_double(double); Chris@87: # define Py_FORCE_DOUBLE(X) (_Py_force_double(X)) Chris@87: # else Chris@87: # define Py_FORCE_DOUBLE(X) (X) Chris@87: # endif Chris@87: #endif Chris@87: Chris@87: #ifdef HAVE_GCC_ASM_FOR_X87 Chris@87: PyAPI_FUNC(unsigned short) _Py_get_387controlword(void); Chris@87: PyAPI_FUNC(void) _Py_set_387controlword(unsigned short); Chris@87: #endif Chris@87: Chris@87: /* Py_IS_NAN(X) Chris@87: * Return 1 if float or double arg is a NaN, else 0. Chris@87: * Caution: Chris@87: * X is evaluated more than once. Chris@87: * This may not work on all platforms. Each platform has *some* Chris@87: * way to spell this, though -- override in pyconfig.h if you have Chris@87: * a platform where it doesn't work. Chris@87: * Note: PC/pyconfig.h defines Py_IS_NAN as _isnan Chris@87: */ Chris@87: #ifndef Py_IS_NAN Chris@87: #if defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 1 Chris@87: #define Py_IS_NAN(X) isnan(X) Chris@87: #else Chris@87: #define Py_IS_NAN(X) ((X) != (X)) Chris@87: #endif Chris@87: #endif Chris@87: Chris@87: /* Py_IS_INFINITY(X) Chris@87: * Return 1 if float or double arg is an infinity, else 0. Chris@87: * Caution: Chris@87: * X is evaluated more than once. Chris@87: * This implementation may set the underflow flag if |X| is very small; Chris@87: * it really can't be implemented correctly (& easily) before C99. Chris@87: * Override in pyconfig.h if you have a better spelling on your platform. Chris@87: * Py_FORCE_DOUBLE is used to avoid getting false negatives from a Chris@87: * non-infinite value v sitting in an 80-bit x87 register such that Chris@87: * v becomes infinite when spilled from the register to 64-bit memory. Chris@87: * Note: PC/pyconfig.h defines Py_IS_INFINITY as _isinf Chris@87: */ Chris@87: #ifndef Py_IS_INFINITY Chris@87: # if defined HAVE_DECL_ISINF && HAVE_DECL_ISINF == 1 Chris@87: # define Py_IS_INFINITY(X) isinf(X) Chris@87: # else Chris@87: # define Py_IS_INFINITY(X) ((X) && \ Chris@87: (Py_FORCE_DOUBLE(X)*0.5 == Py_FORCE_DOUBLE(X))) Chris@87: # endif Chris@87: #endif Chris@87: Chris@87: /* Py_IS_FINITE(X) Chris@87: * Return 1 if float or double arg is neither infinite nor NAN, else 0. Chris@87: * Some compilers (e.g. VisualStudio) have intrisics for this, so a special Chris@87: * macro for this particular test is useful Chris@87: * Note: PC/pyconfig.h defines Py_IS_FINITE as _finite Chris@87: */ Chris@87: #ifndef Py_IS_FINITE Chris@87: #if defined HAVE_DECL_ISFINITE && HAVE_DECL_ISFINITE == 1 Chris@87: #define Py_IS_FINITE(X) isfinite(X) Chris@87: #elif defined HAVE_FINITE Chris@87: #define Py_IS_FINITE(X) finite(X) Chris@87: #else Chris@87: #define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X)) Chris@87: #endif Chris@87: #endif Chris@87: Chris@87: /* HUGE_VAL is supposed to expand to a positive double infinity. Python Chris@87: * uses Py_HUGE_VAL instead because some platforms are broken in this Chris@87: * respect. We used to embed code in pyport.h to try to worm around that, Chris@87: * but different platforms are broken in conflicting ways. If you're on Chris@87: * a platform where HUGE_VAL is defined incorrectly, fiddle your Python Chris@87: * config to #define Py_HUGE_VAL to something that works on your platform. Chris@87: */ Chris@87: #ifndef Py_HUGE_VAL Chris@87: #define Py_HUGE_VAL HUGE_VAL Chris@87: #endif Chris@87: Chris@87: /* Py_NAN Chris@87: * A value that evaluates to a NaN. On IEEE 754 platforms INF*0 or Chris@87: * INF/INF works. Define Py_NO_NAN in pyconfig.h if your platform Chris@87: * doesn't support NaNs. Chris@87: */ Chris@87: #if !defined(Py_NAN) && !defined(Py_NO_NAN) Chris@87: #define Py_NAN (Py_HUGE_VAL * 0.) Chris@87: #endif Chris@87: Chris@87: /* Py_OVERFLOWED(X) Chris@87: * Return 1 iff a libm function overflowed. Set errno to 0 before calling Chris@87: * a libm function, and invoke this macro after, passing the function Chris@87: * result. Chris@87: * Caution: Chris@87: * This isn't reliable. C99 no longer requires libm to set errno under Chris@87: * any exceptional condition, but does require +- HUGE_VAL return Chris@87: * values on overflow. A 754 box *probably* maps HUGE_VAL to a Chris@87: * double infinity, and we're cool if that's so, unless the input Chris@87: * was an infinity and an infinity is the expected result. A C89 Chris@87: * system sets errno to ERANGE, so we check for that too. We're Chris@87: * out of luck if a C99 754 box doesn't map HUGE_VAL to +Inf, or Chris@87: * if the returned result is a NaN, or if a C89 box returns HUGE_VAL Chris@87: * in non-overflow cases. Chris@87: * X is evaluated more than once. Chris@87: * Some platforms have better way to spell this, so expect some #ifdef'ery. Chris@87: * Chris@87: * OpenBSD uses 'isinf()' because a compiler bug on that platform causes Chris@87: * the longer macro version to be mis-compiled. This isn't optimal, and Chris@87: * should be removed once a newer compiler is available on that platform. Chris@87: * The system that had the failure was running OpenBSD 3.2 on Intel, with Chris@87: * gcc 2.95.3. Chris@87: * Chris@87: * According to Tim's checkin, the FreeBSD systems use isinf() to work Chris@87: * around a FPE bug on that platform. Chris@87: */ Chris@87: #if defined(__FreeBSD__) || defined(__OpenBSD__) Chris@87: #define Py_OVERFLOWED(X) isinf(X) Chris@87: #else Chris@87: #define Py_OVERFLOWED(X) ((X) != 0.0 && (errno == ERANGE || \ Chris@87: (X) == Py_HUGE_VAL || \ Chris@87: (X) == -Py_HUGE_VAL)) Chris@87: #endif Chris@87: Chris@87: #endif /* Py_PYMATH_H */