blob: 816691abd9677693e6a4db95913c7ed5cba00078 [file] [log] [blame]
Christian Heimes53876d92008-04-19 00:31:39 +00001#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/**************************************************************************
11Symbols and macros to supply platform-independent interfaces to mathematical
12functions 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 Peterson4aeec042008-08-19 21:42:13 +000022extern double copysign(double, double);
Christian Heimes53876d92008-04-19 00:31:39 +000023#endif
24
Mark Dickinsonf2537862009-04-18 13:58:18 +000025#ifndef HAVE_ROUND
26extern double round(double);
27#endif
28
Christian Heimes53876d92008-04-19 00:31:39 +000029#ifndef HAVE_ACOSH
30extern double acosh(double);
31#endif
32
33#ifndef HAVE_ASINH
34extern double asinh(double);
35#endif
36
37#ifndef HAVE_ATANH
38extern double atanh(double);
39#endif
40
41#ifndef HAVE_LOG1P
42extern double log1p(double);
43#endif
44
45#ifndef HAVE_HYPOT
46extern double hypot(double, double);
47#endif
48
49/* extra declarations */
50#ifndef _MSC_VER
51#ifndef __STDC__
52extern double fmod (double, double);
53extern double frexp (double, int *);
54extern double ldexp (double, int);
55extern double modf (double, double *);
56extern double pow(double, double);
57#endif /* __STDC__ */
58#endif /* _MSC_VER */
59
60#ifdef _OSF_SOURCE
61/* OSF1 5.1 doesn't make these available with XOPEN_SOURCE_EXTENDED defined */
62extern int finite(double);
63extern double copysign(double, double);
64#endif
65
66/* High precision defintion of pi and e (Euler)
67 * The values are taken from libc6's math.h.
68 */
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
75
76#ifndef Py_MATH_El
77#define Py_MATH_El 2.7182818284590452353602874713526625L
78#endif
79
80#ifndef Py_MATH_E
81#define Py_MATH_E 2.7182818284590452354
82#endif
83
Mark Dickinson87ec0852009-02-09 17:15:59 +000084/* 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. */
88
89/* we take double rounding as evidence of x87 usage */
90#ifndef Py_FORCE_DOUBLE
91# ifdef X87_DOUBLE_ROUNDING
92PyAPI_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
98
Mark Dickinsonb08a53a2009-04-16 19:52:09 +000099#ifdef HAVE_GCC_ASM_FOR_X87
100PyAPI_FUNC(unsigned short) _Py_get_387controlword(void);
101PyAPI_FUNC(void) _Py_set_387controlword(unsigned short);
102#endif
103
Christian Heimes53876d92008-04-19 00:31:39 +0000104/* Py_IS_NAN(X)
105 * Return 1 if float or double arg is a NaN, else 0.
106 * Caution:
107 * X is evaluated more than once.
108 * This may not work on all platforms. Each platform has *some*
109 * way to spell this, though -- override in pyconfig.h if you have
110 * a platform where it doesn't work.
111 * Note: PC/pyconfig.h defines Py_IS_NAN as _isnan
112 */
113#ifndef Py_IS_NAN
Mark Dickinson6cb2bdd2009-01-04 17:02:56 +0000114#if defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 1
Christian Heimes53876d92008-04-19 00:31:39 +0000115#define Py_IS_NAN(X) isnan(X)
116#else
117#define Py_IS_NAN(X) ((X) != (X))
118#endif
119#endif
120
121/* Py_IS_INFINITY(X)
122 * Return 1 if float or double arg is an infinity, else 0.
123 * Caution:
124 * X is evaluated more than once.
125 * This implementation may set the underflow flag if |X| is very small;
126 * it really can't be implemented correctly (& easily) before C99.
127 * Override in pyconfig.h if you have a better spelling on your platform.
Mark Dickinson87ec0852009-02-09 17:15:59 +0000128 * Py_FORCE_DOUBLE is used to avoid getting false negatives from a
129 * non-infinite value v sitting in an 80-bit x87 register such that
130 * v becomes infinite when spilled from the register to 64-bit memory.
Christian Heimes53876d92008-04-19 00:31:39 +0000131 * Note: PC/pyconfig.h defines Py_IS_INFINITY as _isinf
132 */
133#ifndef Py_IS_INFINITY
Mark Dickinson87ec0852009-02-09 17:15:59 +0000134# if defined HAVE_DECL_ISINF && HAVE_DECL_ISINF == 1
135# define Py_IS_INFINITY(X) isinf(X)
136# else
137# define Py_IS_INFINITY(X) ((X) && \
138 (Py_FORCE_DOUBLE(X)*0.5 == Py_FORCE_DOUBLE(X)))
139# endif
Christian Heimes53876d92008-04-19 00:31:39 +0000140#endif
141
142/* Py_IS_FINITE(X)
143 * Return 1 if float or double arg is neither infinite nor NAN, else 0.
144 * Some compilers (e.g. VisualStudio) have intrisics for this, so a special
145 * macro for this particular test is useful
146 * Note: PC/pyconfig.h defines Py_IS_FINITE as _finite
147 */
148#ifndef Py_IS_FINITE
Mark Dickinson52144f52009-01-05 17:08:27 +0000149#if defined HAVE_DECL_ISFINITE && HAVE_DECL_ISFINITE == 1
150#define Py_IS_FINITE(X) isfinite(X)
151#elif defined HAVE_FINITE
Christian Heimes53876d92008-04-19 00:31:39 +0000152#define Py_IS_FINITE(X) finite(X)
153#else
154#define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X))
155#endif
156#endif
157
158/* HUGE_VAL is supposed to expand to a positive double infinity. Python
159 * uses Py_HUGE_VAL instead because some platforms are broken in this
160 * respect. We used to embed code in pyport.h to try to worm around that,
161 * but different platforms are broken in conflicting ways. If you're on
162 * a platform where HUGE_VAL is defined incorrectly, fiddle your Python
163 * config to #define Py_HUGE_VAL to something that works on your platform.
164 */
165#ifndef Py_HUGE_VAL
166#define Py_HUGE_VAL HUGE_VAL
167#endif
168
169/* Py_NAN
170 * A value that evaluates to a NaN. On IEEE 754 platforms INF*0 or
171 * INF/INF works. Define Py_NO_NAN in pyconfig.h if your platform
172 * doesn't support NaNs.
173 */
174#if !defined(Py_NAN) && !defined(Py_NO_NAN)
175#define Py_NAN (Py_HUGE_VAL * 0.)
176#endif
177
178/* Py_OVERFLOWED(X)
179 * Return 1 iff a libm function overflowed. Set errno to 0 before calling
180 * a libm function, and invoke this macro after, passing the function
181 * result.
182 * Caution:
183 * This isn't reliable. C99 no longer requires libm to set errno under
184 * any exceptional condition, but does require +- HUGE_VAL return
185 * values on overflow. A 754 box *probably* maps HUGE_VAL to a
186 * double infinity, and we're cool if that's so, unless the input
187 * was an infinity and an infinity is the expected result. A C89
188 * system sets errno to ERANGE, so we check for that too. We're
189 * out of luck if a C99 754 box doesn't map HUGE_VAL to +Inf, or
190 * if the returned result is a NaN, or if a C89 box returns HUGE_VAL
191 * in non-overflow cases.
192 * X is evaluated more than once.
193 * Some platforms have better way to spell this, so expect some #ifdef'ery.
194 *
195 * OpenBSD uses 'isinf()' because a compiler bug on that platform causes
196 * the longer macro version to be mis-compiled. This isn't optimal, and
197 * should be removed once a newer compiler is available on that platform.
198 * The system that had the failure was running OpenBSD 3.2 on Intel, with
199 * gcc 2.95.3.
200 *
201 * According to Tim's checkin, the FreeBSD systems use isinf() to work
202 * around a FPE bug on that platform.
203 */
204#if defined(__FreeBSD__) || defined(__OpenBSD__)
205#define Py_OVERFLOWED(X) isinf(X)
206#else
207#define Py_OVERFLOWED(X) ((X) != 0.0 && (errno == ERANGE || \
208 (X) == Py_HUGE_VAL || \
209 (X) == -Py_HUGE_VAL))
210#endif
211
212#endif /* Py_PYMATH_H */