blob: 45b03028753a3fe1d8019fc293eeae5ececb3c63 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Math module -- standard C math library functions, pi and e */
2
Christian Heimes53876d92008-04-19 00:31:39 +00003/* Here are some comments from Tim Peters, extracted from the
4 discussion attached to http://bugs.python.org/issue1640. They
5 describe the general aims of the math module with respect to
6 special values, IEEE-754 floating-point exceptions, and Python
7 exceptions.
8
9These are the "spirit of 754" rules:
10
111. If the mathematical result is a real number, but of magnitude too
12large to approximate by a machine float, overflow is signaled and the
13result is an infinity (with the appropriate sign).
14
152. If the mathematical result is a real number, but of magnitude too
16small to approximate by a machine float, underflow is signaled and the
17result is a zero (with the appropriate sign).
18
193. At a singularity (a value x such that the limit of f(y) as y
20approaches x exists and is an infinity), "divide by zero" is signaled
21and the result is an infinity (with the appropriate sign). This is
22complicated a little by that the left-side and right-side limits may
23not be the same; e.g., 1/x approaches +inf or -inf as x approaches 0
24from the positive or negative directions. In that specific case, the
25sign of the zero determines the result of 1/0.
26
274. At a point where a function has no defined result in the extended
28reals (i.e., the reals plus an infinity or two), invalid operation is
29signaled and a NaN is returned.
30
31And these are what Python has historically /tried/ to do (but not
32always successfully, as platform libm behavior varies a lot):
33
34For #1, raise OverflowError.
35
36For #2, return a zero (with the appropriate sign if that happens by
37accident ;-)).
38
39For #3 and #4, raise ValueError. It may have made sense to raise
40Python's ZeroDivisionError in #3, but historically that's only been
41raised for division by zero and mod by zero.
42
43*/
44
45/*
46 In general, on an IEEE-754 platform the aim is to follow the C99
47 standard, including Annex 'F', whenever possible. Where the
48 standard recommends raising the 'divide-by-zero' or 'invalid'
49 floating-point exceptions, Python should raise a ValueError. Where
50 the standard recommends raising 'overflow', Python should raise an
51 OverflowError. In all other circumstances a value should be
52 returned.
53 */
54
Barry Warsaw8b43b191996-12-09 22:32:36 +000055#include "Python.h"
Niklas Fiekas794e7d12020-06-15 14:33:48 +020056#include "pycore_bitutils.h" // _Py_bit_length()
Victor Stinnere9e7d282020-02-12 22:54:42 +010057#include "pycore_dtoa.h"
Mark Dickinson664b5112009-12-16 20:23:42 +000058#include "_math.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059
Serhiy Storchakac9ea9332017-01-19 18:13:09 +020060#include "clinic/mathmodule.c.h"
61
62/*[clinic input]
63module math
64[clinic start generated code]*/
65/*[clinic end generated code: output=da39a3ee5e6b4b0d input=76bc7002685dd942]*/
66
67
Mark Dickinson12c4bdb2009-09-28 19:21:11 +000068/*
69 sin(pi*x), giving accurate results for all finite x (especially x
70 integral or close to an integer). This is here for use in the
71 reflection formula for the gamma function. It conforms to IEEE
72 754-2008 for finite arguments, but not for infinities or nans.
73*/
Tim Petersa40c7932001-09-05 22:36:56 +000074
Mark Dickinson12c4bdb2009-09-28 19:21:11 +000075static const double pi = 3.141592653589793238462643383279502884197;
Mark Dickinson9c91eb82010-07-07 16:17:31 +000076static const double logpi = 1.144729885849400174143427351353058711647;
Louie Lu7a264642017-03-31 01:05:10 +080077#if !defined(HAVE_ERF) || !defined(HAVE_ERFC)
78static const double sqrtpi = 1.772453850905516027298167483341145182798;
79#endif /* !defined(HAVE_ERF) || !defined(HAVE_ERFC) */
Mark Dickinson12c4bdb2009-09-28 19:21:11 +000080
Raymond Hettingercfd735e2019-01-29 20:39:53 -080081
82/* Version of PyFloat_AsDouble() with in-line fast paths
83 for exact floats and integers. Gives a substantial
84 speed improvement for extracting float arguments.
85*/
86
87#define ASSIGN_DOUBLE(target_var, obj, error_label) \
88 if (PyFloat_CheckExact(obj)) { \
89 target_var = PyFloat_AS_DOUBLE(obj); \
90 } \
91 else if (PyLong_CheckExact(obj)) { \
92 target_var = PyLong_AsDouble(obj); \
93 if (target_var == -1.0 && PyErr_Occurred()) { \
94 goto error_label; \
95 } \
96 } \
97 else { \
98 target_var = PyFloat_AsDouble(obj); \
99 if (target_var == -1.0 && PyErr_Occurred()) { \
100 goto error_label; \
101 } \
102 }
103
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000104static double
Dima Pasechnikf57cd822019-02-26 06:36:11 +0000105m_sinpi(double x)
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 double y, r;
108 int n;
109 /* this function should only ever be called for finite arguments */
110 assert(Py_IS_FINITE(x));
111 y = fmod(fabs(x), 2.0);
112 n = (int)round(2.0*y);
113 assert(0 <= n && n <= 4);
114 switch (n) {
115 case 0:
116 r = sin(pi*y);
117 break;
118 case 1:
119 r = cos(pi*(y-0.5));
120 break;
121 case 2:
122 /* N.B. -sin(pi*(y-1.0)) is *not* equivalent: it would give
123 -0.0 instead of 0.0 when y == 1.0. */
124 r = sin(pi*(1.0-y));
125 break;
126 case 3:
127 r = -cos(pi*(y-1.5));
128 break;
129 case 4:
130 r = sin(pi*(y-2.0));
131 break;
132 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700133 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 }
135 return copysign(1.0, x)*r;
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000136}
137
138/* Implementation of the real gamma function. In extensive but non-exhaustive
139 random tests, this function proved accurate to within <= 10 ulps across the
140 entire float domain. Note that accuracy may depend on the quality of the
141 system math functions, the pow function in particular. Special cases
142 follow C99 annex F. The parameters and method are tailored to platforms
143 whose double format is the IEEE 754 binary64 format.
144
145 Method: for x > 0.0 we use the Lanczos approximation with parameters N=13
146 and g=6.024680040776729583740234375; these parameters are amongst those
147 used by the Boost library. Following Boost (again), we re-express the
148 Lanczos sum as a rational function, and compute it that way. The
149 coefficients below were computed independently using MPFR, and have been
150 double-checked against the coefficients in the Boost source code.
151
152 For x < 0.0 we use the reflection formula.
153
154 There's one minor tweak that deserves explanation: Lanczos' formula for
155 Gamma(x) involves computing pow(x+g-0.5, x-0.5) / exp(x+g-0.5). For many x
156 values, x+g-0.5 can be represented exactly. However, in cases where it
157 can't be represented exactly the small error in x+g-0.5 can be magnified
158 significantly by the pow and exp calls, especially for large x. A cheap
159 correction is to multiply by (1 + e*g/(x+g-0.5)), where e is the error
160 involved in the computation of x+g-0.5 (that is, e = computed value of
161 x+g-0.5 - exact value of x+g-0.5). Here's the proof:
162
163 Correction factor
164 -----------------
165 Write x+g-0.5 = y-e, where y is exactly representable as an IEEE 754
166 double, and e is tiny. Then:
167
168 pow(x+g-0.5,x-0.5)/exp(x+g-0.5) = pow(y-e, x-0.5)/exp(y-e)
169 = pow(y, x-0.5)/exp(y) * C,
170
171 where the correction_factor C is given by
172
173 C = pow(1-e/y, x-0.5) * exp(e)
174
175 Since e is tiny, pow(1-e/y, x-0.5) ~ 1-(x-0.5)*e/y, and exp(x) ~ 1+e, so:
176
177 C ~ (1-(x-0.5)*e/y) * (1+e) ~ 1 + e*(y-(x-0.5))/y
178
179 But y-(x-0.5) = g+e, and g+e ~ g. So we get C ~ 1 + e*g/y, and
180
181 pow(x+g-0.5,x-0.5)/exp(x+g-0.5) ~ pow(y, x-0.5)/exp(y) * (1 + e*g/y),
182
183 Note that for accuracy, when computing r*C it's better to do
184
185 r + e*g/y*r;
186
187 than
188
189 r * (1 + e*g/y);
190
191 since the addition in the latter throws away most of the bits of
192 information in e*g/y.
193*/
194
195#define LANCZOS_N 13
196static const double lanczos_g = 6.024680040776729583740234375;
197static const double lanczos_g_minus_half = 5.524680040776729583740234375;
198static const double lanczos_num_coeffs[LANCZOS_N] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 23531376880.410759688572007674451636754734846804940,
200 42919803642.649098768957899047001988850926355848959,
201 35711959237.355668049440185451547166705960488635843,
202 17921034426.037209699919755754458931112671403265390,
203 6039542586.3520280050642916443072979210699388420708,
204 1439720407.3117216736632230727949123939715485786772,
205 248874557.86205415651146038641322942321632125127801,
206 31426415.585400194380614231628318205362874684987640,
207 2876370.6289353724412254090516208496135991145378768,
208 186056.26539522349504029498971604569928220784236328,
209 8071.6720023658162106380029022722506138218516325024,
210 210.82427775157934587250973392071336271166969580291,
211 2.5066282746310002701649081771338373386264310793408
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000212};
213
214/* denominator is x*(x+1)*...*(x+LANCZOS_N-2) */
215static const double lanczos_den_coeffs[LANCZOS_N] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 0.0, 39916800.0, 120543840.0, 150917976.0, 105258076.0, 45995730.0,
217 13339535.0, 2637558.0, 357423.0, 32670.0, 1925.0, 66.0, 1.0};
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000218
219/* gamma values for small positive integers, 1 though NGAMMA_INTEGRAL */
220#define NGAMMA_INTEGRAL 23
221static const double gamma_integral[NGAMMA_INTEGRAL] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0, 5040.0, 40320.0, 362880.0,
223 3628800.0, 39916800.0, 479001600.0, 6227020800.0, 87178291200.0,
224 1307674368000.0, 20922789888000.0, 355687428096000.0,
225 6402373705728000.0, 121645100408832000.0, 2432902008176640000.0,
226 51090942171709440000.0, 1124000727777607680000.0,
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000227};
228
229/* Lanczos' sum L_g(x), for positive x */
230
231static double
232lanczos_sum(double x)
233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 double num = 0.0, den = 0.0;
235 int i;
236 assert(x > 0.0);
237 /* evaluate the rational function lanczos_sum(x). For large
238 x, the obvious algorithm risks overflow, so we instead
239 rescale the denominator and numerator of the rational
240 function by x**(1-LANCZOS_N) and treat this as a
241 rational function in 1/x. This also reduces the error for
242 larger x values. The choice of cutoff point (5.0 below) is
243 somewhat arbitrary; in tests, smaller cutoff values than
244 this resulted in lower accuracy. */
245 if (x < 5.0) {
246 for (i = LANCZOS_N; --i >= 0; ) {
247 num = num * x + lanczos_num_coeffs[i];
248 den = den * x + lanczos_den_coeffs[i];
249 }
250 }
251 else {
252 for (i = 0; i < LANCZOS_N; i++) {
253 num = num / x + lanczos_num_coeffs[i];
254 den = den / x + lanczos_den_coeffs[i];
255 }
256 }
257 return num/den;
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000258}
259
Mark Dickinsona5d0c7c2015-01-11 11:55:29 +0000260/* Constant for +infinity, generated in the same way as float('inf'). */
261
262static double
263m_inf(void)
264{
265#ifndef PY_NO_SHORT_FLOAT_REPR
266 return _Py_dg_infinity(0);
267#else
268 return Py_HUGE_VAL;
269#endif
270}
271
272/* Constant nan value, generated in the same way as float('nan'). */
273/* We don't currently assume that Py_NAN is defined everywhere. */
274
275#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
276
277static double
278m_nan(void)
279{
280#ifndef PY_NO_SHORT_FLOAT_REPR
281 return _Py_dg_stdnan(0);
282#else
283 return Py_NAN;
284#endif
285}
286
287#endif
288
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000289static double
290m_tgamma(double x)
291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 double absx, r, y, z, sqrtpow;
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 /* special cases */
295 if (!Py_IS_FINITE(x)) {
296 if (Py_IS_NAN(x) || x > 0.0)
297 return x; /* tgamma(nan) = nan, tgamma(inf) = inf */
298 else {
299 errno = EDOM;
300 return Py_NAN; /* tgamma(-inf) = nan, invalid */
301 }
302 }
303 if (x == 0.0) {
304 errno = EDOM;
Mark Dickinson50203a62011-09-25 15:26:43 +0100305 /* tgamma(+-0.0) = +-inf, divide-by-zero */
306 return copysign(Py_HUGE_VAL, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 }
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 /* integer arguments */
310 if (x == floor(x)) {
311 if (x < 0.0) {
312 errno = EDOM; /* tgamma(n) = nan, invalid for */
313 return Py_NAN; /* negative integers n */
314 }
315 if (x <= NGAMMA_INTEGRAL)
316 return gamma_integral[(int)x - 1];
317 }
318 absx = fabs(x);
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* tiny arguments: tgamma(x) ~ 1/x for x near 0 */
321 if (absx < 1e-20) {
322 r = 1.0/x;
323 if (Py_IS_INFINITY(r))
324 errno = ERANGE;
325 return r;
326 }
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 /* large arguments: assuming IEEE 754 doubles, tgamma(x) overflows for
329 x > 200, and underflows to +-0.0 for x < -200, not a negative
330 integer. */
331 if (absx > 200.0) {
332 if (x < 0.0) {
Dima Pasechnikf57cd822019-02-26 06:36:11 +0000333 return 0.0/m_sinpi(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 }
335 else {
336 errno = ERANGE;
337 return Py_HUGE_VAL;
338 }
339 }
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 y = absx + lanczos_g_minus_half;
342 /* compute error in sum */
343 if (absx > lanczos_g_minus_half) {
344 /* note: the correction can be foiled by an optimizing
345 compiler that (incorrectly) thinks that an expression like
346 a + b - a - b can be optimized to 0.0. This shouldn't
347 happen in a standards-conforming compiler. */
348 double q = y - absx;
349 z = q - lanczos_g_minus_half;
350 }
351 else {
352 double q = y - lanczos_g_minus_half;
353 z = q - absx;
354 }
355 z = z * lanczos_g / y;
356 if (x < 0.0) {
Dima Pasechnikf57cd822019-02-26 06:36:11 +0000357 r = -pi / m_sinpi(absx) / absx * exp(y) / lanczos_sum(absx);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 r -= z * r;
359 if (absx < 140.0) {
360 r /= pow(y, absx - 0.5);
361 }
362 else {
363 sqrtpow = pow(y, absx / 2.0 - 0.25);
364 r /= sqrtpow;
365 r /= sqrtpow;
366 }
367 }
368 else {
369 r = lanczos_sum(absx) / exp(y);
370 r += z * r;
371 if (absx < 140.0) {
372 r *= pow(y, absx - 0.5);
373 }
374 else {
375 sqrtpow = pow(y, absx / 2.0 - 0.25);
376 r *= sqrtpow;
377 r *= sqrtpow;
378 }
379 }
380 if (Py_IS_INFINITY(r))
381 errno = ERANGE;
382 return r;
Guido van Rossum8832b621991-12-16 15:44:24 +0000383}
384
Christian Heimes53876d92008-04-19 00:31:39 +0000385/*
Mark Dickinson05d2e082009-12-11 20:17:17 +0000386 lgamma: natural log of the absolute value of the Gamma function.
387 For large arguments, Lanczos' formula works extremely well here.
388*/
389
390static double
391m_lgamma(double x)
392{
Serhiy Storchaka97553fd2017-03-11 23:37:16 +0200393 double r;
Serhiy Storchaka97553fd2017-03-11 23:37:16 +0200394 double absx;
Mark Dickinson05d2e082009-12-11 20:17:17 +0000395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 /* special cases */
397 if (!Py_IS_FINITE(x)) {
398 if (Py_IS_NAN(x))
399 return x; /* lgamma(nan) = nan */
400 else
401 return Py_HUGE_VAL; /* lgamma(+-inf) = +inf */
402 }
Mark Dickinson05d2e082009-12-11 20:17:17 +0000403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 /* integer arguments */
405 if (x == floor(x) && x <= 2.0) {
406 if (x <= 0.0) {
407 errno = EDOM; /* lgamma(n) = inf, divide-by-zero for */
408 return Py_HUGE_VAL; /* integers n <= 0 */
409 }
410 else {
411 return 0.0; /* lgamma(1) = lgamma(2) = 0.0 */
412 }
413 }
Mark Dickinson05d2e082009-12-11 20:17:17 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 absx = fabs(x);
416 /* tiny arguments: lgamma(x) ~ -log(fabs(x)) for small x */
417 if (absx < 1e-20)
418 return -log(absx);
Mark Dickinson05d2e082009-12-11 20:17:17 +0000419
Mark Dickinson9c91eb82010-07-07 16:17:31 +0000420 /* Lanczos' formula. We could save a fraction of a ulp in accuracy by
421 having a second set of numerator coefficients for lanczos_sum that
422 absorbed the exp(-lanczos_g) term, and throwing out the lanczos_g
423 subtraction below; it's probably not worth it. */
424 r = log(lanczos_sum(absx)) - lanczos_g;
425 r += (absx - 0.5) * (log(absx + lanczos_g - 0.5) - 1);
426 if (x < 0.0)
427 /* Use reflection formula to get value for negative x. */
Dima Pasechnikf57cd822019-02-26 06:36:11 +0000428 r = logpi - log(fabs(m_sinpi(absx))) - log(absx) - r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (Py_IS_INFINITY(r))
430 errno = ERANGE;
431 return r;
Mark Dickinson05d2e082009-12-11 20:17:17 +0000432}
433
Serhiy Storchaka97553fd2017-03-11 23:37:16 +0200434#if !defined(HAVE_ERF) || !defined(HAVE_ERFC)
435
Mark Dickinson45f992a2009-12-19 11:20:49 +0000436/*
437 Implementations of the error function erf(x) and the complementary error
438 function erfc(x).
439
Brett Cannon45adb312016-01-15 09:38:24 -0800440 Method: we use a series approximation for erf for small x, and a continued
441 fraction approximation for erfc(x) for larger x;
Mark Dickinson45f992a2009-12-19 11:20:49 +0000442 combined with the relations erf(-x) = -erf(x) and erfc(x) = 1.0 - erf(x),
443 this gives us erf(x) and erfc(x) for all x.
444
445 The series expansion used is:
446
447 erf(x) = x*exp(-x*x)/sqrt(pi) * [
448 2/1 + 4/3 x**2 + 8/15 x**4 + 16/105 x**6 + ...]
449
450 The coefficient of x**(2k-2) here is 4**k*factorial(k)/factorial(2*k).
451 This series converges well for smallish x, but slowly for larger x.
452
453 The continued fraction expansion used is:
454
455 erfc(x) = x*exp(-x*x)/sqrt(pi) * [1/(0.5 + x**2 -) 0.5/(2.5 + x**2 - )
456 3.0/(4.5 + x**2 - ) 7.5/(6.5 + x**2 - ) ...]
457
458 after the first term, the general term has the form:
459
460 k*(k-0.5)/(2*k+0.5 + x**2 - ...).
461
462 This expansion converges fast for larger x, but convergence becomes
463 infinitely slow as x approaches 0.0. The (somewhat naive) continued
464 fraction evaluation algorithm used below also risks overflow for large x;
465 but for large x, erfc(x) == 0.0 to within machine precision. (For
466 example, erfc(30.0) is approximately 2.56e-393).
467
468 Parameters: use series expansion for abs(x) < ERF_SERIES_CUTOFF and
469 continued fraction expansion for ERF_SERIES_CUTOFF <= abs(x) <
470 ERFC_CONTFRAC_CUTOFF. ERFC_SERIES_TERMS and ERFC_CONTFRAC_TERMS are the
471 numbers of terms to use for the relevant expansions. */
472
473#define ERF_SERIES_CUTOFF 1.5
474#define ERF_SERIES_TERMS 25
475#define ERFC_CONTFRAC_CUTOFF 30.0
476#define ERFC_CONTFRAC_TERMS 50
477
478/*
479 Error function, via power series.
480
481 Given a finite float x, return an approximation to erf(x).
482 Converges reasonably fast for small x.
483*/
484
485static double
486m_erf_series(double x)
487{
Mark Dickinsonbcdf9da2010-06-13 10:52:38 +0000488 double x2, acc, fk, result;
489 int i, saved_errno;
Mark Dickinson45f992a2009-12-19 11:20:49 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 x2 = x * x;
492 acc = 0.0;
493 fk = (double)ERF_SERIES_TERMS + 0.5;
494 for (i = 0; i < ERF_SERIES_TERMS; i++) {
495 acc = 2.0 + x2 * acc / fk;
496 fk -= 1.0;
497 }
Mark Dickinsonbcdf9da2010-06-13 10:52:38 +0000498 /* Make sure the exp call doesn't affect errno;
499 see m_erfc_contfrac for more. */
500 saved_errno = errno;
501 result = acc * x * exp(-x2) / sqrtpi;
502 errno = saved_errno;
503 return result;
Mark Dickinson45f992a2009-12-19 11:20:49 +0000504}
505
506/*
507 Complementary error function, via continued fraction expansion.
508
509 Given a positive float x, return an approximation to erfc(x). Converges
510 reasonably fast for x large (say, x > 2.0), and should be safe from
511 overflow if x and nterms are not too large. On an IEEE 754 machine, with x
512 <= 30.0, we're safe up to nterms = 100. For x >= 30.0, erfc(x) is smaller
513 than the smallest representable nonzero float. */
514
515static double
516m_erfc_contfrac(double x)
517{
Mark Dickinsonbcdf9da2010-06-13 10:52:38 +0000518 double x2, a, da, p, p_last, q, q_last, b, result;
519 int i, saved_errno;
Mark Dickinson45f992a2009-12-19 11:20:49 +0000520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 if (x >= ERFC_CONTFRAC_CUTOFF)
522 return 0.0;
Mark Dickinson45f992a2009-12-19 11:20:49 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 x2 = x*x;
525 a = 0.0;
526 da = 0.5;
527 p = 1.0; p_last = 0.0;
528 q = da + x2; q_last = 1.0;
529 for (i = 0; i < ERFC_CONTFRAC_TERMS; i++) {
530 double temp;
531 a += da;
532 da += 2.0;
533 b = da + x2;
534 temp = p; p = b*p - a*p_last; p_last = temp;
535 temp = q; q = b*q - a*q_last; q_last = temp;
536 }
Mark Dickinsonbcdf9da2010-06-13 10:52:38 +0000537 /* Issue #8986: On some platforms, exp sets errno on underflow to zero;
538 save the current errno value so that we can restore it later. */
539 saved_errno = errno;
540 result = p / q * x * exp(-x2) / sqrtpi;
541 errno = saved_errno;
542 return result;
Mark Dickinson45f992a2009-12-19 11:20:49 +0000543}
544
Serhiy Storchaka97553fd2017-03-11 23:37:16 +0200545#endif /* !defined(HAVE_ERF) || !defined(HAVE_ERFC) */
546
Mark Dickinson45f992a2009-12-19 11:20:49 +0000547/* Error function erf(x), for general x */
548
549static double
550m_erf(double x)
551{
Serhiy Storchaka97553fd2017-03-11 23:37:16 +0200552#ifdef HAVE_ERF
553 return erf(x);
554#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 double absx, cf;
Mark Dickinson45f992a2009-12-19 11:20:49 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 if (Py_IS_NAN(x))
558 return x;
559 absx = fabs(x);
560 if (absx < ERF_SERIES_CUTOFF)
561 return m_erf_series(x);
562 else {
563 cf = m_erfc_contfrac(absx);
564 return x > 0.0 ? 1.0 - cf : cf - 1.0;
565 }
Serhiy Storchaka97553fd2017-03-11 23:37:16 +0200566#endif
Mark Dickinson45f992a2009-12-19 11:20:49 +0000567}
568
569/* Complementary error function erfc(x), for general x. */
570
571static double
572m_erfc(double x)
573{
Serhiy Storchaka97553fd2017-03-11 23:37:16 +0200574#ifdef HAVE_ERFC
575 return erfc(x);
576#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 double absx, cf;
Mark Dickinson45f992a2009-12-19 11:20:49 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (Py_IS_NAN(x))
580 return x;
581 absx = fabs(x);
582 if (absx < ERF_SERIES_CUTOFF)
583 return 1.0 - m_erf_series(x);
584 else {
585 cf = m_erfc_contfrac(absx);
586 return x > 0.0 ? cf : 2.0 - cf;
587 }
Serhiy Storchaka97553fd2017-03-11 23:37:16 +0200588#endif
Mark Dickinson45f992a2009-12-19 11:20:49 +0000589}
Mark Dickinson05d2e082009-12-11 20:17:17 +0000590
591/*
Christian Heimese57950f2008-04-21 13:08:03 +0000592 wrapper for atan2 that deals directly with special cases before
593 delegating to the platform libm for the remaining cases. This
594 is necessary to get consistent behaviour across platforms.
595 Windows, FreeBSD and alpha Tru64 are amongst platforms that don't
596 always follow C99.
597*/
598
599static double
600m_atan2(double y, double x)
601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 if (Py_IS_NAN(x) || Py_IS_NAN(y))
603 return Py_NAN;
604 if (Py_IS_INFINITY(y)) {
605 if (Py_IS_INFINITY(x)) {
606 if (copysign(1., x) == 1.)
607 /* atan2(+-inf, +inf) == +-pi/4 */
608 return copysign(0.25*Py_MATH_PI, y);
609 else
610 /* atan2(+-inf, -inf) == +-pi*3/4 */
611 return copysign(0.75*Py_MATH_PI, y);
612 }
613 /* atan2(+-inf, x) == +-pi/2 for finite x */
614 return copysign(0.5*Py_MATH_PI, y);
615 }
616 if (Py_IS_INFINITY(x) || y == 0.) {
617 if (copysign(1., x) == 1.)
618 /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
619 return copysign(0., y);
620 else
621 /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
622 return copysign(Py_MATH_PI, y);
623 }
624 return atan2(y, x);
Christian Heimese57950f2008-04-21 13:08:03 +0000625}
626
Mark Dickinsona0ce3752017-04-05 18:34:27 +0100627
628/* IEEE 754-style remainder operation: x - n*y where n*y is the nearest
629 multiple of y to x, taking n even in the case of a tie. Assuming an IEEE 754
630 binary floating-point format, the result is always exact. */
631
632static double
633m_remainder(double x, double y)
634{
635 /* Deal with most common case first. */
636 if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) {
637 double absx, absy, c, m, r;
638
639 if (y == 0.0) {
640 return Py_NAN;
641 }
642
643 absx = fabs(x);
644 absy = fabs(y);
645 m = fmod(absx, absy);
646
647 /*
648 Warning: some subtlety here. What we *want* to know at this point is
649 whether the remainder m is less than, equal to, or greater than half
650 of absy. However, we can't do that comparison directly because we
Mark Dickinson01484702019-07-13 16:50:03 +0100651 can't be sure that 0.5*absy is representable (the multiplication
Mark Dickinsona0ce3752017-04-05 18:34:27 +0100652 might incur precision loss due to underflow). So instead we compare
653 m with the complement c = absy - m: m < 0.5*absy if and only if m <
654 c, and so on. The catch is that absy - m might also not be
655 representable, but it turns out that it doesn't matter:
656
657 - if m > 0.5*absy then absy - m is exactly representable, by
658 Sterbenz's lemma, so m > c
659 - if m == 0.5*absy then again absy - m is exactly representable
660 and m == c
661 - if m < 0.5*absy then either (i) 0.5*absy is exactly representable,
662 in which case 0.5*absy < absy - m, so 0.5*absy <= c and hence m <
663 c, or (ii) absy is tiny, either subnormal or in the lowest normal
664 binade. Then absy - m is exactly representable and again m < c.
665 */
666
667 c = absy - m;
668 if (m < c) {
669 r = m;
670 }
671 else if (m > c) {
672 r = -c;
673 }
674 else {
675 /*
676 Here absx is exactly halfway between two multiples of absy,
677 and we need to choose the even multiple. x now has the form
678
679 absx = n * absy + m
680
681 for some integer n (recalling that m = 0.5*absy at this point).
682 If n is even we want to return m; if n is odd, we need to
683 return -m.
684
685 So
686
687 0.5 * (absx - m) = (n/2) * absy
688
689 and now reducing modulo absy gives us:
690
691 | m, if n is odd
692 fmod(0.5 * (absx - m), absy) = |
693 | 0, if n is even
694
695 Now m - 2.0 * fmod(...) gives the desired result: m
696 if n is even, -m if m is odd.
697
698 Note that all steps in fmod(0.5 * (absx - m), absy)
699 will be computed exactly, with no rounding error
700 introduced.
701 */
702 assert(m == c);
703 r = m - 2.0 * fmod(0.5 * (absx - m), absy);
704 }
705 return copysign(1.0, x) * r;
706 }
707
708 /* Special values. */
709 if (Py_IS_NAN(x)) {
710 return x;
711 }
712 if (Py_IS_NAN(y)) {
713 return y;
714 }
715 if (Py_IS_INFINITY(x)) {
716 return Py_NAN;
717 }
718 assert(Py_IS_INFINITY(y));
719 return x;
720}
721
722
Christian Heimese57950f2008-04-21 13:08:03 +0000723/*
Mark Dickinsone675f082008-12-11 21:56:00 +0000724 Various platforms (Solaris, OpenBSD) do nonstandard things for log(0),
725 log(-ve), log(NaN). Here are wrappers for log and log10 that deal with
726 special values directly, passing positive non-special values through to
727 the system log/log10.
728 */
729
730static double
731m_log(double x)
732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 if (Py_IS_FINITE(x)) {
734 if (x > 0.0)
735 return log(x);
736 errno = EDOM;
737 if (x == 0.0)
738 return -Py_HUGE_VAL; /* log(0) = -inf */
739 else
740 return Py_NAN; /* log(-ve) = nan */
741 }
742 else if (Py_IS_NAN(x))
743 return x; /* log(nan) = nan */
744 else if (x > 0.0)
745 return x; /* log(inf) = inf */
746 else {
747 errno = EDOM;
748 return Py_NAN; /* log(-inf) = nan */
749 }
Mark Dickinsone675f082008-12-11 21:56:00 +0000750}
751
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200752/*
753 log2: log to base 2.
754
755 Uses an algorithm that should:
Mark Dickinson83b8c0b2011-05-09 08:40:20 +0100756
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200757 (a) produce exact results for powers of 2, and
Mark Dickinson83b8c0b2011-05-09 08:40:20 +0100758 (b) give a monotonic log2 (for positive finite floats),
759 assuming that the system log is monotonic.
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200760*/
761
762static double
763m_log2(double x)
764{
765 if (!Py_IS_FINITE(x)) {
766 if (Py_IS_NAN(x))
767 return x; /* log2(nan) = nan */
768 else if (x > 0.0)
769 return x; /* log2(+inf) = +inf */
770 else {
771 errno = EDOM;
772 return Py_NAN; /* log2(-inf) = nan, invalid-operation */
773 }
774 }
775
776 if (x > 0.0) {
Victor Stinner8f9f8d62011-05-09 12:45:41 +0200777#ifdef HAVE_LOG2
778 return log2(x);
779#else
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200780 double m;
781 int e;
782 m = frexp(x, &e);
783 /* We want log2(m * 2**e) == log(m) / log(2) + e. Care is needed when
784 * x is just greater than 1.0: in that case e is 1, log(m) is negative,
785 * and we get significant cancellation error from the addition of
786 * log(m) / log(2) to e. The slight rewrite of the expression below
787 * avoids this problem.
788 */
789 if (x >= 1.0) {
790 return log(2.0 * m) / log(2.0) + (e - 1);
791 }
792 else {
793 return log(m) / log(2.0) + e;
794 }
Victor Stinner8f9f8d62011-05-09 12:45:41 +0200795#endif
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200796 }
797 else if (x == 0.0) {
798 errno = EDOM;
799 return -Py_HUGE_VAL; /* log2(0) = -inf, divide-by-zero */
800 }
801 else {
802 errno = EDOM;
Mark Dickinson23442582011-05-09 08:05:00 +0100803 return Py_NAN; /* log2(-inf) = nan, invalid-operation */
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200804 }
805}
806
Mark Dickinsone675f082008-12-11 21:56:00 +0000807static double
808m_log10(double x)
809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 if (Py_IS_FINITE(x)) {
811 if (x > 0.0)
812 return log10(x);
813 errno = EDOM;
814 if (x == 0.0)
815 return -Py_HUGE_VAL; /* log10(0) = -inf */
816 else
817 return Py_NAN; /* log10(-ve) = nan */
818 }
819 else if (Py_IS_NAN(x))
820 return x; /* log10(nan) = nan */
821 else if (x > 0.0)
822 return x; /* log10(inf) = inf */
823 else {
824 errno = EDOM;
825 return Py_NAN; /* log10(-inf) = nan */
826 }
Mark Dickinsone675f082008-12-11 21:56:00 +0000827}
828
829
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200830static PyObject *
Serhiy Storchaka559e7f12020-02-23 13:21:29 +0200831math_gcd(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200832{
Serhiy Storchaka559e7f12020-02-23 13:21:29 +0200833 PyObject *res, *x;
834 Py_ssize_t i;
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +0300835
Serhiy Storchaka559e7f12020-02-23 13:21:29 +0200836 if (nargs == 0) {
837 return PyLong_FromLong(0);
838 }
839 res = PyNumber_Index(args[0]);
840 if (res == NULL) {
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +0300841 return NULL;
842 }
Serhiy Storchaka559e7f12020-02-23 13:21:29 +0200843 if (nargs == 1) {
844 Py_SETREF(res, PyNumber_Absolute(res));
845 return res;
846 }
847 for (i = 1; i < nargs; i++) {
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300848 x = _PyNumber_Index(args[i]);
Serhiy Storchaka559e7f12020-02-23 13:21:29 +0200849 if (x == NULL) {
850 Py_DECREF(res);
851 return NULL;
852 }
853 if (res == _PyLong_One) {
854 /* Fast path: just check arguments.
855 It is okay to use identity comparison here. */
856 Py_DECREF(x);
857 continue;
858 }
859 Py_SETREF(res, _PyLong_GCD(res, x));
860 Py_DECREF(x);
861 if (res == NULL) {
862 return NULL;
863 }
864 }
865 return res;
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +0300866}
867
Serhiy Storchaka559e7f12020-02-23 13:21:29 +0200868PyDoc_STRVAR(math_gcd_doc,
869"gcd($module, *integers)\n"
870"--\n"
871"\n"
872"Greatest Common Divisor.");
873
874
875static PyObject *
876long_lcm(PyObject *a, PyObject *b)
877{
878 PyObject *g, *m, *f, *ab;
879
880 if (Py_SIZE(a) == 0 || Py_SIZE(b) == 0) {
881 return PyLong_FromLong(0);
882 }
883 g = _PyLong_GCD(a, b);
884 if (g == NULL) {
885 return NULL;
886 }
887 f = PyNumber_FloorDivide(a, g);
888 Py_DECREF(g);
889 if (f == NULL) {
890 return NULL;
891 }
892 m = PyNumber_Multiply(f, b);
893 Py_DECREF(f);
894 if (m == NULL) {
895 return NULL;
896 }
897 ab = PyNumber_Absolute(m);
898 Py_DECREF(m);
899 return ab;
900}
901
902
903static PyObject *
904math_lcm(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
905{
906 PyObject *res, *x;
907 Py_ssize_t i;
908
909 if (nargs == 0) {
910 return PyLong_FromLong(1);
911 }
912 res = PyNumber_Index(args[0]);
913 if (res == NULL) {
914 return NULL;
915 }
916 if (nargs == 1) {
917 Py_SETREF(res, PyNumber_Absolute(res));
918 return res;
919 }
920 for (i = 1; i < nargs; i++) {
921 x = PyNumber_Index(args[i]);
922 if (x == NULL) {
923 Py_DECREF(res);
924 return NULL;
925 }
926 if (res == _PyLong_Zero) {
927 /* Fast path: just check arguments.
928 It is okay to use identity comparison here. */
929 Py_DECREF(x);
930 continue;
931 }
932 Py_SETREF(res, long_lcm(res, x));
933 Py_DECREF(x);
934 if (res == NULL) {
935 return NULL;
936 }
937 }
938 return res;
939}
940
941
942PyDoc_STRVAR(math_lcm_doc,
943"lcm($module, *integers)\n"
944"--\n"
945"\n"
946"Least Common Multiple.");
947
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +0300948
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000949/* Call is_error when errno != 0, and where x is the result libm
950 * returned. is_error will usually set up an exception and return
951 * true (1), but may return false (0) without setting up an exception.
952 */
953static int
954is_error(double x)
955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 int result = 1; /* presumption of guilt */
957 assert(errno); /* non-zero errno is a precondition for calling */
958 if (errno == EDOM)
959 PyErr_SetString(PyExc_ValueError, "math domain error");
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 else if (errno == ERANGE) {
962 /* ANSI C generally requires libm functions to set ERANGE
963 * on overflow, but also generally *allows* them to set
964 * ERANGE on underflow too. There's no consistency about
965 * the latter across platforms.
966 * Alas, C99 never requires that errno be set.
967 * Here we suppress the underflow errors (libm functions
968 * should return a zero on underflow, and +- HUGE_VAL on
969 * overflow, so testing the result for zero suffices to
970 * distinguish the cases).
971 *
972 * On some platforms (Ubuntu/ia64) it seems that errno can be
973 * set to ERANGE for subnormal results that do *not* underflow
974 * to zero. So to be safe, we'll ignore ERANGE whenever the
975 * function result is less than one in absolute value.
976 */
977 if (fabs(x) < 1.0)
978 result = 0;
979 else
980 PyErr_SetString(PyExc_OverflowError,
981 "math range error");
982 }
983 else
984 /* Unexpected math error */
985 PyErr_SetFromErrno(PyExc_ValueError);
986 return result;
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000987}
988
Mark Dickinsone675f082008-12-11 21:56:00 +0000989/*
Christian Heimes53876d92008-04-19 00:31:39 +0000990 math_1 is used to wrap a libm function f that takes a double
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200991 argument and returns a double.
Christian Heimes53876d92008-04-19 00:31:39 +0000992
993 The error reporting follows these rules, which are designed to do
994 the right thing on C89/C99 platforms and IEEE 754/non IEEE 754
995 platforms.
996
997 - a NaN result from non-NaN inputs causes ValueError to be raised
998 - an infinite result from finite inputs causes OverflowError to be
999 raised if can_overflow is 1, or raises ValueError if can_overflow
1000 is 0.
1001 - if the result is finite and errno == EDOM then ValueError is
1002 raised
1003 - if the result is finite and nonzero and errno == ERANGE then
1004 OverflowError is raised
1005
1006 The last rule is used to catch overflow on platforms which follow
1007 C89 but for which HUGE_VAL is not an infinity.
1008
1009 For the majority of one-argument functions these rules are enough
1010 to ensure that Python's functions behave as specified in 'Annex F'
1011 of the C99 standard, with the 'invalid' and 'divide-by-zero'
1012 floating-point exceptions mapping to Python's ValueError and the
1013 'overflow' floating-point exception mapping to OverflowError.
1014 math_1 only works for functions that don't have singularities *and*
1015 the possibility of overflow; fortunately, that covers everything we
1016 care about right now.
1017*/
1018
Barry Warsaw8b43b191996-12-09 22:32:36 +00001019static PyObject *
Jeffrey Yasskinc2155832008-01-05 20:03:11 +00001020math_1_to_whatever(PyObject *arg, double (*func) (double),
Christian Heimes53876d92008-04-19 00:31:39 +00001021 PyObject *(*from_double_func) (double),
1022 int can_overflow)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 double x, r;
1025 x = PyFloat_AsDouble(arg);
1026 if (x == -1.0 && PyErr_Occurred())
1027 return NULL;
1028 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 r = (*func)(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (Py_IS_NAN(r) && !Py_IS_NAN(x)) {
1031 PyErr_SetString(PyExc_ValueError,
1032 "math domain error"); /* invalid arg */
1033 return NULL;
1034 }
1035 if (Py_IS_INFINITY(r) && Py_IS_FINITE(x)) {
Benjamin Peterson2354a752012-03-13 16:13:09 -05001036 if (can_overflow)
1037 PyErr_SetString(PyExc_OverflowError,
1038 "math range error"); /* overflow */
1039 else
1040 PyErr_SetString(PyExc_ValueError,
1041 "math domain error"); /* singularity */
1042 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 }
1044 if (Py_IS_FINITE(r) && errno && is_error(r))
1045 /* this branch unnecessary on most platforms */
1046 return NULL;
Mark Dickinsonde429622008-05-01 00:19:23 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 return (*from_double_func)(r);
Christian Heimes53876d92008-04-19 00:31:39 +00001049}
1050
Mark Dickinson12c4bdb2009-09-28 19:21:11 +00001051/* variant of math_1, to be used when the function being wrapped is known to
1052 set errno properly (that is, errno = EDOM for invalid or divide-by-zero,
1053 errno = ERANGE for overflow). */
1054
1055static PyObject *
1056math_1a(PyObject *arg, double (*func) (double))
1057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 double x, r;
1059 x = PyFloat_AsDouble(arg);
1060 if (x == -1.0 && PyErr_Occurred())
1061 return NULL;
1062 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 r = (*func)(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if (errno && is_error(r))
1065 return NULL;
1066 return PyFloat_FromDouble(r);
Mark Dickinson12c4bdb2009-09-28 19:21:11 +00001067}
1068
Christian Heimes53876d92008-04-19 00:31:39 +00001069/*
1070 math_2 is used to wrap a libm function f that takes two double
1071 arguments and returns a double.
1072
1073 The error reporting follows these rules, which are designed to do
1074 the right thing on C89/C99 platforms and IEEE 754/non IEEE 754
1075 platforms.
1076
1077 - a NaN result from non-NaN inputs causes ValueError to be raised
1078 - an infinite result from finite inputs causes OverflowError to be
1079 raised.
1080 - if the result is finite and errno == EDOM then ValueError is
1081 raised
1082 - if the result is finite and nonzero and errno == ERANGE then
1083 OverflowError is raised
1084
1085 The last rule is used to catch overflow on platforms which follow
1086 C89 but for which HUGE_VAL is not an infinity.
1087
1088 For most two-argument functions (copysign, fmod, hypot, atan2)
1089 these rules are enough to ensure that Python's functions behave as
1090 specified in 'Annex F' of the C99 standard, with the 'invalid' and
1091 'divide-by-zero' floating-point exceptions mapping to Python's
1092 ValueError and the 'overflow' floating-point exception mapping to
1093 OverflowError.
1094*/
1095
1096static PyObject *
1097math_1(PyObject *arg, double (*func) (double), int can_overflow)
1098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 return math_1_to_whatever(arg, func, PyFloat_FromDouble, can_overflow);
Jeffrey Yasskinc2155832008-01-05 20:03:11 +00001100}
1101
1102static PyObject *
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02001103math_2(PyObject *const *args, Py_ssize_t nargs,
1104 double (*func) (double, double), const char *funcname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 double x, y, r;
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02001107 if (!_PyArg_CheckPositional(funcname, nargs, 2, 2))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 return NULL;
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02001109 x = PyFloat_AsDouble(args[0]);
Zackery Spytz5208b4b2020-03-14 04:45:32 -06001110 if (x == -1.0 && PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 return NULL;
Zackery Spytz5208b4b2020-03-14 04:45:32 -06001112 }
1113 y = PyFloat_AsDouble(args[1]);
1114 if (y == -1.0 && PyErr_Occurred()) {
1115 return NULL;
1116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 r = (*func)(x, y);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 if (Py_IS_NAN(r)) {
1120 if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
1121 errno = EDOM;
1122 else
1123 errno = 0;
1124 }
1125 else if (Py_IS_INFINITY(r)) {
1126 if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
1127 errno = ERANGE;
1128 else
1129 errno = 0;
1130 }
1131 if (errno && is_error(r))
1132 return NULL;
1133 else
1134 return PyFloat_FromDouble(r);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001135}
1136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137#define FUNC1(funcname, func, can_overflow, docstring) \
1138 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
1139 return math_1(args, func, can_overflow); \
1140 }\
1141 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143#define FUNC1A(funcname, func, docstring) \
1144 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
1145 return math_1a(args, func); \
1146 }\
1147 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Mark Dickinson12c4bdb2009-09-28 19:21:11 +00001148
Fred Drake40c48682000-07-03 18:11:56 +00001149#define FUNC2(funcname, func, docstring) \
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02001150 static PyObject * math_##funcname(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { \
1151 return math_2(args, nargs, func, #funcname); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 }\
1153 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001154
Christian Heimes53876d92008-04-19 00:31:39 +00001155FUNC1(acos, acos, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001156 "acos($module, x, /)\n--\n\n"
Giovanni Cappellottodc3f99f2019-07-13 09:59:55 -04001157 "Return the arc cosine (measured in radians) of x.\n\n"
1158 "The result is between 0 and pi.")
Mark Dickinsonf3718592009-12-21 15:27:41 +00001159FUNC1(acosh, m_acosh, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001160 "acosh($module, x, /)\n--\n\n"
1161 "Return the inverse hyperbolic cosine of x.")
Christian Heimes53876d92008-04-19 00:31:39 +00001162FUNC1(asin, asin, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001163 "asin($module, x, /)\n--\n\n"
Giovanni Cappellottodc3f99f2019-07-13 09:59:55 -04001164 "Return the arc sine (measured in radians) of x.\n\n"
1165 "The result is between -pi/2 and pi/2.")
Mark Dickinsonf3718592009-12-21 15:27:41 +00001166FUNC1(asinh, m_asinh, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001167 "asinh($module, x, /)\n--\n\n"
1168 "Return the inverse hyperbolic sine of x.")
Christian Heimes53876d92008-04-19 00:31:39 +00001169FUNC1(atan, atan, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001170 "atan($module, x, /)\n--\n\n"
Giovanni Cappellottodc3f99f2019-07-13 09:59:55 -04001171 "Return the arc tangent (measured in radians) of x.\n\n"
1172 "The result is between -pi/2 and pi/2.")
Christian Heimese57950f2008-04-21 13:08:03 +00001173FUNC2(atan2, m_atan2,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001174 "atan2($module, y, x, /)\n--\n\n"
1175 "Return the arc tangent (measured in radians) of y/x.\n\n"
Tim Petersfe71f812001-08-07 22:10:00 +00001176 "Unlike atan(y/x), the signs of both x and y are considered.")
Mark Dickinsonf3718592009-12-21 15:27:41 +00001177FUNC1(atanh, m_atanh, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001178 "atanh($module, x, /)\n--\n\n"
1179 "Return the inverse hyperbolic tangent of x.")
Guido van Rossum13e05de2007-08-23 22:56:55 +00001180
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001181/*[clinic input]
1182math.ceil
1183
1184 x as number: object
1185 /
1186
1187Return the ceiling of x as an Integral.
1188
1189This is the smallest integer >= x.
1190[clinic start generated code]*/
1191
1192static PyObject *
1193math_ceil(PyObject *module, PyObject *number)
1194/*[clinic end generated code: output=6c3b8a78bc201c67 input=2725352806399cab]*/
1195{
Benjamin Petersonce798522012-01-22 11:24:29 -05001196 _Py_IDENTIFIER(__ceil__);
Guido van Rossum13e05de2007-08-23 22:56:55 +00001197
Serhiy Storchaka5fd5cb82019-11-16 18:00:57 +02001198 if (!PyFloat_CheckExact(number)) {
1199 PyObject *method = _PyObject_LookupSpecial(number, &PyId___ceil__);
1200 if (method != NULL) {
1201 PyObject *result = _PyObject_CallNoArg(method);
1202 Py_DECREF(method);
1203 return result;
1204 }
Benjamin Petersonf751bc92010-07-02 13:46:42 +00001205 if (PyErr_Occurred())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 return NULL;
Benjamin Petersonf751bc92010-07-02 13:46:42 +00001207 }
Serhiy Storchaka5fd5cb82019-11-16 18:00:57 +02001208 double x = PyFloat_AsDouble(number);
1209 if (x == -1.0 && PyErr_Occurred())
1210 return NULL;
1211
1212 return PyLong_FromDouble(ceil(x));
Guido van Rossum13e05de2007-08-23 22:56:55 +00001213}
1214
Christian Heimes072c0f12008-01-03 23:01:04 +00001215FUNC2(copysign, copysign,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001216 "copysign($module, x, y, /)\n--\n\n"
1217 "Return a float with the magnitude (absolute value) of x but the sign of y.\n\n"
1218 "On platforms that support signed zeros, copysign(1.0, -0.0)\n"
1219 "returns -1.0.\n")
Christian Heimes53876d92008-04-19 00:31:39 +00001220FUNC1(cos, cos, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001221 "cos($module, x, /)\n--\n\n"
1222 "Return the cosine of x (measured in radians).")
Christian Heimes53876d92008-04-19 00:31:39 +00001223FUNC1(cosh, cosh, 1,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001224 "cosh($module, x, /)\n--\n\n"
1225 "Return the hyperbolic cosine of x.")
Mark Dickinson45f992a2009-12-19 11:20:49 +00001226FUNC1A(erf, m_erf,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001227 "erf($module, x, /)\n--\n\n"
1228 "Error function at x.")
Mark Dickinson45f992a2009-12-19 11:20:49 +00001229FUNC1A(erfc, m_erfc,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001230 "erfc($module, x, /)\n--\n\n"
1231 "Complementary error function at x.")
Christian Heimes53876d92008-04-19 00:31:39 +00001232FUNC1(exp, exp, 1,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001233 "exp($module, x, /)\n--\n\n"
1234 "Return e raised to the power of x.")
Mark Dickinson664b5112009-12-16 20:23:42 +00001235FUNC1(expm1, m_expm1, 1,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001236 "expm1($module, x, /)\n--\n\n"
1237 "Return exp(x)-1.\n\n"
Mark Dickinson664b5112009-12-16 20:23:42 +00001238 "This function avoids the loss of precision involved in the direct "
1239 "evaluation of exp(x)-1 for small x.")
Christian Heimes53876d92008-04-19 00:31:39 +00001240FUNC1(fabs, fabs, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001241 "fabs($module, x, /)\n--\n\n"
1242 "Return the absolute value of the float x.")
Guido van Rossum13e05de2007-08-23 22:56:55 +00001243
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001244/*[clinic input]
1245math.floor
1246
1247 x as number: object
1248 /
1249
1250Return the floor of x as an Integral.
1251
1252This is the largest integer <= x.
1253[clinic start generated code]*/
1254
1255static PyObject *
1256math_floor(PyObject *module, PyObject *number)
1257/*[clinic end generated code: output=c6a65c4884884b8a input=63af6b5d7ebcc3d6]*/
1258{
Raymond Hettinger930f4512020-06-23 11:45:25 -07001259 double x;
1260
Benjamin Petersonce798522012-01-22 11:24:29 -05001261 _Py_IDENTIFIER(__floor__);
Guido van Rossum13e05de2007-08-23 22:56:55 +00001262
Raymond Hettinger930f4512020-06-23 11:45:25 -07001263 if (PyFloat_CheckExact(number)) {
1264 x = PyFloat_AS_DOUBLE(number);
1265 }
1266 else
1267 {
Serhiy Storchaka5fd5cb82019-11-16 18:00:57 +02001268 PyObject *method = _PyObject_LookupSpecial(number, &PyId___floor__);
1269 if (method != NULL) {
1270 PyObject *result = _PyObject_CallNoArg(method);
1271 Py_DECREF(method);
1272 return result;
1273 }
Benjamin Peterson8bb9cde2010-07-01 15:16:55 +00001274 if (PyErr_Occurred())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 return NULL;
Raymond Hettinger930f4512020-06-23 11:45:25 -07001276 x = PyFloat_AsDouble(number);
1277 if (x == -1.0 && PyErr_Occurred())
1278 return NULL;
Benjamin Peterson8bb9cde2010-07-01 15:16:55 +00001279 }
Serhiy Storchaka5fd5cb82019-11-16 18:00:57 +02001280 return PyLong_FromDouble(floor(x));
Guido van Rossum13e05de2007-08-23 22:56:55 +00001281}
1282
Mark Dickinson12c4bdb2009-09-28 19:21:11 +00001283FUNC1A(gamma, m_tgamma,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001284 "gamma($module, x, /)\n--\n\n"
1285 "Gamma function at x.")
Mark Dickinson05d2e082009-12-11 20:17:17 +00001286FUNC1A(lgamma, m_lgamma,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001287 "lgamma($module, x, /)\n--\n\n"
1288 "Natural logarithm of absolute value of Gamma function at x.")
Mark Dickinsonbe64d952010-07-07 16:21:29 +00001289FUNC1(log1p, m_log1p, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001290 "log1p($module, x, /)\n--\n\n"
1291 "Return the natural logarithm of 1+x (base e).\n\n"
Benjamin Petersona0dfa822009-11-13 02:25:08 +00001292 "The result is computed in a way which is accurate for x near zero.")
Mark Dickinsona0ce3752017-04-05 18:34:27 +01001293FUNC2(remainder, m_remainder,
1294 "remainder($module, x, y, /)\n--\n\n"
1295 "Difference between x and the closest integer multiple of y.\n\n"
1296 "Return x - n*y where n*y is the closest integer multiple of y.\n"
1297 "In the case where x is exactly halfway between two multiples of\n"
1298 "y, the nearest even value of n is used. The result is always exact.")
Christian Heimes53876d92008-04-19 00:31:39 +00001299FUNC1(sin, sin, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001300 "sin($module, x, /)\n--\n\n"
1301 "Return the sine of x (measured in radians).")
Christian Heimes53876d92008-04-19 00:31:39 +00001302FUNC1(sinh, sinh, 1,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001303 "sinh($module, x, /)\n--\n\n"
1304 "Return the hyperbolic sine of x.")
Christian Heimes53876d92008-04-19 00:31:39 +00001305FUNC1(sqrt, sqrt, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001306 "sqrt($module, x, /)\n--\n\n"
1307 "Return the square root of x.")
Christian Heimes53876d92008-04-19 00:31:39 +00001308FUNC1(tan, tan, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001309 "tan($module, x, /)\n--\n\n"
1310 "Return the tangent of x (measured in radians).")
Christian Heimes53876d92008-04-19 00:31:39 +00001311FUNC1(tanh, tanh, 0,
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001312 "tanh($module, x, /)\n--\n\n"
1313 "Return the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001314
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001315/* Precision summation function as msum() by Raymond Hettinger in
1316 <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090>,
1317 enhanced with the exact partials sum and roundoff from Mark
1318 Dickinson's post at <http://bugs.python.org/file10357/msum4.py>.
1319 See those links for more details, proofs and other references.
1320
1321 Note 1: IEEE 754R floating point semantics are assumed,
1322 but the current implementation does not re-establish special
1323 value semantics across iterations (i.e. handling -Inf + Inf).
1324
1325 Note 2: No provision is made for intermediate overflow handling;
Georg Brandlf78e02b2008-06-10 17:40:04 +00001326 therefore, sum([1e+308, 1e-308, 1e+308]) returns 1e+308 while
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001327 sum([1e+308, 1e+308, 1e-308]) raises an OverflowError due to the
1328 overflow of the first partial sum.
1329
Benjamin Petersonfea6a942008-07-02 16:11:42 +00001330 Note 3: The intermediate values lo, yr, and hi are declared volatile so
1331 aggressive compilers won't algebraically reduce lo to always be exactly 0.0.
Georg Brandlf78e02b2008-06-10 17:40:04 +00001332 Also, the volatile declaration forces the values to be stored in memory as
1333 regular doubles instead of extended long precision (80-bit) values. This
Benjamin Petersonfea6a942008-07-02 16:11:42 +00001334 prevents double rounding because any addition or subtraction of two doubles
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 can be resolved exactly into double-sized hi and lo values. As long as the
Georg Brandlf78e02b2008-06-10 17:40:04 +00001336 hi value gets forced into a double before yr and lo are computed, the extra
1337 bits in downstream extended precision operations (x87 for example) will be
1338 exactly zero and therefore can be losslessly stored back into a double,
1339 thereby preventing double rounding.
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001340
1341 Note 4: A similar implementation is in Modules/cmathmodule.c.
1342 Be sure to update both when making changes.
1343
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +02001344 Note 5: The signature of math.fsum() differs from builtins.sum()
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001345 because the start argument doesn't make sense in the context of
1346 accurate summation. Since the partials table is collapsed before
1347 returning a result, sum(seq2, start=sum(seq1)) may not equal the
1348 accurate result returned by sum(itertools.chain(seq1, seq2)).
1349*/
1350
1351#define NUM_PARTIALS 32 /* initial partials array size, on stack */
1352
1353/* Extend the partials array p[] by doubling its size. */
1354static int /* non-zero on error */
Mark Dickinsonaa7633a2008-08-01 08:16:13 +00001355_fsum_realloc(double **p_ptr, Py_ssize_t n,
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001356 double *ps, Py_ssize_t *m_ptr)
1357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 void *v = NULL;
1359 Py_ssize_t m = *m_ptr;
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 m += m; /* double */
Victor Stinner049e5092014-08-17 22:20:00 +02001362 if (n < m && (size_t)m < ((size_t)PY_SSIZE_T_MAX / sizeof(double))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 double *p = *p_ptr;
1364 if (p == ps) {
1365 v = PyMem_Malloc(sizeof(double) * m);
1366 if (v != NULL)
1367 memcpy(v, ps, sizeof(double) * n);
1368 }
1369 else
1370 v = PyMem_Realloc(p, sizeof(double) * m);
1371 }
1372 if (v == NULL) { /* size overflow or no memory */
1373 PyErr_SetString(PyExc_MemoryError, "math.fsum partials");
1374 return 1;
1375 }
1376 *p_ptr = (double*) v;
1377 *m_ptr = m;
1378 return 0;
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001379}
1380
1381/* Full precision summation of a sequence of floats.
1382
1383 def msum(iterable):
1384 partials = [] # sorted, non-overlapping partial sums
1385 for x in iterable:
Mark Dickinsonfdb0acc2010-06-25 20:22:24 +00001386 i = 0
1387 for y in partials:
1388 if abs(x) < abs(y):
1389 x, y = y, x
1390 hi = x + y
1391 lo = y - (hi - x)
1392 if lo:
1393 partials[i] = lo
1394 i += 1
1395 x = hi
1396 partials[i:] = [x]
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001397 return sum_exact(partials)
1398
1399 Rounded x+y stored in hi with the roundoff stored in lo. Together hi+lo
1400 are exactly equal to x+y. The inner loop applies hi/lo summation to each
1401 partial so that the list of partial sums remains exact.
1402
1403 Sum_exact() adds the partial sums exactly and correctly rounds the final
1404 result (using the round-half-to-even rule). The items in partials remain
1405 non-zero, non-special, non-overlapping and strictly increasing in
1406 magnitude, but possibly not all having the same sign.
1407
1408 Depends on IEEE 754 arithmetic guarantees and half-even rounding.
1409*/
1410
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001411/*[clinic input]
1412math.fsum
1413
1414 seq: object
1415 /
1416
1417Return an accurate floating point sum of values in the iterable seq.
1418
1419Assumes IEEE-754 floating point arithmetic.
1420[clinic start generated code]*/
1421
1422static PyObject *
1423math_fsum(PyObject *module, PyObject *seq)
1424/*[clinic end generated code: output=ba5c672b87fe34fc input=c51b7d8caf6f6e82]*/
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 PyObject *item, *iter, *sum = NULL;
1427 Py_ssize_t i, j, n = 0, m = NUM_PARTIALS;
1428 double x, y, t, ps[NUM_PARTIALS], *p = ps;
1429 double xsave, special_sum = 0.0, inf_sum = 0.0;
1430 volatile double hi, yr, lo;
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 iter = PyObject_GetIter(seq);
1433 if (iter == NULL)
1434 return NULL;
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 for(;;) { /* for x in iterable */
1437 assert(0 <= n && n <= m);
1438 assert((m == NUM_PARTIALS && p == ps) ||
1439 (m > NUM_PARTIALS && p != NULL));
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 item = PyIter_Next(iter);
1442 if (item == NULL) {
1443 if (PyErr_Occurred())
1444 goto _fsum_error;
1445 break;
1446 }
Raymond Hettingercfd735e2019-01-29 20:39:53 -08001447 ASSIGN_DOUBLE(x, item, error_with_item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 Py_DECREF(item);
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 xsave = x;
1451 for (i = j = 0; j < n; j++) { /* for y in partials */
1452 y = p[j];
1453 if (fabs(x) < fabs(y)) {
1454 t = x; x = y; y = t;
1455 }
1456 hi = x + y;
1457 yr = hi - x;
1458 lo = y - yr;
1459 if (lo != 0.0)
1460 p[i++] = lo;
1461 x = hi;
1462 }
Mark Dickinsonaa7633a2008-08-01 08:16:13 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 n = i; /* ps[i:] = [x] */
1465 if (x != 0.0) {
1466 if (! Py_IS_FINITE(x)) {
1467 /* a nonfinite x could arise either as
1468 a result of intermediate overflow, or
1469 as a result of a nan or inf in the
1470 summands */
1471 if (Py_IS_FINITE(xsave)) {
1472 PyErr_SetString(PyExc_OverflowError,
1473 "intermediate overflow in fsum");
1474 goto _fsum_error;
1475 }
1476 if (Py_IS_INFINITY(xsave))
1477 inf_sum += xsave;
1478 special_sum += xsave;
1479 /* reset partials */
1480 n = 0;
1481 }
1482 else if (n >= m && _fsum_realloc(&p, n, ps, &m))
1483 goto _fsum_error;
1484 else
1485 p[n++] = x;
1486 }
1487 }
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 if (special_sum != 0.0) {
1490 if (Py_IS_NAN(inf_sum))
1491 PyErr_SetString(PyExc_ValueError,
1492 "-inf + inf in fsum");
1493 else
1494 sum = PyFloat_FromDouble(special_sum);
1495 goto _fsum_error;
1496 }
Mark Dickinsonaa7633a2008-08-01 08:16:13 +00001497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 hi = 0.0;
1499 if (n > 0) {
1500 hi = p[--n];
1501 /* sum_exact(ps, hi) from the top, stop when the sum becomes
1502 inexact. */
1503 while (n > 0) {
1504 x = hi;
1505 y = p[--n];
1506 assert(fabs(y) < fabs(x));
1507 hi = x + y;
1508 yr = hi - x;
1509 lo = y - yr;
1510 if (lo != 0.0)
1511 break;
1512 }
1513 /* Make half-even rounding work across multiple partials.
1514 Needed so that sum([1e-16, 1, 1e16]) will round-up the last
1515 digit to two instead of down to zero (the 1e-16 makes the 1
1516 slightly closer to two). With a potential 1 ULP rounding
1517 error fixed-up, math.fsum() can guarantee commutativity. */
1518 if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) ||
1519 (lo > 0.0 && p[n-1] > 0.0))) {
1520 y = lo * 2.0;
1521 x = hi + y;
1522 yr = x - hi;
1523 if (y == yr)
1524 hi = x;
1525 }
1526 }
1527 sum = PyFloat_FromDouble(hi);
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001528
Raymond Hettingercfd735e2019-01-29 20:39:53 -08001529 _fsum_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 Py_DECREF(iter);
1531 if (p != ps)
1532 PyMem_Free(p);
1533 return sum;
Raymond Hettingercfd735e2019-01-29 20:39:53 -08001534
1535 error_with_item:
1536 Py_DECREF(item);
1537 goto _fsum_error;
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001538}
1539
1540#undef NUM_PARTIALS
1541
Benjamin Peterson2b7411d2008-05-26 17:36:47 +00001542
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00001543static unsigned long
1544count_set_bits(unsigned long n)
1545{
1546 unsigned long count = 0;
1547 while (n != 0) {
1548 ++count;
1549 n &= n - 1; /* clear least significant bit */
1550 }
1551 return count;
1552}
1553
Mark Dickinson73934b92019-05-18 12:29:50 +01001554/* Integer square root
1555
1556Given a nonnegative integer `n`, we want to compute the largest integer
1557`a` for which `a * a <= n`, or equivalently the integer part of the exact
1558square root of `n`.
1559
1560We use an adaptive-precision pure-integer version of Newton's iteration. Given
1561a positive integer `n`, the algorithm produces at each iteration an integer
1562approximation `a` to the square root of `n >> s` for some even integer `s`,
1563with `s` decreasing as the iterations progress. On the final iteration, `s` is
1564zero and we have an approximation to the square root of `n` itself.
1565
1566At every step, the approximation `a` is strictly within 1.0 of the true square
1567root, so we have
1568
1569 (a - 1)**2 < (n >> s) < (a + 1)**2
1570
1571After the final iteration, a check-and-correct step is needed to determine
1572whether `a` or `a - 1` gives the desired integer square root of `n`.
1573
1574The algorithm is remarkable in its simplicity. There's no need for a
1575per-iteration check-and-correct step, and termination is straightforward: the
1576number of iterations is known in advance (it's exactly `floor(log2(log2(n)))`
1577for `n > 1`). The only tricky part of the correctness proof is in establishing
1578that the bound `(a - 1)**2 < (n >> s) < (a + 1)**2` is maintained from one
1579iteration to the next. A sketch of the proof of this is given below.
1580
1581In addition to the proof sketch, a formal, computer-verified proof
1582of correctness (using Lean) of an equivalent recursive algorithm can be found
1583here:
1584
1585 https://github.com/mdickinson/snippets/blob/master/proofs/isqrt/src/isqrt.lean
1586
1587
1588Here's Python code equivalent to the C implementation below:
1589
1590 def isqrt(n):
1591 """
1592 Return the integer part of the square root of the input.
1593 """
1594 n = operator.index(n)
1595
1596 if n < 0:
1597 raise ValueError("isqrt() argument must be nonnegative")
1598 if n == 0:
1599 return 0
1600
1601 c = (n.bit_length() - 1) // 2
1602 a = 1
1603 d = 0
1604 for s in reversed(range(c.bit_length())):
Mark Dickinson2dfeaa92019-06-16 17:53:21 +01001605 # Loop invariant: (a-1)**2 < (n >> 2*(c - d)) < (a+1)**2
Mark Dickinson73934b92019-05-18 12:29:50 +01001606 e = d
1607 d = c >> s
1608 a = (a << d - e - 1) + (n >> 2*c - e - d + 1) // a
Mark Dickinson73934b92019-05-18 12:29:50 +01001609
1610 return a - (a*a > n)
1611
1612
1613Sketch of proof of correctness
1614------------------------------
1615
1616The delicate part of the correctness proof is showing that the loop invariant
1617is preserved from one iteration to the next. That is, just before the line
1618
1619 a = (a << d - e - 1) + (n >> 2*c - e - d + 1) // a
1620
1621is executed in the above code, we know that
1622
1623 (1) (a - 1)**2 < (n >> 2*(c - e)) < (a + 1)**2.
1624
1625(since `e` is always the value of `d` from the previous iteration). We must
1626prove that after that line is executed, we have
1627
1628 (a - 1)**2 < (n >> 2*(c - d)) < (a + 1)**2
1629
Min ho Kimf7d72e42019-07-06 07:39:32 +10001630To facilitate the proof, we make some changes of notation. Write `m` for
Mark Dickinson73934b92019-05-18 12:29:50 +01001631`n >> 2*(c-d)`, and write `b` for the new value of `a`, so
1632
1633 b = (a << d - e - 1) + (n >> 2*c - e - d + 1) // a
1634
1635or equivalently:
1636
1637 (2) b = (a << d - e - 1) + (m >> d - e + 1) // a
1638
1639Then we can rewrite (1) as:
1640
1641 (3) (a - 1)**2 < (m >> 2*(d - e)) < (a + 1)**2
1642
1643and we must show that (b - 1)**2 < m < (b + 1)**2.
1644
1645From this point on, we switch to mathematical notation, so `/` means exact
1646division rather than integer division and `^` is used for exponentiation. We
1647use the `√` symbol for the exact square root. In (3), we can remove the
1648implicit floor operation to give:
1649
1650 (4) (a - 1)^2 < m / 4^(d - e) < (a + 1)^2
1651
1652Taking square roots throughout (4), scaling by `2^(d-e)`, and rearranging gives
1653
1654 (5) 0 <= | 2^(d-e)a - √m | < 2^(d-e)
1655
1656Squaring and dividing through by `2^(d-e+1) a` gives
1657
1658 (6) 0 <= 2^(d-e-1) a + m / (2^(d-e+1) a) - √m < 2^(d-e-1) / a
1659
1660We'll show below that `2^(d-e-1) <= a`. Given that, we can replace the
1661right-hand side of (6) with `1`, and now replacing the central
1662term `m / (2^(d-e+1) a)` with its floor in (6) gives
1663
1664 (7) -1 < 2^(d-e-1) a + m // 2^(d-e+1) a - √m < 1
1665
1666Or equivalently, from (2):
1667
1668 (7) -1 < b - √m < 1
1669
1670and rearranging gives that `(b-1)^2 < m < (b+1)^2`, which is what we needed
1671to prove.
1672
1673We're not quite done: we still have to prove the inequality `2^(d - e - 1) <=
1674a` that was used to get line (7) above. From the definition of `c`, we have
1675`4^c <= n`, which implies
1676
1677 (8) 4^d <= m
1678
1679also, since `e == d >> 1`, `d` is at most `2e + 1`, from which it follows
1680that `2d - 2e - 1 <= d` and hence that
1681
1682 (9) 4^(2d - 2e - 1) <= m
1683
1684Dividing both sides by `4^(d - e)` gives
1685
1686 (10) 4^(d - e - 1) <= m / 4^(d - e)
1687
1688But we know from (4) that `m / 4^(d-e) < (a + 1)^2`, hence
1689
1690 (11) 4^(d - e - 1) < (a + 1)^2
1691
1692Now taking square roots of both sides and observing that both `2^(d-e-1)` and
1693`a` are integers gives `2^(d - e - 1) <= a`, which is what we needed. This
1694completes the proof sketch.
1695
1696*/
1697
Mark Dickinson5c08ce92019-05-19 17:51:56 +01001698
1699/* Approximate square root of a large 64-bit integer.
1700
1701 Given `n` satisfying `2**62 <= n < 2**64`, return `a`
1702 satisfying `(a - 1)**2 < n < (a + 1)**2`. */
1703
1704static uint64_t
1705_approximate_isqrt(uint64_t n)
1706{
1707 uint32_t u = 1U + (n >> 62);
1708 u = (u << 1) + (n >> 59) / u;
1709 u = (u << 3) + (n >> 53) / u;
1710 u = (u << 7) + (n >> 41) / u;
1711 return (u << 15) + (n >> 17) / u;
1712}
1713
Mark Dickinson73934b92019-05-18 12:29:50 +01001714/*[clinic input]
1715math.isqrt
1716
1717 n: object
1718 /
1719
1720Return the integer part of the square root of the input.
1721[clinic start generated code]*/
1722
1723static PyObject *
1724math_isqrt(PyObject *module, PyObject *n)
1725/*[clinic end generated code: output=35a6f7f980beab26 input=5b6e7ae4fa6c43d6]*/
1726{
Mark Dickinson5c08ce92019-05-19 17:51:56 +01001727 int a_too_large, c_bit_length;
Mark Dickinson73934b92019-05-18 12:29:50 +01001728 size_t c, d;
Mark Dickinson5c08ce92019-05-19 17:51:56 +01001729 uint64_t m, u;
Mark Dickinson73934b92019-05-18 12:29:50 +01001730 PyObject *a = NULL, *b;
1731
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +03001732 n = _PyNumber_Index(n);
Mark Dickinson73934b92019-05-18 12:29:50 +01001733 if (n == NULL) {
1734 return NULL;
1735 }
1736
1737 if (_PyLong_Sign(n) < 0) {
1738 PyErr_SetString(
1739 PyExc_ValueError,
1740 "isqrt() argument must be nonnegative");
1741 goto error;
1742 }
1743 if (_PyLong_Sign(n) == 0) {
1744 Py_DECREF(n);
1745 return PyLong_FromLong(0);
1746 }
1747
Mark Dickinson5c08ce92019-05-19 17:51:56 +01001748 /* c = (n.bit_length() - 1) // 2 */
Mark Dickinson73934b92019-05-18 12:29:50 +01001749 c = _PyLong_NumBits(n);
1750 if (c == (size_t)(-1)) {
1751 goto error;
1752 }
1753 c = (c - 1U) / 2U;
1754
Mark Dickinson5c08ce92019-05-19 17:51:56 +01001755 /* Fast path: if c <= 31 then n < 2**64 and we can compute directly with a
1756 fast, almost branch-free algorithm. In the final correction, we use `u*u
1757 - 1 >= m` instead of the simpler `u*u > m` in order to get the correct
1758 result in the corner case where `u=2**32`. */
1759 if (c <= 31U) {
1760 m = (uint64_t)PyLong_AsUnsignedLongLong(n);
1761 Py_DECREF(n);
1762 if (m == (uint64_t)(-1) && PyErr_Occurred()) {
1763 return NULL;
1764 }
1765 u = _approximate_isqrt(m << (62U - 2U*c)) >> (31U - c);
1766 u -= u * u - 1U >= m;
1767 return PyLong_FromUnsignedLongLong((unsigned long long)u);
Mark Dickinson73934b92019-05-18 12:29:50 +01001768 }
1769
Mark Dickinson5c08ce92019-05-19 17:51:56 +01001770 /* Slow path: n >= 2**64. We perform the first five iterations in C integer
1771 arithmetic, then switch to using Python long integers. */
1772
1773 /* From n >= 2**64 it follows that c.bit_length() >= 6. */
1774 c_bit_length = 6;
1775 while ((c >> c_bit_length) > 0U) {
1776 ++c_bit_length;
1777 }
1778
1779 /* Initialise d and a. */
1780 d = c >> (c_bit_length - 5);
1781 b = _PyLong_Rshift(n, 2U*c - 62U);
1782 if (b == NULL) {
1783 goto error;
1784 }
1785 m = (uint64_t)PyLong_AsUnsignedLongLong(b);
1786 Py_DECREF(b);
1787 if (m == (uint64_t)(-1) && PyErr_Occurred()) {
1788 goto error;
1789 }
1790 u = _approximate_isqrt(m) >> (31U - d);
1791 a = PyLong_FromUnsignedLongLong((unsigned long long)u);
Mark Dickinson73934b92019-05-18 12:29:50 +01001792 if (a == NULL) {
1793 goto error;
1794 }
Mark Dickinson5c08ce92019-05-19 17:51:56 +01001795
1796 for (int s = c_bit_length - 6; s >= 0; --s) {
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03001797 PyObject *q;
Mark Dickinson73934b92019-05-18 12:29:50 +01001798 size_t e = d;
1799
1800 d = c >> s;
1801
1802 /* q = (n >> 2*c - e - d + 1) // a */
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03001803 q = _PyLong_Rshift(n, 2U*c - d - e + 1U);
Mark Dickinson73934b92019-05-18 12:29:50 +01001804 if (q == NULL) {
1805 goto error;
1806 }
1807 Py_SETREF(q, PyNumber_FloorDivide(q, a));
1808 if (q == NULL) {
1809 goto error;
1810 }
1811
1812 /* a = (a << d - 1 - e) + q */
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03001813 Py_SETREF(a, _PyLong_Lshift(a, d - 1U - e));
Mark Dickinson73934b92019-05-18 12:29:50 +01001814 if (a == NULL) {
1815 Py_DECREF(q);
1816 goto error;
1817 }
1818 Py_SETREF(a, PyNumber_Add(a, q));
1819 Py_DECREF(q);
1820 if (a == NULL) {
1821 goto error;
1822 }
1823 }
1824
1825 /* The correct result is either a or a - 1. Figure out which, and
1826 decrement a if necessary. */
1827
1828 /* a_too_large = n < a * a */
1829 b = PyNumber_Multiply(a, a);
1830 if (b == NULL) {
1831 goto error;
1832 }
1833 a_too_large = PyObject_RichCompareBool(n, b, Py_LT);
1834 Py_DECREF(b);
1835 if (a_too_large == -1) {
1836 goto error;
1837 }
1838
1839 if (a_too_large) {
1840 Py_SETREF(a, PyNumber_Subtract(a, _PyLong_One));
1841 }
1842 Py_DECREF(n);
1843 return a;
1844
1845 error:
1846 Py_XDECREF(a);
1847 Py_DECREF(n);
1848 return NULL;
1849}
1850
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00001851/* Divide-and-conquer factorial algorithm
1852 *
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001853 * Based on the formula and pseudo-code provided at:
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00001854 * http://www.luschny.de/math/factorial/binarysplitfact.html
1855 *
1856 * Faster algorithms exist, but they're more complicated and depend on
Ezio Melotti9527afd2010-07-08 15:03:02 +00001857 * a fast prime factorization algorithm.
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00001858 *
1859 * Notes on the algorithm
1860 * ----------------------
1861 *
1862 * factorial(n) is written in the form 2**k * m, with m odd. k and m are
1863 * computed separately, and then combined using a left shift.
1864 *
1865 * The function factorial_odd_part computes the odd part m (i.e., the greatest
1866 * odd divisor) of factorial(n), using the formula:
1867 *
1868 * factorial_odd_part(n) =
1869 *
1870 * product_{i >= 0} product_{0 < j <= n / 2**i, j odd} j
1871 *
1872 * Example: factorial_odd_part(20) =
1873 *
1874 * (1) *
1875 * (1) *
1876 * (1 * 3 * 5) *
1877 * (1 * 3 * 5 * 7 * 9)
1878 * (1 * 3 * 5 * 7 * 9 * 11 * 13 * 15 * 17 * 19)
1879 *
1880 * Here i goes from large to small: the first term corresponds to i=4 (any
1881 * larger i gives an empty product), and the last term corresponds to i=0.
1882 * Each term can be computed from the last by multiplying by the extra odd
1883 * numbers required: e.g., to get from the penultimate term to the last one,
1884 * we multiply by (11 * 13 * 15 * 17 * 19).
1885 *
1886 * To see a hint of why this formula works, here are the same numbers as above
1887 * but with the even parts (i.e., the appropriate powers of 2) included. For
1888 * each subterm in the product for i, we multiply that subterm by 2**i:
1889 *
1890 * factorial(20) =
1891 *
1892 * (16) *
1893 * (8) *
1894 * (4 * 12 * 20) *
1895 * (2 * 6 * 10 * 14 * 18) *
1896 * (1 * 3 * 5 * 7 * 9 * 11 * 13 * 15 * 17 * 19)
1897 *
1898 * The factorial_partial_product function computes the product of all odd j in
1899 * range(start, stop) for given start and stop. It's used to compute the
1900 * partial products like (11 * 13 * 15 * 17 * 19) in the example above. It
1901 * operates recursively, repeatedly splitting the range into two roughly equal
1902 * pieces until the subranges are small enough to be computed using only C
1903 * integer arithmetic.
1904 *
1905 * The two-valuation k (i.e., the exponent of the largest power of 2 dividing
1906 * the factorial) is computed independently in the main math_factorial
1907 * function. By standard results, its value is:
1908 *
1909 * two_valuation = n//2 + n//4 + n//8 + ....
1910 *
1911 * It can be shown (e.g., by complete induction on n) that two_valuation is
1912 * equal to n - count_set_bits(n), where count_set_bits(n) gives the number of
1913 * '1'-bits in the binary expansion of n.
1914 */
1915
1916/* factorial_partial_product: Compute product(range(start, stop, 2)) using
1917 * divide and conquer. Assumes start and stop are odd and stop > start.
1918 * max_bits must be >= bit_length(stop - 2). */
1919
1920static PyObject *
1921factorial_partial_product(unsigned long start, unsigned long stop,
1922 unsigned long max_bits)
1923{
1924 unsigned long midpoint, num_operands;
1925 PyObject *left = NULL, *right = NULL, *result = NULL;
1926
1927 /* If the return value will fit an unsigned long, then we can
1928 * multiply in a tight, fast loop where each multiply is O(1).
1929 * Compute an upper bound on the number of bits required to store
1930 * the answer.
1931 *
1932 * Storing some integer z requires floor(lg(z))+1 bits, which is
1933 * conveniently the value returned by bit_length(z). The
1934 * product x*y will require at most
1935 * bit_length(x) + bit_length(y) bits to store, based
1936 * on the idea that lg product = lg x + lg y.
1937 *
1938 * We know that stop - 2 is the largest number to be multiplied. From
1939 * there, we have: bit_length(answer) <= num_operands *
1940 * bit_length(stop - 2)
1941 */
1942
1943 num_operands = (stop - start) / 2;
1944 /* The "num_operands <= 8 * SIZEOF_LONG" check guards against the
1945 * unlikely case of an overflow in num_operands * max_bits. */
1946 if (num_operands <= 8 * SIZEOF_LONG &&
1947 num_operands * max_bits <= 8 * SIZEOF_LONG) {
1948 unsigned long j, total;
1949 for (total = start, j = start + 2; j < stop; j += 2)
1950 total *= j;
1951 return PyLong_FromUnsignedLong(total);
1952 }
1953
1954 /* find midpoint of range(start, stop), rounded up to next odd number. */
1955 midpoint = (start + num_operands) | 1;
1956 left = factorial_partial_product(start, midpoint,
Niklas Fiekasc5b79002020-01-16 15:09:19 +01001957 _Py_bit_length(midpoint - 2));
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00001958 if (left == NULL)
1959 goto error;
1960 right = factorial_partial_product(midpoint, stop, max_bits);
1961 if (right == NULL)
1962 goto error;
1963 result = PyNumber_Multiply(left, right);
1964
1965 error:
1966 Py_XDECREF(left);
1967 Py_XDECREF(right);
1968 return result;
1969}
1970
1971/* factorial_odd_part: compute the odd part of factorial(n). */
1972
1973static PyObject *
1974factorial_odd_part(unsigned long n)
1975{
1976 long i;
1977 unsigned long v, lower, upper;
1978 PyObject *partial, *tmp, *inner, *outer;
1979
1980 inner = PyLong_FromLong(1);
1981 if (inner == NULL)
1982 return NULL;
1983 outer = inner;
1984 Py_INCREF(outer);
1985
1986 upper = 3;
Niklas Fiekasc5b79002020-01-16 15:09:19 +01001987 for (i = _Py_bit_length(n) - 2; i >= 0; i--) {
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00001988 v = n >> i;
1989 if (v <= 2)
1990 continue;
1991 lower = upper;
1992 /* (v + 1) | 1 = least odd integer strictly larger than n / 2**i */
1993 upper = (v + 1) | 1;
1994 /* Here inner is the product of all odd integers j in the range (0,
1995 n/2**(i+1)]. The factorial_partial_product call below gives the
1996 product of all odd integers j in the range (n/2**(i+1), n/2**i]. */
Niklas Fiekasc5b79002020-01-16 15:09:19 +01001997 partial = factorial_partial_product(lower, upper, _Py_bit_length(upper-2));
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00001998 /* inner *= partial */
1999 if (partial == NULL)
2000 goto error;
2001 tmp = PyNumber_Multiply(inner, partial);
2002 Py_DECREF(partial);
2003 if (tmp == NULL)
2004 goto error;
2005 Py_DECREF(inner);
2006 inner = tmp;
2007 /* Now inner is the product of all odd integers j in the range (0,
2008 n/2**i], giving the inner product in the formula above. */
2009
2010 /* outer *= inner; */
2011 tmp = PyNumber_Multiply(outer, inner);
2012 if (tmp == NULL)
2013 goto error;
2014 Py_DECREF(outer);
2015 outer = tmp;
2016 }
Mark Dickinson76464492012-10-25 10:46:28 +01002017 Py_DECREF(inner);
2018 return outer;
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00002019
2020 error:
2021 Py_DECREF(outer);
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00002022 Py_DECREF(inner);
Mark Dickinson76464492012-10-25 10:46:28 +01002023 return NULL;
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00002024}
2025
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002026
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00002027/* Lookup table for small factorial values */
2028
2029static const unsigned long SmallFactorials[] = {
2030 1, 1, 2, 6, 24, 120, 720, 5040, 40320,
2031 362880, 3628800, 39916800, 479001600,
2032#if SIZEOF_LONG >= 8
2033 6227020800, 87178291200, 1307674368000,
2034 20922789888000, 355687428096000, 6402373705728000,
2035 121645100408832000, 2432902008176640000
2036#endif
2037};
2038
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002039/*[clinic input]
2040math.factorial
2041
2042 x as arg: object
2043 /
2044
2045Find x!.
2046
2047Raise a ValueError if x is negative or non-integral.
2048[clinic start generated code]*/
2049
Barry Warsaw8b43b191996-12-09 22:32:36 +00002050static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002051math_factorial(PyObject *module, PyObject *arg)
2052/*[clinic end generated code: output=6686f26fae00e9ca input=6d1c8105c0d91fb4]*/
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002053{
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03002054 long x, two_valuation;
Mark Dickinson5990d282014-04-10 09:29:39 -04002055 int overflow;
Serhiy Storchaka578c3952020-05-26 18:43:38 +03002056 PyObject *result, *odd_part;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002057
Serhiy Storchaka578c3952020-05-26 18:43:38 +03002058 x = PyLong_AsLongAndOverflow(arg, &overflow);
Mark Dickinson5990d282014-04-10 09:29:39 -04002059 if (x == -1 && PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 return NULL;
Mark Dickinson5990d282014-04-10 09:29:39 -04002061 }
2062 else if (overflow == 1) {
2063 PyErr_Format(PyExc_OverflowError,
2064 "factorial() argument should not exceed %ld",
2065 LONG_MAX);
2066 return NULL;
2067 }
2068 else if (overflow == -1 || x < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 PyErr_SetString(PyExc_ValueError,
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00002070 "factorial() not defined for negative values");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 return NULL;
2072 }
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002073
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00002074 /* use lookup table if x is small */
Victor Stinner63941882011-09-29 00:42:28 +02002075 if (x < (long)Py_ARRAY_LENGTH(SmallFactorials))
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00002076 return PyLong_FromUnsignedLong(SmallFactorials[x]);
2077
2078 /* else express in the form odd_part * 2**two_valuation, and compute as
2079 odd_part << two_valuation. */
2080 odd_part = factorial_odd_part(x);
2081 if (odd_part == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 return NULL;
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03002083 two_valuation = x - count_set_bits(x);
2084 result = _PyLong_Lshift(odd_part, two_valuation);
Mark Dickinson4c8a9a22010-05-15 17:02:38 +00002085 Py_DECREF(odd_part);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002087}
2088
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002089
2090/*[clinic input]
2091math.trunc
2092
2093 x: object
2094 /
2095
2096Truncates the Real x to the nearest Integral toward 0.
2097
2098Uses the __trunc__ magic method.
2099[clinic start generated code]*/
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002100
2101static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002102math_trunc(PyObject *module, PyObject *x)
2103/*[clinic end generated code: output=34b9697b707e1031 input=2168b34e0a09134d]*/
Christian Heimes400adb02008-02-01 08:12:03 +00002104{
Benjamin Petersonce798522012-01-22 11:24:29 -05002105 _Py_IDENTIFIER(__trunc__);
Benjamin Petersonb0125892010-07-02 13:35:17 +00002106 PyObject *trunc, *result;
Christian Heimes400adb02008-02-01 08:12:03 +00002107
Serhiy Storchaka5fd5cb82019-11-16 18:00:57 +02002108 if (PyFloat_CheckExact(x)) {
2109 return PyFloat_Type.tp_as_number->nb_int(x);
2110 }
2111
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002112 if (Py_TYPE(x)->tp_dict == NULL) {
2113 if (PyType_Ready(Py_TYPE(x)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 return NULL;
2115 }
Christian Heimes400adb02008-02-01 08:12:03 +00002116
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002117 trunc = _PyObject_LookupSpecial(x, &PyId___trunc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 if (trunc == NULL) {
Benjamin Peterson8bb9cde2010-07-01 15:16:55 +00002119 if (!PyErr_Occurred())
2120 PyErr_Format(PyExc_TypeError,
2121 "type %.100s doesn't define __trunc__ method",
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002122 Py_TYPE(x)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 return NULL;
2124 }
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002125 result = _PyObject_CallNoArg(trunc);
Benjamin Petersonb0125892010-07-02 13:35:17 +00002126 Py_DECREF(trunc);
2127 return result;
Christian Heimes400adb02008-02-01 08:12:03 +00002128}
2129
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002130
2131/*[clinic input]
2132math.frexp
2133
2134 x: double
2135 /
2136
2137Return the mantissa and exponent of x, as pair (m, e).
2138
2139m is a float and e is an int, such that x = m * 2.**e.
2140If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
2141[clinic start generated code]*/
Christian Heimes400adb02008-02-01 08:12:03 +00002142
2143static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002144math_frexp_impl(PyObject *module, double x)
2145/*[clinic end generated code: output=03e30d252a15ad4a input=96251c9e208bc6e9]*/
Guido van Rossumd18ad581991-10-24 14:57:21 +00002146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 /* deal with special cases directly, to sidestep platform
2149 differences */
2150 if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) {
2151 i = 0;
2152 }
2153 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 x = frexp(x, &i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 }
2156 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +00002157}
2158
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002159
2160/*[clinic input]
2161math.ldexp
2162
2163 x: double
2164 i: object
2165 /
2166
2167Return x * (2**i).
2168
2169This is essentially the inverse of frexp().
2170[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +00002171
Barry Warsaw8b43b191996-12-09 22:32:36 +00002172static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002173math_ldexp_impl(PyObject *module, double x, PyObject *i)
2174/*[clinic end generated code: output=b6892f3c2df9cc6a input=17d5970c1a40a8c1]*/
Guido van Rossumd18ad581991-10-24 14:57:21 +00002175{
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002176 double r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 long exp;
2178 int overflow;
Alexandre Vassalotti6461e102008-05-15 22:09:29 +00002179
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002180 if (PyLong_Check(i)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 /* on overflow, replace exponent with either LONG_MAX
2182 or LONG_MIN, depending on the sign. */
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002183 exp = PyLong_AsLongAndOverflow(i, &overflow);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 if (exp == -1 && PyErr_Occurred())
2185 return NULL;
2186 if (overflow)
2187 exp = overflow < 0 ? LONG_MIN : LONG_MAX;
2188 }
2189 else {
2190 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002191 "Expected an int as second argument to ldexp.");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 return NULL;
2193 }
Alexandre Vassalotti6461e102008-05-15 22:09:29 +00002194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 if (x == 0. || !Py_IS_FINITE(x)) {
2196 /* NaNs, zeros and infinities are returned unchanged */
2197 r = x;
2198 errno = 0;
2199 } else if (exp > INT_MAX) {
2200 /* overflow */
2201 r = copysign(Py_HUGE_VAL, x);
2202 errno = ERANGE;
2203 } else if (exp < INT_MIN) {
2204 /* underflow to +-0 */
2205 r = copysign(0., x);
2206 errno = 0;
2207 } else {
2208 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 r = ldexp(x, (int)exp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 if (Py_IS_INFINITY(r))
2211 errno = ERANGE;
2212 }
Alexandre Vassalotti6461e102008-05-15 22:09:29 +00002213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 if (errno && is_error(r))
2215 return NULL;
2216 return PyFloat_FromDouble(r);
Guido van Rossumd18ad581991-10-24 14:57:21 +00002217}
2218
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002219
2220/*[clinic input]
2221math.modf
2222
2223 x: double
2224 /
2225
2226Return the fractional and integer parts of x.
2227
2228Both results carry the sign of x and are floats.
2229[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +00002230
Barry Warsaw8b43b191996-12-09 22:32:36 +00002231static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002232math_modf_impl(PyObject *module, double x)
2233/*[clinic end generated code: output=90cee0260014c3c0 input=b4cfb6786afd9035]*/
Guido van Rossumd18ad581991-10-24 14:57:21 +00002234{
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002235 double y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 /* some platforms don't do the right thing for NaNs and
2237 infinities, so we take care of special cases directly. */
2238 if (!Py_IS_FINITE(x)) {
2239 if (Py_IS_INFINITY(x))
2240 return Py_BuildValue("(dd)", copysign(0., x), x);
2241 else if (Py_IS_NAN(x))
2242 return Py_BuildValue("(dd)", x, x);
2243 }
Christian Heimesa342c012008-04-20 21:01:16 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 x = modf(x, &y);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +00002248}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002249
Guido van Rossumc6e22901998-12-04 19:26:43 +00002250
Serhiy Storchaka95949422013-08-27 19:40:23 +03002251/* A decent logarithm is easy to compute even for huge ints, but libm can't
Tim Peters78526162001-09-05 00:53:45 +00002252 do that by itself -- loghelper can. func is log or log10, and name is
Serhiy Storchaka95949422013-08-27 19:40:23 +03002253 "log" or "log10". Note that overflow of the result isn't possible: an int
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002254 can contain no more than INT_MAX * SHIFT bits, so has value certainly less
2255 than 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
Tim Peters78526162001-09-05 00:53:45 +00002256 small enough to fit in an IEEE single. log and log10 are even smaller.
Serhiy Storchaka95949422013-08-27 19:40:23 +03002257 However, intermediate overflow is possible for an int if the number of bits
2258 in that int is larger than PY_SSIZE_T_MAX. */
Tim Peters78526162001-09-05 00:53:45 +00002259
2260static PyObject*
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002261loghelper(PyObject* arg, double (*func)(double), const char *funcname)
Tim Peters78526162001-09-05 00:53:45 +00002262{
Serhiy Storchaka95949422013-08-27 19:40:23 +03002263 /* If it is int, do it ourselves. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 if (PyLong_Check(arg)) {
Mark Dickinsonc6037172010-09-29 19:06:36 +00002265 double x, result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 Py_ssize_t e;
Mark Dickinsonc6037172010-09-29 19:06:36 +00002267
2268 /* Negative or zero inputs give a ValueError. */
2269 if (Py_SIZE(arg) <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 PyErr_SetString(PyExc_ValueError,
2271 "math domain error");
2272 return NULL;
2273 }
Mark Dickinsonfa41e602010-09-28 07:22:27 +00002274
Mark Dickinsonc6037172010-09-29 19:06:36 +00002275 x = PyLong_AsDouble(arg);
2276 if (x == -1.0 && PyErr_Occurred()) {
2277 if (!PyErr_ExceptionMatches(PyExc_OverflowError))
2278 return NULL;
2279 /* Here the conversion to double overflowed, but it's possible
2280 to compute the log anyway. Clear the exception and continue. */
2281 PyErr_Clear();
2282 x = _PyLong_Frexp((PyLongObject *)arg, &e);
2283 if (x == -1.0 && PyErr_Occurred())
2284 return NULL;
2285 /* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */
2286 result = func(x) + func(2.0) * e;
2287 }
2288 else
2289 /* Successfully converted x to a double. */
2290 result = func(x);
2291 return PyFloat_FromDouble(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 }
Tim Peters78526162001-09-05 00:53:45 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 /* Else let libm handle it by itself. */
2295 return math_1(arg, func, 0);
Tim Peters78526162001-09-05 00:53:45 +00002296}
2297
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002298
2299/*[clinic input]
2300math.log
2301
2302 x: object
2303 [
2304 base: object(c_default="NULL") = math.e
2305 ]
2306 /
2307
2308Return the logarithm of x to the given base.
2309
2310If the base not specified, returns the natural logarithm (base e) of x.
2311[clinic start generated code]*/
2312
Tim Peters78526162001-09-05 00:53:45 +00002313static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002314math_log_impl(PyObject *module, PyObject *x, int group_right_1,
2315 PyObject *base)
2316/*[clinic end generated code: output=7b5a39e526b73fc9 input=0f62d5726cbfebbd]*/
Tim Peters78526162001-09-05 00:53:45 +00002317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 PyObject *num, *den;
2319 PyObject *ans;
Raymond Hettinger866964c2002-12-14 19:51:34 +00002320
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002321 num = loghelper(x, m_log, "log");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 if (num == NULL || base == NULL)
2323 return num;
Raymond Hettinger866964c2002-12-14 19:51:34 +00002324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 den = loghelper(base, m_log, "log");
2326 if (den == NULL) {
2327 Py_DECREF(num);
2328 return NULL;
2329 }
Raymond Hettinger866964c2002-12-14 19:51:34 +00002330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 ans = PyNumber_TrueDivide(num, den);
2332 Py_DECREF(num);
2333 Py_DECREF(den);
2334 return ans;
Tim Peters78526162001-09-05 00:53:45 +00002335}
2336
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002337
2338/*[clinic input]
2339math.log2
2340
2341 x: object
2342 /
2343
2344Return the base 2 logarithm of x.
2345[clinic start generated code]*/
Tim Peters78526162001-09-05 00:53:45 +00002346
2347static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002348math_log2(PyObject *module, PyObject *x)
2349/*[clinic end generated code: output=5425899a4d5d6acb input=08321262bae4f39b]*/
Victor Stinnerfa0e3d52011-05-09 01:01:09 +02002350{
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002351 return loghelper(x, m_log2, "log2");
Victor Stinnerfa0e3d52011-05-09 01:01:09 +02002352}
2353
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002354
2355/*[clinic input]
2356math.log10
2357
2358 x: object
2359 /
2360
2361Return the base 10 logarithm of x.
2362[clinic start generated code]*/
Victor Stinnerfa0e3d52011-05-09 01:01:09 +02002363
2364static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002365math_log10(PyObject *module, PyObject *x)
2366/*[clinic end generated code: output=be72a64617df9c6f input=b2469d02c6469e53]*/
Tim Peters78526162001-09-05 00:53:45 +00002367{
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002368 return loghelper(x, m_log10, "log10");
Tim Peters78526162001-09-05 00:53:45 +00002369}
2370
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002371
2372/*[clinic input]
2373math.fmod
2374
2375 x: double
2376 y: double
2377 /
2378
2379Return fmod(x, y), according to platform C.
2380
2381x % y may differ.
2382[clinic start generated code]*/
Tim Peters78526162001-09-05 00:53:45 +00002383
Christian Heimes53876d92008-04-19 00:31:39 +00002384static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002385math_fmod_impl(PyObject *module, double x, double y)
2386/*[clinic end generated code: output=7559d794343a27b5 input=4f84caa8cfc26a03]*/
Christian Heimes53876d92008-04-19 00:31:39 +00002387{
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002388 double r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 /* fmod(x, +/-Inf) returns x for finite x. */
2390 if (Py_IS_INFINITY(y) && Py_IS_FINITE(x))
2391 return PyFloat_FromDouble(x);
2392 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 r = fmod(x, y);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 if (Py_IS_NAN(r)) {
2395 if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
2396 errno = EDOM;
2397 else
2398 errno = 0;
2399 }
2400 if (errno && is_error(r))
2401 return NULL;
2402 else
2403 return PyFloat_FromDouble(r);
Christian Heimes53876d92008-04-19 00:31:39 +00002404}
2405
Raymond Hettinger13990742018-08-11 11:26:36 -07002406/*
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002407Given a *vec* of values, compute the vector norm:
Raymond Hettinger13990742018-08-11 11:26:36 -07002408
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002409 sqrt(sum(x ** 2 for x in vec))
Raymond Hettingerfff3c282020-08-15 19:38:19 -07002410
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002411The *max* variable should be equal to the largest fabs(x).
2412The *n* variable is the length of *vec*.
2413If n==0, then *max* should be 0.0.
Raymond Hettinger745c0f32018-08-31 11:22:13 -07002414If an infinity is present in the vec, *max* should be INF.
Raymond Hettingerc630e102018-08-11 18:39:05 -07002415The *found_nan* variable indicates whether some member of
2416the *vec* is a NaN.
Raymond Hettinger21786f52018-08-28 22:47:24 -07002417
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002418To avoid overflow/underflow and to achieve high accuracy giving results
2419that are almost always correctly rounded, four techniques are used:
Raymond Hettinger21786f52018-08-28 22:47:24 -07002420
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002421* lossless scaling using a power-of-two scaling factor
Raymond Hettinger67c998d2020-09-06 15:10:07 -07002422* accurate squaring using Veltkamp-Dekker splitting [1]
2423* compensated summation using a variant of the Neumaier algorithm [2]
2424* differential correction of the square root [3]
Raymond Hettinger745c0f32018-08-31 11:22:13 -07002425
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002426The usual presentation of the Neumaier summation algorithm has an
2427expensive branch depending on which operand has the larger
2428magnitude. We avoid this cost by arranging the calculation so that
2429fabs(csum) is always as large as fabs(x).
Raymond Hettinger21786f52018-08-28 22:47:24 -07002430
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002431To establish the invariant, *csum* is initialized to 1.0 which is
Raymond Hettinger457d4e92020-09-13 23:33:41 -07002432always larger than x**2 after scaling or after division by *max*.
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002433After the loop is finished, the initial 1.0 is subtracted out for a
2434net zero effect on the final sum. Since *csum* will be greater than
24351.0, the subtraction of 1.0 will not cause fractional digits to be
2436dropped from *csum*.
2437
2438To get the full benefit from compensated summation, the largest
2439addend should be in the range: 0.5 <= |x| <= 1.0. Accordingly,
2440scaling or division by *max* should not be skipped even if not
2441otherwise needed to prevent overflow or loss of precision.
2442
Raymond Hettinger82e79482020-08-26 13:09:40 -07002443The assertion that hi*hi <= 1.0 is a bit subtle. Each vector element
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002444gets scaled to a magnitude below 1.0. The Veltkamp-Dekker splitting
2445algorithm gives a *hi* value that is correctly rounded to half
2446precision. When a value at or below 1.0 is correctly rounded, it
2447never goes above 1.0. And when values at or below 1.0 are squared,
2448they remain at or below 1.0, thus preserving the summation invariant.
2449
Raymond Hettinger27de2862020-08-29 09:11:04 -07002450Another interesting assertion is that csum+lo*lo == csum. In the loop,
2451each scaled vector element has a magnitude less than 1.0. After the
2452Veltkamp split, *lo* has a maximum value of 2**-27. So the maximum
2453value of *lo* squared is 2**-54. The value of ulp(1.0)/2.0 is 2**-53.
2454Given that csum >= 1.0, we have:
2455 lo**2 <= 2**-54 < 2**-53 == 1/2*ulp(1.0) <= ulp(csum)/2
2456Since lo**2 is less than 1/2 ulp(csum), we have csum+lo*lo == csum.
2457
Raymond Hettinger92c38162020-08-30 10:00:11 -07002458To minimize loss of information during the accumulation of fractional
Raymond Hettinger67c998d2020-09-06 15:10:07 -07002459values, each term has a separate accumulator. This also breaks up
2460sequential dependencies in the inner loop so the CPU can maximize
Raymond Hettinger457d4e92020-09-13 23:33:41 -07002461floating point throughput. [4] On a 2.6 GHz Haswell, adding one
Raymond Hettinger67c998d2020-09-06 15:10:07 -07002462dimension has an incremental cost of only 5ns -- for example when
2463moving from hypot(x,y) to hypot(x,y,z).
Raymond Hettinger92c38162020-08-30 10:00:11 -07002464
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002465The square root differential correction is needed because a
2466correctly rounded square root of a correctly rounded sum of
2467squares can still be off by as much as one ulp.
2468
2469The differential correction starts with a value *x* that is
2470the difference between the square of *h*, the possibly inaccurately
2471rounded square root, and the accurately computed sum of squares.
2472The correction is the first order term of the Maclaurin series
Raymond Hettinger457d4e92020-09-13 23:33:41 -07002473expansion of sqrt(h**2 + x) == h + x/(2*h) + O(x**2). [5]
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002474
2475Essentially, this differential correction is equivalent to one
Raymond Hettinger82e79482020-08-26 13:09:40 -07002476refinement step in Newton's divide-and-average square root
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002477algorithm, effectively doubling the number of accurate bits.
2478This technique is used in Dekker's SQRT2 algorithm and again in
2479Borges' ALGORITHM 4 and 5.
2480
Raymond Hettinger67c998d2020-09-06 15:10:07 -07002481Without proof for all cases, hypot() cannot claim to be always
2482correctly rounded. However for n <= 1000, prior to the final addition
2483that rounds the overall result, the internal accuracy of "h" together
2484with its correction of "x / (2.0 * h)" is at least 100 bits. [6]
2485Also, hypot() was tested against a Decimal implementation with
2486prec=300. After 100 million trials, no incorrectly rounded examples
2487were found. In addition, perfect commutativity (all permutations are
2488exactly equal) was verified for 1 billion random inputs with n=5. [7]
2489
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002490References:
2491
24921. Veltkamp-Dekker splitting: http://csclub.uwaterloo.ca/~pbarfuss/dekker1971.pdf
24932. Compensated summation: http://www.ti3.tu-harburg.de/paper/rump/Ru08b.pdf
Raymond Hettinger92c38162020-08-30 10:00:11 -070024943. Square root differential correction: https://arxiv.org/pdf/1904.09481.pdf
Raymond Hettinger457d4e92020-09-13 23:33:41 -070024954. Data dependency graph: https://bugs.python.org/file49439/hypot.png
24965. https://www.wolframalpha.com/input/?i=Maclaurin+series+sqrt%28h**2+%2B+x%29+at+x%3D0
Raymond Hettinger497126f2020-10-01 19:30:54 -070024976. Analysis of internal accuracy: https://bugs.python.org/file49484/best_frac.py
Raymond Hettinger457d4e92020-09-13 23:33:41 -070024987. Commutativity test: https://bugs.python.org/file49448/test_hypot_commutativity.py
Raymond Hettingerfff3c282020-08-15 19:38:19 -07002499
Raymond Hettinger13990742018-08-11 11:26:36 -07002500*/
2501
2502static inline double
Raymond Hettingerc630e102018-08-11 18:39:05 -07002503vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
Raymond Hettinger13990742018-08-11 11:26:36 -07002504{
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002505 const double T27 = 134217729.0; /* ldexp(1.0, 27) + 1.0) */
2506 double x, scale, oldcsum, csum = 1.0, frac1 = 0.0, frac2 = 0.0, frac3 = 0.0;
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002507 double t, hi, lo, h;
Raymond Hettingerfff3c282020-08-15 19:38:19 -07002508 int max_e;
Raymond Hettinger13990742018-08-11 11:26:36 -07002509 Py_ssize_t i;
2510
Raymond Hettingerc630e102018-08-11 18:39:05 -07002511 if (Py_IS_INFINITY(max)) {
2512 return max;
2513 }
2514 if (found_nan) {
2515 return Py_NAN;
2516 }
Raymond Hettingerf3267142018-09-02 13:34:21 -07002517 if (max == 0.0 || n <= 1) {
Raymond Hettinger745c0f32018-08-31 11:22:13 -07002518 return max;
Raymond Hettinger13990742018-08-11 11:26:36 -07002519 }
Raymond Hettingerfff3c282020-08-15 19:38:19 -07002520 frexp(max, &max_e);
2521 if (max_e >= -1023) {
2522 scale = ldexp(1.0, -max_e);
2523 assert(max * scale >= 0.5);
2524 assert(max * scale < 1.0);
2525 for (i=0 ; i < n ; i++) {
2526 x = vec[i];
2527 assert(Py_IS_FINITE(x) && fabs(x) <= max);
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002528
Raymond Hettingerfff3c282020-08-15 19:38:19 -07002529 x *= scale;
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002530 assert(fabs(x) < 1.0);
2531
2532 t = x * T27;
2533 hi = t - (t - x);
2534 lo = x - hi;
2535 assert(hi + lo == x);
2536
2537 x = hi * hi;
Raymond Hettingerfff3c282020-08-15 19:38:19 -07002538 assert(x <= 1.0);
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002539 assert(fabs(csum) >= fabs(x));
2540 oldcsum = csum;
2541 csum += x;
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002542 frac1 += (oldcsum - csum) + x;
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002543
2544 x = 2.0 * hi * lo;
2545 assert(fabs(csum) >= fabs(x));
2546 oldcsum = csum;
2547 csum += x;
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002548 frac2 += (oldcsum - csum) + x;
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002549
Raymond Hettinger27de2862020-08-29 09:11:04 -07002550 assert(csum + lo * lo == csum);
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002551 frac3 += lo * lo;
Raymond Hettingerfff3c282020-08-15 19:38:19 -07002552 }
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002553 h = sqrt(csum - 1.0 + (frac1 + frac2 + frac3));
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002554
2555 x = h;
2556 t = x * T27;
2557 hi = t - (t - x);
2558 lo = x - hi;
2559 assert (hi + lo == x);
2560
2561 x = -hi * hi;
2562 assert(fabs(csum) >= fabs(x));
2563 oldcsum = csum;
2564 csum += x;
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002565 frac1 += (oldcsum - csum) + x;
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002566
2567 x = -2.0 * hi * lo;
2568 assert(fabs(csum) >= fabs(x));
2569 oldcsum = csum;
2570 csum += x;
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002571 frac2 += (oldcsum - csum) + x;
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002572
2573 x = -lo * lo;
2574 assert(fabs(csum) >= fabs(x));
2575 oldcsum = csum;
2576 csum += x;
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002577 frac3 += (oldcsum - csum) + x;
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002578
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002579 x = csum - 1.0 + (frac1 + frac2 + frac3);
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002580 return (h + x / (2.0 * h)) / scale;
Raymond Hettingerfff3c282020-08-15 19:38:19 -07002581 }
2582 /* When max_e < -1023, ldexp(1.0, -max_e) overflows.
2583 So instead of multiplying by a scale, we just divide by *max*.
2584 */
Raymond Hettinger745c0f32018-08-31 11:22:13 -07002585 for (i=0 ; i < n ; i++) {
Raymond Hettinger13990742018-08-11 11:26:36 -07002586 x = vec[i];
Raymond Hettinger745c0f32018-08-31 11:22:13 -07002587 assert(Py_IS_FINITE(x) && fabs(x) <= max);
Raymond Hettinger13990742018-08-11 11:26:36 -07002588 x /= max;
Raymond Hettinger21786f52018-08-28 22:47:24 -07002589 x = x*x;
Raymond Hettingerfff3c282020-08-15 19:38:19 -07002590 assert(x <= 1.0);
Raymond Hettinger8e19c8b2020-08-24 17:40:08 -07002591 assert(fabs(csum) >= fabs(x));
Raymond Hettinger13990742018-08-11 11:26:36 -07002592 oldcsum = csum;
2593 csum += x;
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002594 frac1 += (oldcsum - csum) + x;
Raymond Hettinger13990742018-08-11 11:26:36 -07002595 }
Raymond Hettinger438e9fc2020-09-22 20:01:12 -07002596 return max * sqrt(csum - 1.0 + frac1);
Raymond Hettinger13990742018-08-11 11:26:36 -07002597}
2598
Raymond Hettingerc630e102018-08-11 18:39:05 -07002599#define NUM_STACK_ELEMS 16
2600
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002601/*[clinic input]
2602math.dist
2603
Raymond Hettinger6b5f1b42019-07-27 14:04:29 -07002604 p: object
2605 q: object
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002606 /
2607
2608Return the Euclidean distance between two points p and q.
2609
Raymond Hettinger6b5f1b42019-07-27 14:04:29 -07002610The points should be specified as sequences (or iterables) of
2611coordinates. Both inputs must have the same dimension.
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002612
2613Roughly equivalent to:
2614 sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
2615[clinic start generated code]*/
2616
2617static PyObject *
2618math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
Raymond Hettinger6b5f1b42019-07-27 14:04:29 -07002619/*[clinic end generated code: output=56bd9538d06bbcfe input=74e85e1b6092e68e]*/
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002620{
2621 PyObject *item;
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002622 double max = 0.0;
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002623 double x, px, qx, result;
2624 Py_ssize_t i, m, n;
Raymond Hettinger6b5f1b42019-07-27 14:04:29 -07002625 int found_nan = 0, p_allocated = 0, q_allocated = 0;
Raymond Hettingerc630e102018-08-11 18:39:05 -07002626 double diffs_on_stack[NUM_STACK_ELEMS];
2627 double *diffs = diffs_on_stack;
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002628
Raymond Hettinger6b5f1b42019-07-27 14:04:29 -07002629 if (!PyTuple_Check(p)) {
2630 p = PySequence_Tuple(p);
2631 if (p == NULL) {
2632 return NULL;
2633 }
2634 p_allocated = 1;
2635 }
2636 if (!PyTuple_Check(q)) {
2637 q = PySequence_Tuple(q);
2638 if (q == NULL) {
2639 if (p_allocated) {
2640 Py_DECREF(p);
2641 }
2642 return NULL;
2643 }
2644 q_allocated = 1;
2645 }
2646
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002647 m = PyTuple_GET_SIZE(p);
2648 n = PyTuple_GET_SIZE(q);
2649 if (m != n) {
2650 PyErr_SetString(PyExc_ValueError,
2651 "both points must have the same number of dimensions");
2652 return NULL;
2653
2654 }
Raymond Hettingerc630e102018-08-11 18:39:05 -07002655 if (n > NUM_STACK_ELEMS) {
2656 diffs = (double *) PyObject_Malloc(n * sizeof(double));
2657 if (diffs == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07002658 return PyErr_NoMemory();
Raymond Hettingerc630e102018-08-11 18:39:05 -07002659 }
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002660 }
2661 for (i=0 ; i<n ; i++) {
2662 item = PyTuple_GET_ITEM(p, i);
Raymond Hettingercfd735e2019-01-29 20:39:53 -08002663 ASSIGN_DOUBLE(px, item, error_exit);
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002664 item = PyTuple_GET_ITEM(q, i);
Raymond Hettingercfd735e2019-01-29 20:39:53 -08002665 ASSIGN_DOUBLE(qx, item, error_exit);
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002666 x = fabs(px - qx);
2667 diffs[i] = x;
2668 found_nan |= Py_IS_NAN(x);
2669 if (x > max) {
2670 max = x;
2671 }
2672 }
Raymond Hettingerc630e102018-08-11 18:39:05 -07002673 result = vector_norm(n, diffs, max, found_nan);
2674 if (diffs != diffs_on_stack) {
2675 PyObject_Free(diffs);
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002676 }
Raymond Hettinger6b5f1b42019-07-27 14:04:29 -07002677 if (p_allocated) {
2678 Py_DECREF(p);
2679 }
2680 if (q_allocated) {
2681 Py_DECREF(q);
2682 }
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002683 return PyFloat_FromDouble(result);
Raymond Hettingerc630e102018-08-11 18:39:05 -07002684
2685 error_exit:
2686 if (diffs != diffs_on_stack) {
2687 PyObject_Free(diffs);
2688 }
Raymond Hettinger6b5f1b42019-07-27 14:04:29 -07002689 if (p_allocated) {
2690 Py_DECREF(p);
2691 }
2692 if (q_allocated) {
2693 Py_DECREF(q);
2694 }
Raymond Hettingerc630e102018-08-11 18:39:05 -07002695 return NULL;
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07002696}
2697
Raymond Hettingerc6dabe32018-07-28 07:48:04 -07002698/* AC: cannot convert yet, waiting for *args support */
Christian Heimes53876d92008-04-19 00:31:39 +00002699static PyObject *
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02002700math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Christian Heimes53876d92008-04-19 00:31:39 +00002701{
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02002702 Py_ssize_t i;
Raymond Hettingerc6dabe32018-07-28 07:48:04 -07002703 PyObject *item;
Raymond Hettingerc6dabe32018-07-28 07:48:04 -07002704 double max = 0.0;
Raymond Hettingerc6dabe32018-07-28 07:48:04 -07002705 double x, result;
2706 int found_nan = 0;
Raymond Hettingerc630e102018-08-11 18:39:05 -07002707 double coord_on_stack[NUM_STACK_ELEMS];
2708 double *coordinates = coord_on_stack;
Raymond Hettingerc6dabe32018-07-28 07:48:04 -07002709
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02002710 if (nargs > NUM_STACK_ELEMS) {
2711 coordinates = (double *) PyObject_Malloc(nargs * sizeof(double));
Zackery Spytz4c49da02018-12-07 03:11:30 -07002712 if (coordinates == NULL) {
2713 return PyErr_NoMemory();
2714 }
Raymond Hettingerc630e102018-08-11 18:39:05 -07002715 }
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02002716 for (i = 0; i < nargs; i++) {
2717 item = args[i];
Raymond Hettingercfd735e2019-01-29 20:39:53 -08002718 ASSIGN_DOUBLE(x, item, error_exit);
Raymond Hettingerc6dabe32018-07-28 07:48:04 -07002719 x = fabs(x);
2720 coordinates[i] = x;
2721 found_nan |= Py_IS_NAN(x);
2722 if (x > max) {
2723 max = x;
2724 }
2725 }
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02002726 result = vector_norm(nargs, coordinates, max, found_nan);
Raymond Hettingerc630e102018-08-11 18:39:05 -07002727 if (coordinates != coord_on_stack) {
2728 PyObject_Free(coordinates);
Raymond Hettingerc6dabe32018-07-28 07:48:04 -07002729 }
Raymond Hettingerc6dabe32018-07-28 07:48:04 -07002730 return PyFloat_FromDouble(result);
Raymond Hettingerc630e102018-08-11 18:39:05 -07002731
2732 error_exit:
2733 if (coordinates != coord_on_stack) {
2734 PyObject_Free(coordinates);
2735 }
2736 return NULL;
Christian Heimes53876d92008-04-19 00:31:39 +00002737}
2738
Raymond Hettingerc630e102018-08-11 18:39:05 -07002739#undef NUM_STACK_ELEMS
2740
Raymond Hettingerc6dabe32018-07-28 07:48:04 -07002741PyDoc_STRVAR(math_hypot_doc,
2742 "hypot(*coordinates) -> value\n\n\
2743Multidimensional Euclidean distance from the origin to a point.\n\
2744\n\
2745Roughly equivalent to:\n\
2746 sqrt(sum(x**2 for x in coordinates))\n\
2747\n\
2748For a two dimensional point (x, y), gives the hypotenuse\n\
2749using the Pythagorean theorem: sqrt(x*x + y*y).\n\
2750\n\
2751For example, the hypotenuse of a 3/4/5 right triangle is:\n\
2752\n\
2753 >>> hypot(3.0, 4.0)\n\
2754 5.0\n\
2755");
Christian Heimes53876d92008-04-19 00:31:39 +00002756
2757/* pow can't use math_2, but needs its own wrapper: the problem is
2758 that an infinite result can arise either as a result of overflow
2759 (in which case OverflowError should be raised) or as a result of
2760 e.g. 0.**-5. (for which ValueError needs to be raised.)
2761*/
2762
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002763/*[clinic input]
2764math.pow
Christian Heimes53876d92008-04-19 00:31:39 +00002765
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002766 x: double
2767 y: double
2768 /
2769
2770Return x**y (x to the power of y).
2771[clinic start generated code]*/
2772
2773static PyObject *
2774math_pow_impl(PyObject *module, double x, double y)
2775/*[clinic end generated code: output=fff93e65abccd6b0 input=c26f1f6075088bfd]*/
2776{
2777 double r;
2778 int odd_y;
Christian Heimesa342c012008-04-20 21:01:16 +00002779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 /* deal directly with IEEE specials, to cope with problems on various
2781 platforms whose semantics don't exactly match C99 */
2782 r = 0.; /* silence compiler warning */
2783 if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) {
2784 errno = 0;
2785 if (Py_IS_NAN(x))
2786 r = y == 0. ? 1. : x; /* NaN**0 = 1 */
2787 else if (Py_IS_NAN(y))
2788 r = x == 1. ? 1. : y; /* 1**NaN = 1 */
2789 else if (Py_IS_INFINITY(x)) {
2790 odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0;
2791 if (y > 0.)
2792 r = odd_y ? x : fabs(x);
2793 else if (y == 0.)
2794 r = 1.;
2795 else /* y < 0. */
2796 r = odd_y ? copysign(0., x) : 0.;
2797 }
2798 else if (Py_IS_INFINITY(y)) {
2799 if (fabs(x) == 1.0)
2800 r = 1.;
2801 else if (y > 0. && fabs(x) > 1.0)
2802 r = y;
2803 else if (y < 0. && fabs(x) < 1.0) {
2804 r = -y; /* result is +inf */
2805 if (x == 0.) /* 0**-inf: divide-by-zero */
2806 errno = EDOM;
2807 }
2808 else
2809 r = 0.;
2810 }
2811 }
2812 else {
2813 /* let libm handle finite**finite */
2814 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 r = pow(x, y);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 /* a NaN result should arise only from (-ve)**(finite
2817 non-integer); in this case we want to raise ValueError. */
2818 if (!Py_IS_FINITE(r)) {
2819 if (Py_IS_NAN(r)) {
2820 errno = EDOM;
2821 }
2822 /*
2823 an infinite result here arises either from:
2824 (A) (+/-0.)**negative (-> divide-by-zero)
2825 (B) overflow of x**y with x and y finite
2826 */
2827 else if (Py_IS_INFINITY(r)) {
2828 if (x == 0.)
2829 errno = EDOM;
2830 else
2831 errno = ERANGE;
2832 }
2833 }
2834 }
Christian Heimes53876d92008-04-19 00:31:39 +00002835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 if (errno && is_error(r))
2837 return NULL;
2838 else
2839 return PyFloat_FromDouble(r);
Christian Heimes53876d92008-04-19 00:31:39 +00002840}
2841
Christian Heimes53876d92008-04-19 00:31:39 +00002842
Christian Heimes072c0f12008-01-03 23:01:04 +00002843static const double degToRad = Py_MATH_PI / 180.0;
2844static const double radToDeg = 180.0 / Py_MATH_PI;
Raymond Hettingerd6f22672002-05-13 03:56:10 +00002845
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002846/*[clinic input]
2847math.degrees
2848
2849 x: double
2850 /
2851
2852Convert angle x from radians to degrees.
2853[clinic start generated code]*/
2854
Raymond Hettingerd6f22672002-05-13 03:56:10 +00002855static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002856math_degrees_impl(PyObject *module, double x)
2857/*[clinic end generated code: output=7fea78b294acd12f input=81e016555d6e3660]*/
Raymond Hettingerd6f22672002-05-13 03:56:10 +00002858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 return PyFloat_FromDouble(x * radToDeg);
Raymond Hettingerd6f22672002-05-13 03:56:10 +00002860}
2861
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002862
2863/*[clinic input]
2864math.radians
2865
2866 x: double
2867 /
2868
2869Convert angle x from degrees to radians.
2870[clinic start generated code]*/
Raymond Hettingerd6f22672002-05-13 03:56:10 +00002871
2872static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002873math_radians_impl(PyObject *module, double x)
2874/*[clinic end generated code: output=34daa47caf9b1590 input=91626fc489fe3d63]*/
Raymond Hettingerd6f22672002-05-13 03:56:10 +00002875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 return PyFloat_FromDouble(x * degToRad);
Raymond Hettingerd6f22672002-05-13 03:56:10 +00002877}
2878
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002879
2880/*[clinic input]
2881math.isfinite
2882
2883 x: double
2884 /
2885
2886Return True if x is neither an infinity nor a NaN, and False otherwise.
2887[clinic start generated code]*/
Tim Peters78526162001-09-05 00:53:45 +00002888
Christian Heimes072c0f12008-01-03 23:01:04 +00002889static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002890math_isfinite_impl(PyObject *module, double x)
2891/*[clinic end generated code: output=8ba1f396440c9901 input=46967d254812e54a]*/
Mark Dickinson8e0c9962010-07-11 17:38:24 +00002892{
Mark Dickinson8e0c9962010-07-11 17:38:24 +00002893 return PyBool_FromLong((long)Py_IS_FINITE(x));
2894}
2895
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002896
2897/*[clinic input]
2898math.isnan
2899
2900 x: double
2901 /
2902
2903Return True if x is a NaN (not a number), and False otherwise.
2904[clinic start generated code]*/
Mark Dickinson8e0c9962010-07-11 17:38:24 +00002905
2906static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002907math_isnan_impl(PyObject *module, double x)
2908/*[clinic end generated code: output=f537b4d6df878c3e input=935891e66083f46a]*/
Christian Heimes072c0f12008-01-03 23:01:04 +00002909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 return PyBool_FromLong((long)Py_IS_NAN(x));
Christian Heimes072c0f12008-01-03 23:01:04 +00002911}
2912
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002913
2914/*[clinic input]
2915math.isinf
2916
2917 x: double
2918 /
2919
2920Return True if x is a positive or negative infinity, and False otherwise.
2921[clinic start generated code]*/
Christian Heimes072c0f12008-01-03 23:01:04 +00002922
2923static PyObject *
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002924math_isinf_impl(PyObject *module, double x)
2925/*[clinic end generated code: output=9f00cbec4de7b06b input=32630e4212cf961f]*/
Christian Heimes072c0f12008-01-03 23:01:04 +00002926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 return PyBool_FromLong((long)Py_IS_INFINITY(x));
Christian Heimes072c0f12008-01-03 23:01:04 +00002928}
2929
Christian Heimes072c0f12008-01-03 23:01:04 +00002930
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002931/*[clinic input]
2932math.isclose -> bool
2933
2934 a: double
2935 b: double
2936 *
2937 rel_tol: double = 1e-09
2938 maximum difference for being considered "close", relative to the
2939 magnitude of the input values
2940 abs_tol: double = 0.0
2941 maximum difference for being considered "close", regardless of the
2942 magnitude of the input values
2943
2944Determine whether two floating point numbers are close in value.
2945
2946Return True if a is close in value to b, and False otherwise.
2947
2948For the values to be considered close, the difference between them
2949must be smaller than at least one of the tolerances.
2950
2951-inf, inf and NaN behave similarly to the IEEE 754 Standard. That
2952is, NaN is not close to anything, even itself. inf and -inf are
2953only close to themselves.
2954[clinic start generated code]*/
2955
2956static int
2957math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
2958 double abs_tol)
2959/*[clinic end generated code: output=b73070207511952d input=f28671871ea5bfba]*/
Tal Einatd5519ed2015-05-31 22:05:00 +03002960{
Tal Einatd5519ed2015-05-31 22:05:00 +03002961 double diff = 0.0;
Tal Einatd5519ed2015-05-31 22:05:00 +03002962
2963 /* sanity check on the inputs */
2964 if (rel_tol < 0.0 || abs_tol < 0.0 ) {
2965 PyErr_SetString(PyExc_ValueError,
2966 "tolerances must be non-negative");
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002967 return -1;
Tal Einatd5519ed2015-05-31 22:05:00 +03002968 }
2969
2970 if ( a == b ) {
2971 /* short circuit exact equality -- needed to catch two infinities of
2972 the same sign. And perhaps speeds things up a bit sometimes.
2973 */
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002974 return 1;
Tal Einatd5519ed2015-05-31 22:05:00 +03002975 }
2976
2977 /* This catches the case of two infinities of opposite sign, or
2978 one infinity and one finite number. Two infinities of opposite
2979 sign would otherwise have an infinite relative tolerance.
2980 Two infinities of the same sign are caught by the equality check
2981 above.
2982 */
2983
2984 if (Py_IS_INFINITY(a) || Py_IS_INFINITY(b)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002985 return 0;
Tal Einatd5519ed2015-05-31 22:05:00 +03002986 }
2987
2988 /* now do the regular computation
2989 this is essentially the "weak" test from the Boost library
2990 */
2991
2992 diff = fabs(b - a);
2993
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02002994 return (((diff <= fabs(rel_tol * b)) ||
2995 (diff <= fabs(rel_tol * a))) ||
2996 (diff <= abs_tol));
Tal Einatd5519ed2015-05-31 22:05:00 +03002997}
2998
Pablo Galindo04114112019-03-09 19:18:08 +00002999static inline int
3000_check_long_mult_overflow(long a, long b) {
3001
3002 /* From Python2's int_mul code:
3003
3004 Integer overflow checking for * is painful: Python tried a couple ways, but
3005 they didn't work on all platforms, or failed in endcases (a product of
3006 -sys.maxint-1 has been a particular pain).
3007
3008 Here's another way:
3009
3010 The native long product x*y is either exactly right or *way* off, being
3011 just the last n bits of the true product, where n is the number of bits
3012 in a long (the delivered product is the true product plus i*2**n for
3013 some integer i).
3014
3015 The native double product (double)x * (double)y is subject to three
3016 rounding errors: on a sizeof(long)==8 box, each cast to double can lose
3017 info, and even on a sizeof(long)==4 box, the multiplication can lose info.
3018 But, unlike the native long product, it's not in *range* trouble: even
3019 if sizeof(long)==32 (256-bit longs), the product easily fits in the
3020 dynamic range of a double. So the leading 50 (or so) bits of the double
3021 product are correct.
3022
3023 We check these two ways against each other, and declare victory if they're
3024 approximately the same. Else, because the native long product is the only
3025 one that can lose catastrophic amounts of information, it's the native long
3026 product that must have overflowed.
3027
3028 */
3029
3030 long longprod = (long)((unsigned long)a * b);
3031 double doubleprod = (double)a * (double)b;
3032 double doubled_longprod = (double)longprod;
3033
3034 if (doubled_longprod == doubleprod) {
3035 return 0;
3036 }
3037
3038 const double diff = doubled_longprod - doubleprod;
3039 const double absdiff = diff >= 0.0 ? diff : -diff;
3040 const double absprod = doubleprod >= 0.0 ? doubleprod : -doubleprod;
3041
3042 if (32.0 * absdiff <= absprod) {
3043 return 0;
3044 }
3045
3046 return 1;
3047}
Tal Einatd5519ed2015-05-31 22:05:00 +03003048
Pablo Galindobc098512019-02-07 07:04:02 +00003049/*[clinic input]
3050math.prod
3051
3052 iterable: object
3053 /
3054 *
3055 start: object(c_default="NULL") = 1
3056
3057Calculate the product of all the elements in the input iterable.
3058
3059The default start value for the product is 1.
3060
3061When the iterable is empty, return the start value. This function is
3062intended specifically for use with numeric values and may reject
3063non-numeric types.
3064[clinic start generated code]*/
3065
3066static PyObject *
3067math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
3068/*[clinic end generated code: output=36153bedac74a198 input=4c5ab0682782ed54]*/
3069{
3070 PyObject *result = start;
3071 PyObject *temp, *item, *iter;
3072
3073 iter = PyObject_GetIter(iterable);
3074 if (iter == NULL) {
3075 return NULL;
3076 }
3077
3078 if (result == NULL) {
3079 result = PyLong_FromLong(1);
3080 if (result == NULL) {
3081 Py_DECREF(iter);
3082 return NULL;
3083 }
3084 } else {
3085 Py_INCREF(result);
3086 }
3087#ifndef SLOW_PROD
3088 /* Fast paths for integers keeping temporary products in C.
3089 * Assumes all inputs are the same type.
3090 * If the assumption fails, default to use PyObjects instead.
3091 */
3092 if (PyLong_CheckExact(result)) {
3093 int overflow;
3094 long i_result = PyLong_AsLongAndOverflow(result, &overflow);
3095 /* If this already overflowed, don't even enter the loop. */
3096 if (overflow == 0) {
3097 Py_DECREF(result);
3098 result = NULL;
3099 }
3100 /* Loop over all the items in the iterable until we finish, we overflow
3101 * or we found a non integer element */
3102 while(result == NULL) {
3103 item = PyIter_Next(iter);
3104 if (item == NULL) {
3105 Py_DECREF(iter);
3106 if (PyErr_Occurred()) {
3107 return NULL;
3108 }
3109 return PyLong_FromLong(i_result);
3110 }
3111 if (PyLong_CheckExact(item)) {
3112 long b = PyLong_AsLongAndOverflow(item, &overflow);
Pablo Galindo04114112019-03-09 19:18:08 +00003113 if (overflow == 0 && !_check_long_mult_overflow(i_result, b)) {
3114 long x = i_result * b;
Pablo Galindobc098512019-02-07 07:04:02 +00003115 i_result = x;
3116 Py_DECREF(item);
3117 continue;
3118 }
3119 }
3120 /* Either overflowed or is not an int.
3121 * Restore real objects and process normally */
3122 result = PyLong_FromLong(i_result);
3123 if (result == NULL) {
3124 Py_DECREF(item);
3125 Py_DECREF(iter);
3126 return NULL;
3127 }
3128 temp = PyNumber_Multiply(result, item);
3129 Py_DECREF(result);
3130 Py_DECREF(item);
3131 result = temp;
3132 if (result == NULL) {
3133 Py_DECREF(iter);
3134 return NULL;
3135 }
3136 }
3137 }
3138
3139 /* Fast paths for floats keeping temporary products in C.
3140 * Assumes all inputs are the same type.
3141 * If the assumption fails, default to use PyObjects instead.
3142 */
3143 if (PyFloat_CheckExact(result)) {
3144 double f_result = PyFloat_AS_DOUBLE(result);
3145 Py_DECREF(result);
3146 result = NULL;
3147 while(result == NULL) {
3148 item = PyIter_Next(iter);
3149 if (item == NULL) {
3150 Py_DECREF(iter);
3151 if (PyErr_Occurred()) {
3152 return NULL;
3153 }
3154 return PyFloat_FromDouble(f_result);
3155 }
3156 if (PyFloat_CheckExact(item)) {
3157 f_result *= PyFloat_AS_DOUBLE(item);
3158 Py_DECREF(item);
3159 continue;
3160 }
3161 if (PyLong_CheckExact(item)) {
3162 long value;
3163 int overflow;
3164 value = PyLong_AsLongAndOverflow(item, &overflow);
3165 if (!overflow) {
3166 f_result *= (double)value;
3167 Py_DECREF(item);
3168 continue;
3169 }
3170 }
3171 result = PyFloat_FromDouble(f_result);
3172 if (result == NULL) {
3173 Py_DECREF(item);
3174 Py_DECREF(iter);
3175 return NULL;
3176 }
3177 temp = PyNumber_Multiply(result, item);
3178 Py_DECREF(result);
3179 Py_DECREF(item);
3180 result = temp;
3181 if (result == NULL) {
3182 Py_DECREF(iter);
3183 return NULL;
3184 }
3185 }
3186 }
3187#endif
3188 /* Consume rest of the iterable (if any) that could not be handled
3189 * by specialized functions above.*/
3190 for(;;) {
3191 item = PyIter_Next(iter);
3192 if (item == NULL) {
3193 /* error, or end-of-sequence */
3194 if (PyErr_Occurred()) {
3195 Py_DECREF(result);
3196 result = NULL;
3197 }
3198 break;
3199 }
3200 temp = PyNumber_Multiply(result, item);
3201 Py_DECREF(result);
3202 Py_DECREF(item);
3203 result = temp;
3204 if (result == NULL)
3205 break;
3206 }
3207 Py_DECREF(iter);
3208 return result;
3209}
3210
3211
Yash Aggarwal4a686502019-06-01 12:51:27 +05303212/*[clinic input]
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003213math.perm
3214
3215 n: object
Raymond Hettingere119b3d2019-06-08 08:58:11 -07003216 k: object = None
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003217 /
3218
3219Number of ways to choose k items from n items without repetition and with order.
3220
Raymond Hettinger963eb0f2019-06-04 01:23:06 -07003221Evaluates to n! / (n - k)! when k <= n and evaluates
3222to zero when k > n.
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003223
Raymond Hettingere119b3d2019-06-08 08:58:11 -07003224If k is not specified or is None, then k defaults to n
3225and the function returns n!.
3226
Raymond Hettinger963eb0f2019-06-04 01:23:06 -07003227Raises TypeError if either of the arguments are not integers.
3228Raises ValueError if either of the arguments are negative.
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003229[clinic start generated code]*/
3230
3231static PyObject *
3232math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
Raymond Hettingere119b3d2019-06-08 08:58:11 -07003233/*[clinic end generated code: output=e021a25469653e23 input=5311c5a00f359b53]*/
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003234{
3235 PyObject *result = NULL, *factor = NULL;
3236 int overflow, cmp;
3237 long long i, factors;
3238
Raymond Hettingere119b3d2019-06-08 08:58:11 -07003239 if (k == Py_None) {
3240 return math_factorial(module, n);
3241 }
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003242 n = PyNumber_Index(n);
3243 if (n == NULL) {
3244 return NULL;
3245 }
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003246 k = PyNumber_Index(k);
3247 if (k == NULL) {
3248 Py_DECREF(n);
3249 return NULL;
3250 }
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003251
3252 if (Py_SIZE(n) < 0) {
3253 PyErr_SetString(PyExc_ValueError,
3254 "n must be a non-negative integer");
3255 goto error;
3256 }
Mark Dickinson45e04112019-06-16 11:06:06 +01003257 if (Py_SIZE(k) < 0) {
3258 PyErr_SetString(PyExc_ValueError,
3259 "k must be a non-negative integer");
3260 goto error;
3261 }
3262
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003263 cmp = PyObject_RichCompareBool(n, k, Py_LT);
3264 if (cmp != 0) {
3265 if (cmp > 0) {
Raymond Hettinger963eb0f2019-06-04 01:23:06 -07003266 result = PyLong_FromLong(0);
3267 goto done;
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003268 }
3269 goto error;
3270 }
3271
3272 factors = PyLong_AsLongLongAndOverflow(k, &overflow);
3273 if (overflow > 0) {
3274 PyErr_Format(PyExc_OverflowError,
3275 "k must not exceed %lld",
3276 LLONG_MAX);
3277 goto error;
3278 }
Mark Dickinson45e04112019-06-16 11:06:06 +01003279 else if (factors == -1) {
3280 /* k is nonnegative, so a return value of -1 can only indicate error */
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003281 goto error;
3282 }
3283
3284 if (factors == 0) {
3285 result = PyLong_FromLong(1);
3286 goto done;
3287 }
3288
3289 result = n;
3290 Py_INCREF(result);
3291 if (factors == 1) {
3292 goto done;
3293 }
3294
3295 factor = n;
3296 Py_INCREF(factor);
3297 for (i = 1; i < factors; ++i) {
3298 Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_One));
3299 if (factor == NULL) {
3300 goto error;
3301 }
3302 Py_SETREF(result, PyNumber_Multiply(result, factor));
3303 if (result == NULL) {
3304 goto error;
3305 }
3306 }
3307 Py_DECREF(factor);
3308
3309done:
3310 Py_DECREF(n);
3311 Py_DECREF(k);
3312 return result;
3313
3314error:
3315 Py_XDECREF(factor);
3316 Py_XDECREF(result);
3317 Py_DECREF(n);
3318 Py_DECREF(k);
3319 return NULL;
3320}
3321
3322
3323/*[clinic input]
Yash Aggarwal4a686502019-06-01 12:51:27 +05303324math.comb
3325
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003326 n: object
3327 k: object
3328 /
Yash Aggarwal4a686502019-06-01 12:51:27 +05303329
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003330Number of ways to choose k items from n items without repetition and without order.
Yash Aggarwal4a686502019-06-01 12:51:27 +05303331
Raymond Hettinger963eb0f2019-06-04 01:23:06 -07003332Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates
3333to zero when k > n.
Yash Aggarwal4a686502019-06-01 12:51:27 +05303334
Raymond Hettinger963eb0f2019-06-04 01:23:06 -07003335Also called the binomial coefficient because it is equivalent
3336to the coefficient of k-th term in polynomial expansion of the
3337expression (1 + x)**n.
3338
3339Raises TypeError if either of the arguments are not integers.
3340Raises ValueError if either of the arguments are negative.
Yash Aggarwal4a686502019-06-01 12:51:27 +05303341
3342[clinic start generated code]*/
3343
3344static PyObject *
3345math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
Raymond Hettinger963eb0f2019-06-04 01:23:06 -07003346/*[clinic end generated code: output=bd2cec8d854f3493 input=9a05315af2518709]*/
Yash Aggarwal4a686502019-06-01 12:51:27 +05303347{
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003348 PyObject *result = NULL, *factor = NULL, *temp;
Yash Aggarwal4a686502019-06-01 12:51:27 +05303349 int overflow, cmp;
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003350 long long i, factors;
Yash Aggarwal4a686502019-06-01 12:51:27 +05303351
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003352 n = PyNumber_Index(n);
3353 if (n == NULL) {
3354 return NULL;
Yash Aggarwal4a686502019-06-01 12:51:27 +05303355 }
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003356 k = PyNumber_Index(k);
3357 if (k == NULL) {
3358 Py_DECREF(n);
3359 return NULL;
Yash Aggarwal4a686502019-06-01 12:51:27 +05303360 }
3361
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003362 if (Py_SIZE(n) < 0) {
3363 PyErr_SetString(PyExc_ValueError,
3364 "n must be a non-negative integer");
3365 goto error;
Yash Aggarwal4a686502019-06-01 12:51:27 +05303366 }
Mark Dickinson45e04112019-06-16 11:06:06 +01003367 if (Py_SIZE(k) < 0) {
3368 PyErr_SetString(PyExc_ValueError,
3369 "k must be a non-negative integer");
3370 goto error;
3371 }
3372
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003373 /* k = min(k, n - k) */
3374 temp = PyNumber_Subtract(n, k);
3375 if (temp == NULL) {
3376 goto error;
Yash Aggarwal4a686502019-06-01 12:51:27 +05303377 }
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003378 if (Py_SIZE(temp) < 0) {
3379 Py_DECREF(temp);
Raymond Hettinger963eb0f2019-06-04 01:23:06 -07003380 result = PyLong_FromLong(0);
3381 goto done;
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003382 }
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003383 cmp = PyObject_RichCompareBool(temp, k, Py_LT);
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003384 if (cmp > 0) {
3385 Py_SETREF(k, temp);
Yash Aggarwal4a686502019-06-01 12:51:27 +05303386 }
3387 else {
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003388 Py_DECREF(temp);
3389 if (cmp < 0) {
3390 goto error;
3391 }
Yash Aggarwal4a686502019-06-01 12:51:27 +05303392 }
3393
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003394 factors = PyLong_AsLongLongAndOverflow(k, &overflow);
3395 if (overflow > 0) {
Yash Aggarwal4a686502019-06-01 12:51:27 +05303396 PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003397 "min(n - k, k) must not exceed %lld",
Yash Aggarwal4a686502019-06-01 12:51:27 +05303398 LLONG_MAX);
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003399 goto error;
Yash Aggarwal4a686502019-06-01 12:51:27 +05303400 }
Mark Dickinson45e04112019-06-16 11:06:06 +01003401 if (factors == -1) {
3402 /* k is nonnegative, so a return value of -1 can only indicate error */
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003403 goto error;
Yash Aggarwal4a686502019-06-01 12:51:27 +05303404 }
3405
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003406 if (factors == 0) {
3407 result = PyLong_FromLong(1);
3408 goto done;
Yash Aggarwal4a686502019-06-01 12:51:27 +05303409 }
3410
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003411 result = n;
3412 Py_INCREF(result);
3413 if (factors == 1) {
3414 goto done;
Yash Aggarwal4a686502019-06-01 12:51:27 +05303415 }
3416
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003417 factor = n;
3418 Py_INCREF(factor);
3419 for (i = 1; i < factors; ++i) {
3420 Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_One));
3421 if (factor == NULL) {
3422 goto error;
3423 }
3424 Py_SETREF(result, PyNumber_Multiply(result, factor));
3425 if (result == NULL) {
3426 goto error;
3427 }
Yash Aggarwal4a686502019-06-01 12:51:27 +05303428
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003429 temp = PyLong_FromUnsignedLongLong((unsigned long long)i + 1);
3430 if (temp == NULL) {
3431 goto error;
3432 }
3433 Py_SETREF(result, PyNumber_FloorDivide(result, temp));
3434 Py_DECREF(temp);
3435 if (result == NULL) {
3436 goto error;
3437 }
3438 }
3439 Py_DECREF(factor);
Yash Aggarwal4a686502019-06-01 12:51:27 +05303440
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +03003441done:
3442 Py_DECREF(n);
3443 Py_DECREF(k);
3444 return result;
3445
3446error:
3447 Py_XDECREF(factor);
3448 Py_XDECREF(result);
3449 Py_DECREF(n);
3450 Py_DECREF(k);
Yash Aggarwal4a686502019-06-01 12:51:27 +05303451 return NULL;
3452}
3453
3454
Victor Stinner100fafc2020-01-12 02:15:42 +01003455/*[clinic input]
3456math.nextafter
3457
3458 x: double
3459 y: double
3460 /
3461
3462Return the next floating-point value after x towards y.
3463[clinic start generated code]*/
3464
3465static PyObject *
3466math_nextafter_impl(PyObject *module, double x, double y)
3467/*[clinic end generated code: output=750c8266c1c540ce input=02b2d50cd1d9f9b6]*/
3468{
Victor Stinner85ead4f2020-01-21 11:14:10 +01003469#if defined(_AIX)
3470 if (x == y) {
3471 /* On AIX 7.1, libm nextafter(-0.0, +0.0) returns -0.0.
3472 Bug fixed in bos.adt.libm 7.2.2.0 by APAR IV95512. */
3473 return PyFloat_FromDouble(y);
3474 }
3475#endif
3476 return PyFloat_FromDouble(nextafter(x, y));
Victor Stinner100fafc2020-01-12 02:15:42 +01003477}
3478
3479
Victor Stinner0b2ab212020-01-13 12:44:35 +01003480/*[clinic input]
3481math.ulp -> double
3482
3483 x: double
3484 /
3485
3486Return the value of the least significant bit of the float x.
3487[clinic start generated code]*/
3488
3489static double
3490math_ulp_impl(PyObject *module, double x)
3491/*[clinic end generated code: output=f5207867a9384dd4 input=31f9bfbbe373fcaa]*/
3492{
3493 if (Py_IS_NAN(x)) {
3494 return x;
3495 }
3496 x = fabs(x);
3497 if (Py_IS_INFINITY(x)) {
3498 return x;
3499 }
3500 double inf = m_inf();
3501 double x2 = nextafter(x, inf);
3502 if (Py_IS_INFINITY(x2)) {
3503 /* special case: x is the largest positive representable float */
3504 x2 = nextafter(x, -inf);
3505 return x - x2;
3506 }
3507 return x2 - x;
3508}
3509
Dong-hee Na5be82412020-03-31 23:33:22 +09003510static int
3511math_exec(PyObject *module)
3512{
3513 if (PyModule_AddObject(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
3514 return -1;
3515 }
3516 if (PyModule_AddObject(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
3517 return -1;
3518 }
3519 // 2pi
3520 if (PyModule_AddObject(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
3521 return -1;
3522 }
3523 if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(m_inf())) < 0) {
3524 return -1;
3525 }
3526#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
3527 if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(m_nan())) < 0) {
3528 return -1;
3529 }
3530#endif
3531 return 0;
3532}
Victor Stinner0b2ab212020-01-13 12:44:35 +01003533
Barry Warsaw8b43b191996-12-09 22:32:36 +00003534static PyMethodDef math_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 {"acos", math_acos, METH_O, math_acos_doc},
3536 {"acosh", math_acosh, METH_O, math_acosh_doc},
3537 {"asin", math_asin, METH_O, math_asin_doc},
3538 {"asinh", math_asinh, METH_O, math_asinh_doc},
3539 {"atan", math_atan, METH_O, math_atan_doc},
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02003540 {"atan2", (PyCFunction)(void(*)(void))math_atan2, METH_FASTCALL, math_atan2_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 {"atanh", math_atanh, METH_O, math_atanh_doc},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02003542 MATH_CEIL_METHODDEF
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02003543 {"copysign", (PyCFunction)(void(*)(void))math_copysign, METH_FASTCALL, math_copysign_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 {"cos", math_cos, METH_O, math_cos_doc},
3545 {"cosh", math_cosh, METH_O, math_cosh_doc},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02003546 MATH_DEGREES_METHODDEF
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -07003547 MATH_DIST_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 {"erf", math_erf, METH_O, math_erf_doc},
3549 {"erfc", math_erfc, METH_O, math_erfc_doc},
3550 {"exp", math_exp, METH_O, math_exp_doc},
3551 {"expm1", math_expm1, METH_O, math_expm1_doc},
3552 {"fabs", math_fabs, METH_O, math_fabs_doc},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02003553 MATH_FACTORIAL_METHODDEF
3554 MATH_FLOOR_METHODDEF
3555 MATH_FMOD_METHODDEF
3556 MATH_FREXP_METHODDEF
3557 MATH_FSUM_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 {"gamma", math_gamma, METH_O, math_gamma_doc},
Serhiy Storchaka559e7f12020-02-23 13:21:29 +02003559 {"gcd", (PyCFunction)(void(*)(void))math_gcd, METH_FASTCALL, math_gcd_doc},
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02003560 {"hypot", (PyCFunction)(void(*)(void))math_hypot, METH_FASTCALL, math_hypot_doc},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02003561 MATH_ISCLOSE_METHODDEF
3562 MATH_ISFINITE_METHODDEF
3563 MATH_ISINF_METHODDEF
3564 MATH_ISNAN_METHODDEF
Mark Dickinson73934b92019-05-18 12:29:50 +01003565 MATH_ISQRT_METHODDEF
Serhiy Storchaka559e7f12020-02-23 13:21:29 +02003566 {"lcm", (PyCFunction)(void(*)(void))math_lcm, METH_FASTCALL, math_lcm_doc},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02003567 MATH_LDEXP_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 {"lgamma", math_lgamma, METH_O, math_lgamma_doc},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02003569 MATH_LOG_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 {"log1p", math_log1p, METH_O, math_log1p_doc},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02003571 MATH_LOG10_METHODDEF
3572 MATH_LOG2_METHODDEF
3573 MATH_MODF_METHODDEF
3574 MATH_POW_METHODDEF
3575 MATH_RADIANS_METHODDEF
Serhiy Storchakad0d3e992019-01-12 08:26:34 +02003576 {"remainder", (PyCFunction)(void(*)(void))math_remainder, METH_FASTCALL, math_remainder_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 {"sin", math_sin, METH_O, math_sin_doc},
3578 {"sinh", math_sinh, METH_O, math_sinh_doc},
3579 {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
3580 {"tan", math_tan, METH_O, math_tan_doc},
3581 {"tanh", math_tanh, METH_O, math_tanh_doc},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02003582 MATH_TRUNC_METHODDEF
Pablo Galindobc098512019-02-07 07:04:02 +00003583 MATH_PROD_METHODDEF
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +03003584 MATH_PERM_METHODDEF
Yash Aggarwal4a686502019-06-01 12:51:27 +05303585 MATH_COMB_METHODDEF
Victor Stinner100fafc2020-01-12 02:15:42 +01003586 MATH_NEXTAFTER_METHODDEF
Victor Stinner0b2ab212020-01-13 12:44:35 +01003587 MATH_ULP_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003589};
3590
Dong-hee Na5be82412020-03-31 23:33:22 +09003591static PyModuleDef_Slot math_slots[] = {
3592 {Py_mod_exec, math_exec},
3593 {0, NULL}
3594};
Guido van Rossumc6e22901998-12-04 19:26:43 +00003595
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003596PyDoc_STRVAR(module_doc,
Ned Batchelder6faad352019-05-17 05:59:14 -04003597"This module provides access to the mathematical functions\n"
3598"defined by the C standard.");
Guido van Rossumc6e22901998-12-04 19:26:43 +00003599
Martin v. Löwis1a214512008-06-11 05:26:20 +00003600static struct PyModuleDef mathmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 PyModuleDef_HEAD_INIT,
Dong-hee Na5be82412020-03-31 23:33:22 +09003602 .m_name = "math",
3603 .m_doc = module_doc,
3604 .m_size = 0,
3605 .m_methods = math_methods,
3606 .m_slots = math_slots,
Martin v. Löwis1a214512008-06-11 05:26:20 +00003607};
3608
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003609PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003610PyInit_math(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003611{
Dong-hee Na5be82412020-03-31 23:33:22 +09003612 return PyModuleDef_Init(&mathmodule);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003613}