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