• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Math module -- standard C math library functions, pi and e */
2 
3 /* 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 
9 These are the "spirit of 754" rules:
10 
11 1. If the mathematical result is a real number, but of magnitude too
12 large to approximate by a machine float, overflow is signaled and the
13 result is an infinity (with the appropriate sign).
14 
15 2. If the mathematical result is a real number, but of magnitude too
16 small to approximate by a machine float, underflow is signaled and the
17 result is a zero (with the appropriate sign).
18 
19 3. At a singularity (a value x such that the limit of f(y) as y
20 approaches x exists and is an infinity), "divide by zero" is signaled
21 and the result is an infinity (with the appropriate sign).  This is
22 complicated a little by that the left-side and right-side limits may
23 not be the same; e.g., 1/x approaches +inf or -inf as x approaches 0
24 from the positive or negative directions.  In that specific case, the
25 sign of the zero determines the result of 1/0.
26 
27 4. At a point where a function has no defined result in the extended
28 reals (i.e., the reals plus an infinity or two), invalid operation is
29 signaled and a NaN is returned.
30 
31 And these are what Python has historically /tried/ to do (but not
32 always successfully, as platform libm behavior varies a lot):
33 
34 For #1, raise OverflowError.
35 
36 For #2, return a zero (with the appropriate sign if that happens by
37 accident ;-)).
38 
39 For #3 and #4, raise ValueError.  It may have made sense to raise
40 Python's ZeroDivisionError in #3, but historically that's only been
41 raised 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 
55 #include "Python.h"
56 #include "pycore_dtoa.h"
57 #include "_math.h"
58 
59 #include "clinic/mathmodule.c.h"
60 
61 /*[clinic input]
62 module math
63 [clinic start generated code]*/
64 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=76bc7002685dd942]*/
65 
66 
67 /*
68    sin(pi*x), giving accurate results for all finite x (especially x
69    integral or close to an integer).  This is here for use in the
70    reflection formula for the gamma function.  It conforms to IEEE
71    754-2008 for finite arguments, but not for infinities or nans.
72 */
73 
74 static const double pi = 3.141592653589793238462643383279502884197;
75 static const double logpi = 1.144729885849400174143427351353058711647;
76 #if !defined(HAVE_ERF) || !defined(HAVE_ERFC)
77 static const double sqrtpi = 1.772453850905516027298167483341145182798;
78 #endif /* !defined(HAVE_ERF) || !defined(HAVE_ERFC) */
79 
80 
81 /* Version of PyFloat_AsDouble() with in-line fast paths
82    for exact floats and integers.  Gives a substantial
83    speed improvement for extracting float arguments.
84 */
85 
86 #define ASSIGN_DOUBLE(target_var, obj, error_label)        \
87     if (PyFloat_CheckExact(obj)) {                         \
88         target_var = PyFloat_AS_DOUBLE(obj);               \
89     }                                                      \
90     else if (PyLong_CheckExact(obj)) {                     \
91         target_var = PyLong_AsDouble(obj);                 \
92         if (target_var == -1.0 && PyErr_Occurred()) {      \
93             goto error_label;                              \
94         }                                                  \
95     }                                                      \
96     else {                                                 \
97         target_var = PyFloat_AsDouble(obj);                \
98         if (target_var == -1.0 && PyErr_Occurred()) {      \
99             goto error_label;                              \
100         }                                                  \
101     }
102 
103 static double
m_sinpi(double x)104 m_sinpi(double x)
105 {
106     double y, r;
107     int n;
108     /* this function should only ever be called for finite arguments */
109     assert(Py_IS_FINITE(x));
110     y = fmod(fabs(x), 2.0);
111     n = (int)round(2.0*y);
112     assert(0 <= n && n <= 4);
113     switch (n) {
114     case 0:
115         r = sin(pi*y);
116         break;
117     case 1:
118         r = cos(pi*(y-0.5));
119         break;
120     case 2:
121         /* N.B. -sin(pi*(y-1.0)) is *not* equivalent: it would give
122            -0.0 instead of 0.0 when y == 1.0. */
123         r = sin(pi*(1.0-y));
124         break;
125     case 3:
126         r = -cos(pi*(y-1.5));
127         break;
128     case 4:
129         r = sin(pi*(y-2.0));
130         break;
131     default:
132         Py_UNREACHABLE();
133     }
134     return copysign(1.0, x)*r;
135 }
136 
137 /* Implementation of the real gamma function.  In extensive but non-exhaustive
138    random tests, this function proved accurate to within <= 10 ulps across the
139    entire float domain.  Note that accuracy may depend on the quality of the
140    system math functions, the pow function in particular.  Special cases
141    follow C99 annex F.  The parameters and method are tailored to platforms
142    whose double format is the IEEE 754 binary64 format.
143 
144    Method: for x > 0.0 we use the Lanczos approximation with parameters N=13
145    and g=6.024680040776729583740234375; these parameters are amongst those
146    used by the Boost library.  Following Boost (again), we re-express the
147    Lanczos sum as a rational function, and compute it that way.  The
148    coefficients below were computed independently using MPFR, and have been
149    double-checked against the coefficients in the Boost source code.
150 
151    For x < 0.0 we use the reflection formula.
152 
153    There's one minor tweak that deserves explanation: Lanczos' formula for
154    Gamma(x) involves computing pow(x+g-0.5, x-0.5) / exp(x+g-0.5).  For many x
155    values, x+g-0.5 can be represented exactly.  However, in cases where it
156    can't be represented exactly the small error in x+g-0.5 can be magnified
157    significantly by the pow and exp calls, especially for large x.  A cheap
158    correction is to multiply by (1 + e*g/(x+g-0.5)), where e is the error
159    involved in the computation of x+g-0.5 (that is, e = computed value of
160    x+g-0.5 - exact value of x+g-0.5).  Here's the proof:
161 
162    Correction factor
163    -----------------
164    Write x+g-0.5 = y-e, where y is exactly representable as an IEEE 754
165    double, and e is tiny.  Then:
166 
167      pow(x+g-0.5,x-0.5)/exp(x+g-0.5) = pow(y-e, x-0.5)/exp(y-e)
168      = pow(y, x-0.5)/exp(y) * C,
169 
170    where the correction_factor C is given by
171 
172      C = pow(1-e/y, x-0.5) * exp(e)
173 
174    Since e is tiny, pow(1-e/y, x-0.5) ~ 1-(x-0.5)*e/y, and exp(x) ~ 1+e, so:
175 
176      C ~ (1-(x-0.5)*e/y) * (1+e) ~ 1 + e*(y-(x-0.5))/y
177 
178    But y-(x-0.5) = g+e, and g+e ~ g.  So we get C ~ 1 + e*g/y, and
179 
180      pow(x+g-0.5,x-0.5)/exp(x+g-0.5) ~ pow(y, x-0.5)/exp(y) * (1 + e*g/y),
181 
182    Note that for accuracy, when computing r*C it's better to do
183 
184      r + e*g/y*r;
185 
186    than
187 
188      r * (1 + e*g/y);
189 
190    since the addition in the latter throws away most of the bits of
191    information in e*g/y.
192 */
193 
194 #define LANCZOS_N 13
195 static const double lanczos_g = 6.024680040776729583740234375;
196 static const double lanczos_g_minus_half = 5.524680040776729583740234375;
197 static const double lanczos_num_coeffs[LANCZOS_N] = {
198     23531376880.410759688572007674451636754734846804940,
199     42919803642.649098768957899047001988850926355848959,
200     35711959237.355668049440185451547166705960488635843,
201     17921034426.037209699919755754458931112671403265390,
202     6039542586.3520280050642916443072979210699388420708,
203     1439720407.3117216736632230727949123939715485786772,
204     248874557.86205415651146038641322942321632125127801,
205     31426415.585400194380614231628318205362874684987640,
206     2876370.6289353724412254090516208496135991145378768,
207     186056.26539522349504029498971604569928220784236328,
208     8071.6720023658162106380029022722506138218516325024,
209     210.82427775157934587250973392071336271166969580291,
210     2.5066282746310002701649081771338373386264310793408
211 };
212 
213 /* denominator is x*(x+1)*...*(x+LANCZOS_N-2) */
214 static const double lanczos_den_coeffs[LANCZOS_N] = {
215     0.0, 39916800.0, 120543840.0, 150917976.0, 105258076.0, 45995730.0,
216     13339535.0, 2637558.0, 357423.0, 32670.0, 1925.0, 66.0, 1.0};
217 
218 /* gamma values for small positive integers, 1 though NGAMMA_INTEGRAL */
219 #define NGAMMA_INTEGRAL 23
220 static const double gamma_integral[NGAMMA_INTEGRAL] = {
221     1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0, 5040.0, 40320.0, 362880.0,
222     3628800.0, 39916800.0, 479001600.0, 6227020800.0, 87178291200.0,
223     1307674368000.0, 20922789888000.0, 355687428096000.0,
224     6402373705728000.0, 121645100408832000.0, 2432902008176640000.0,
225     51090942171709440000.0, 1124000727777607680000.0,
226 };
227 
228 /* Lanczos' sum L_g(x), for positive x */
229 
230 static double
lanczos_sum(double x)231 lanczos_sum(double x)
232 {
233     double num = 0.0, den = 0.0;
234     int i;
235     assert(x > 0.0);
236     /* evaluate the rational function lanczos_sum(x).  For large
237        x, the obvious algorithm risks overflow, so we instead
238        rescale the denominator and numerator of the rational
239        function by x**(1-LANCZOS_N) and treat this as a
240        rational function in 1/x.  This also reduces the error for
241        larger x values.  The choice of cutoff point (5.0 below) is
242        somewhat arbitrary; in tests, smaller cutoff values than
243        this resulted in lower accuracy. */
244     if (x < 5.0) {
245         for (i = LANCZOS_N; --i >= 0; ) {
246             num = num * x + lanczos_num_coeffs[i];
247             den = den * x + lanczos_den_coeffs[i];
248         }
249     }
250     else {
251         for (i = 0; i < LANCZOS_N; i++) {
252             num = num / x + lanczos_num_coeffs[i];
253             den = den / x + lanczos_den_coeffs[i];
254         }
255     }
256     return num/den;
257 }
258 
259 /* Constant for +infinity, generated in the same way as float('inf'). */
260 
261 static double
m_inf(void)262 m_inf(void)
263 {
264 #ifndef PY_NO_SHORT_FLOAT_REPR
265     return _Py_dg_infinity(0);
266 #else
267     return Py_HUGE_VAL;
268 #endif
269 }
270 
271 /* Constant nan value, generated in the same way as float('nan'). */
272 /* We don't currently assume that Py_NAN is defined everywhere. */
273 
274 #if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
275 
276 static double
m_nan(void)277 m_nan(void)
278 {
279 #ifndef PY_NO_SHORT_FLOAT_REPR
280     return _Py_dg_stdnan(0);
281 #else
282     return Py_NAN;
283 #endif
284 }
285 
286 #endif
287 
288 static double
m_tgamma(double x)289 m_tgamma(double x)
290 {
291     double absx, r, y, z, sqrtpow;
292 
293     /* special cases */
294     if (!Py_IS_FINITE(x)) {
295         if (Py_IS_NAN(x) || x > 0.0)
296             return x;  /* tgamma(nan) = nan, tgamma(inf) = inf */
297         else {
298             errno = EDOM;
299             return Py_NAN;  /* tgamma(-inf) = nan, invalid */
300         }
301     }
302     if (x == 0.0) {
303         errno = EDOM;
304         /* tgamma(+-0.0) = +-inf, divide-by-zero */
305         return copysign(Py_HUGE_VAL, x);
306     }
307 
308     /* integer arguments */
309     if (x == floor(x)) {
310         if (x < 0.0) {
311             errno = EDOM;  /* tgamma(n) = nan, invalid for */
312             return Py_NAN; /* negative integers n */
313         }
314         if (x <= NGAMMA_INTEGRAL)
315             return gamma_integral[(int)x - 1];
316     }
317     absx = fabs(x);
318 
319     /* tiny arguments:  tgamma(x) ~ 1/x for x near 0 */
320     if (absx < 1e-20) {
321         r = 1.0/x;
322         if (Py_IS_INFINITY(r))
323             errno = ERANGE;
324         return r;
325     }
326 
327     /* large arguments: assuming IEEE 754 doubles, tgamma(x) overflows for
328        x > 200, and underflows to +-0.0 for x < -200, not a negative
329        integer. */
330     if (absx > 200.0) {
331         if (x < 0.0) {
332             return 0.0/m_sinpi(x);
333         }
334         else {
335             errno = ERANGE;
336             return Py_HUGE_VAL;
337         }
338     }
339 
340     y = absx + lanczos_g_minus_half;
341     /* compute error in sum */
342     if (absx > lanczos_g_minus_half) {
343         /* note: the correction can be foiled by an optimizing
344            compiler that (incorrectly) thinks that an expression like
345            a + b - a - b can be optimized to 0.0.  This shouldn't
346            happen in a standards-conforming compiler. */
347         double q = y - absx;
348         z = q - lanczos_g_minus_half;
349     }
350     else {
351         double q = y - lanczos_g_minus_half;
352         z = q - absx;
353     }
354     z = z * lanczos_g / y;
355     if (x < 0.0) {
356         r = -pi / m_sinpi(absx) / absx * exp(y) / lanczos_sum(absx);
357         r -= z * r;
358         if (absx < 140.0) {
359             r /= pow(y, absx - 0.5);
360         }
361         else {
362             sqrtpow = pow(y, absx / 2.0 - 0.25);
363             r /= sqrtpow;
364             r /= sqrtpow;
365         }
366     }
367     else {
368         r = lanczos_sum(absx) / exp(y);
369         r += z * r;
370         if (absx < 140.0) {
371             r *= pow(y, absx - 0.5);
372         }
373         else {
374             sqrtpow = pow(y, absx / 2.0 - 0.25);
375             r *= sqrtpow;
376             r *= sqrtpow;
377         }
378     }
379     if (Py_IS_INFINITY(r))
380         errno = ERANGE;
381     return r;
382 }
383 
384 /*
385    lgamma:  natural log of the absolute value of the Gamma function.
386    For large arguments, Lanczos' formula works extremely well here.
387 */
388 
389 static double
m_lgamma(double x)390 m_lgamma(double x)
391 {
392     double r;
393     double absx;
394 
395     /* special cases */
396     if (!Py_IS_FINITE(x)) {
397         if (Py_IS_NAN(x))
398             return x;  /* lgamma(nan) = nan */
399         else
400             return Py_HUGE_VAL; /* lgamma(+-inf) = +inf */
401     }
402 
403     /* integer arguments */
404     if (x == floor(x) && x <= 2.0) {
405         if (x <= 0.0) {
406             errno = EDOM;  /* lgamma(n) = inf, divide-by-zero for */
407             return Py_HUGE_VAL; /* integers n <= 0 */
408         }
409         else {
410             return 0.0; /* lgamma(1) = lgamma(2) = 0.0 */
411         }
412     }
413 
414     absx = fabs(x);
415     /* tiny arguments: lgamma(x) ~ -log(fabs(x)) for small x */
416     if (absx < 1e-20)
417         return -log(absx);
418 
419     /* Lanczos' formula.  We could save a fraction of a ulp in accuracy by
420        having a second set of numerator coefficients for lanczos_sum that
421        absorbed the exp(-lanczos_g) term, and throwing out the lanczos_g
422        subtraction below; it's probably not worth it. */
423     r = log(lanczos_sum(absx)) - lanczos_g;
424     r += (absx - 0.5) * (log(absx + lanczos_g - 0.5) - 1);
425     if (x < 0.0)
426         /* Use reflection formula to get value for negative x. */
427         r = logpi - log(fabs(m_sinpi(absx))) - log(absx) - r;
428     if (Py_IS_INFINITY(r))
429         errno = ERANGE;
430     return r;
431 }
432 
433 #if !defined(HAVE_ERF) || !defined(HAVE_ERFC)
434 
435 /*
436    Implementations of the error function erf(x) and the complementary error
437    function erfc(x).
438 
439    Method: we use a series approximation for erf for small x, and a continued
440    fraction approximation for erfc(x) for larger x;
441    combined with the relations erf(-x) = -erf(x) and erfc(x) = 1.0 - erf(x),
442    this gives us erf(x) and erfc(x) for all x.
443 
444    The series expansion used is:
445 
446       erf(x) = x*exp(-x*x)/sqrt(pi) * [
447                      2/1 + 4/3 x**2 + 8/15 x**4 + 16/105 x**6 + ...]
448 
449    The coefficient of x**(2k-2) here is 4**k*factorial(k)/factorial(2*k).
450    This series converges well for smallish x, but slowly for larger x.
451 
452    The continued fraction expansion used is:
453 
454       erfc(x) = x*exp(-x*x)/sqrt(pi) * [1/(0.5 + x**2 -) 0.5/(2.5 + x**2 - )
455                               3.0/(4.5 + x**2 - ) 7.5/(6.5 + x**2 - ) ...]
456 
457    after the first term, the general term has the form:
458 
459       k*(k-0.5)/(2*k+0.5 + x**2 - ...).
460 
461    This expansion converges fast for larger x, but convergence becomes
462    infinitely slow as x approaches 0.0.  The (somewhat naive) continued
463    fraction evaluation algorithm used below also risks overflow for large x;
464    but for large x, erfc(x) == 0.0 to within machine precision.  (For
465    example, erfc(30.0) is approximately 2.56e-393).
466 
467    Parameters: use series expansion for abs(x) < ERF_SERIES_CUTOFF and
468    continued fraction expansion for ERF_SERIES_CUTOFF <= abs(x) <
469    ERFC_CONTFRAC_CUTOFF.  ERFC_SERIES_TERMS and ERFC_CONTFRAC_TERMS are the
470    numbers of terms to use for the relevant expansions.  */
471 
472 #define ERF_SERIES_CUTOFF 1.5
473 #define ERF_SERIES_TERMS 25
474 #define ERFC_CONTFRAC_CUTOFF 30.0
475 #define ERFC_CONTFRAC_TERMS 50
476 
477 /*
478    Error function, via power series.
479 
480    Given a finite float x, return an approximation to erf(x).
481    Converges reasonably fast for small x.
482 */
483 
484 static double
m_erf_series(double x)485 m_erf_series(double x)
486 {
487     double x2, acc, fk, result;
488     int i, saved_errno;
489 
490     x2 = x * x;
491     acc = 0.0;
492     fk = (double)ERF_SERIES_TERMS + 0.5;
493     for (i = 0; i < ERF_SERIES_TERMS; i++) {
494         acc = 2.0 + x2 * acc / fk;
495         fk -= 1.0;
496     }
497     /* Make sure the exp call doesn't affect errno;
498        see m_erfc_contfrac for more. */
499     saved_errno = errno;
500     result = acc * x * exp(-x2) / sqrtpi;
501     errno = saved_errno;
502     return result;
503 }
504 
505 /*
506    Complementary error function, via continued fraction expansion.
507 
508    Given a positive float x, return an approximation to erfc(x).  Converges
509    reasonably fast for x large (say, x > 2.0), and should be safe from
510    overflow if x and nterms are not too large.  On an IEEE 754 machine, with x
511    <= 30.0, we're safe up to nterms = 100.  For x >= 30.0, erfc(x) is smaller
512    than the smallest representable nonzero float.  */
513 
514 static double
m_erfc_contfrac(double x)515 m_erfc_contfrac(double x)
516 {
517     double x2, a, da, p, p_last, q, q_last, b, result;
518     int i, saved_errno;
519 
520     if (x >= ERFC_CONTFRAC_CUTOFF)
521         return 0.0;
522 
523     x2 = x*x;
524     a = 0.0;
525     da = 0.5;
526     p = 1.0; p_last = 0.0;
527     q = da + x2; q_last = 1.0;
528     for (i = 0; i < ERFC_CONTFRAC_TERMS; i++) {
529         double temp;
530         a += da;
531         da += 2.0;
532         b = da + x2;
533         temp = p; p = b*p - a*p_last; p_last = temp;
534         temp = q; q = b*q - a*q_last; q_last = temp;
535     }
536     /* Issue #8986: On some platforms, exp sets errno on underflow to zero;
537        save the current errno value so that we can restore it later. */
538     saved_errno = errno;
539     result = p / q * x * exp(-x2) / sqrtpi;
540     errno = saved_errno;
541     return result;
542 }
543 
544 #endif /* !defined(HAVE_ERF) || !defined(HAVE_ERFC) */
545 
546 /* Error function erf(x), for general x */
547 
548 static double
m_erf(double x)549 m_erf(double x)
550 {
551 #ifdef HAVE_ERF
552     return erf(x);
553 #else
554     double absx, cf;
555 
556     if (Py_IS_NAN(x))
557         return x;
558     absx = fabs(x);
559     if (absx < ERF_SERIES_CUTOFF)
560         return m_erf_series(x);
561     else {
562         cf = m_erfc_contfrac(absx);
563         return x > 0.0 ? 1.0 - cf : cf - 1.0;
564     }
565 #endif
566 }
567 
568 /* Complementary error function erfc(x), for general x. */
569 
570 static double
m_erfc(double x)571 m_erfc(double x)
572 {
573 #ifdef HAVE_ERFC
574     return erfc(x);
575 #else
576     double absx, cf;
577 
578     if (Py_IS_NAN(x))
579         return x;
580     absx = fabs(x);
581     if (absx < ERF_SERIES_CUTOFF)
582         return 1.0 - m_erf_series(x);
583     else {
584         cf = m_erfc_contfrac(absx);
585         return x > 0.0 ? cf : 2.0 - cf;
586     }
587 #endif
588 }
589 
590 /*
591    wrapper for atan2 that deals directly with special cases before
592    delegating to the platform libm for the remaining cases.  This
593    is necessary to get consistent behaviour across platforms.
594    Windows, FreeBSD and alpha Tru64 are amongst platforms that don't
595    always follow C99.
596 */
597 
598 static double
m_atan2(double y,double x)599 m_atan2(double y, double x)
600 {
601     if (Py_IS_NAN(x) || Py_IS_NAN(y))
602         return Py_NAN;
603     if (Py_IS_INFINITY(y)) {
604         if (Py_IS_INFINITY(x)) {
605             if (copysign(1., x) == 1.)
606                 /* atan2(+-inf, +inf) == +-pi/4 */
607                 return copysign(0.25*Py_MATH_PI, y);
608             else
609                 /* atan2(+-inf, -inf) == +-pi*3/4 */
610                 return copysign(0.75*Py_MATH_PI, y);
611         }
612         /* atan2(+-inf, x) == +-pi/2 for finite x */
613         return copysign(0.5*Py_MATH_PI, y);
614     }
615     if (Py_IS_INFINITY(x) || y == 0.) {
616         if (copysign(1., x) == 1.)
617             /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
618             return copysign(0., y);
619         else
620             /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
621             return copysign(Py_MATH_PI, y);
622     }
623     return atan2(y, x);
624 }
625 
626 
627 /* IEEE 754-style remainder operation: x - n*y where n*y is the nearest
628    multiple of y to x, taking n even in the case of a tie. Assuming an IEEE 754
629    binary floating-point format, the result is always exact. */
630 
631 static double
m_remainder(double x,double y)632 m_remainder(double x, double y)
633 {
634     /* Deal with most common case first. */
635     if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) {
636         double absx, absy, c, m, r;
637 
638         if (y == 0.0) {
639             return Py_NAN;
640         }
641 
642         absx = fabs(x);
643         absy = fabs(y);
644         m = fmod(absx, absy);
645 
646         /*
647            Warning: some subtlety here. What we *want* to know at this point is
648            whether the remainder m is less than, equal to, or greater than half
649            of absy. However, we can't do that comparison directly because we
650            can't be sure that 0.5*absy is representable (the multiplication
651            might incur precision loss due to underflow). So instead we compare
652            m with the complement c = absy - m: m < 0.5*absy if and only if m <
653            c, and so on. The catch is that absy - m might also not be
654            representable, but it turns out that it doesn't matter:
655 
656            - if m > 0.5*absy then absy - m is exactly representable, by
657              Sterbenz's lemma, so m > c
658            - if m == 0.5*absy then again absy - m is exactly representable
659              and m == c
660            - if m < 0.5*absy then either (i) 0.5*absy is exactly representable,
661              in which case 0.5*absy < absy - m, so 0.5*absy <= c and hence m <
662              c, or (ii) absy is tiny, either subnormal or in the lowest normal
663              binade. Then absy - m is exactly representable and again m < c.
664         */
665 
666         c = absy - m;
667         if (m < c) {
668             r = m;
669         }
670         else if (m > c) {
671             r = -c;
672         }
673         else {
674             /*
675                Here absx is exactly halfway between two multiples of absy,
676                and we need to choose the even multiple. x now has the form
677 
678                    absx = n * absy + m
679 
680                for some integer n (recalling that m = 0.5*absy at this point).
681                If n is even we want to return m; if n is odd, we need to
682                return -m.
683 
684                So
685 
686                    0.5 * (absx - m) = (n/2) * absy
687 
688                and now reducing modulo absy gives us:
689 
690                                                   | m, if n is odd
691                    fmod(0.5 * (absx - m), absy) = |
692                                                   | 0, if n is even
693 
694                Now m - 2.0 * fmod(...) gives the desired result: m
695                if n is even, -m if m is odd.
696 
697                Note that all steps in fmod(0.5 * (absx - m), absy)
698                will be computed exactly, with no rounding error
699                introduced.
700             */
701             assert(m == c);
702             r = m - 2.0 * fmod(0.5 * (absx - m), absy);
703         }
704         return copysign(1.0, x) * r;
705     }
706 
707     /* Special values. */
708     if (Py_IS_NAN(x)) {
709         return x;
710     }
711     if (Py_IS_NAN(y)) {
712         return y;
713     }
714     if (Py_IS_INFINITY(x)) {
715         return Py_NAN;
716     }
717     assert(Py_IS_INFINITY(y));
718     return x;
719 }
720 
721 
722 /*
723     Various platforms (Solaris, OpenBSD) do nonstandard things for log(0),
724     log(-ve), log(NaN).  Here are wrappers for log and log10 that deal with
725     special values directly, passing positive non-special values through to
726     the system log/log10.
727  */
728 
729 static double
m_log(double x)730 m_log(double x)
731 {
732     if (Py_IS_FINITE(x)) {
733         if (x > 0.0)
734             return log(x);
735         errno = EDOM;
736         if (x == 0.0)
737             return -Py_HUGE_VAL; /* log(0) = -inf */
738         else
739             return Py_NAN; /* log(-ve) = nan */
740     }
741     else if (Py_IS_NAN(x))
742         return x; /* log(nan) = nan */
743     else if (x > 0.0)
744         return x; /* log(inf) = inf */
745     else {
746         errno = EDOM;
747         return Py_NAN; /* log(-inf) = nan */
748     }
749 }
750 
751 /*
752    log2: log to base 2.
753 
754    Uses an algorithm that should:
755 
756      (a) produce exact results for powers of 2, and
757      (b) give a monotonic log2 (for positive finite floats),
758          assuming that the system log is monotonic.
759 */
760 
761 static double
m_log2(double x)762 m_log2(double x)
763 {
764     if (!Py_IS_FINITE(x)) {
765         if (Py_IS_NAN(x))
766             return x; /* log2(nan) = nan */
767         else if (x > 0.0)
768             return x; /* log2(+inf) = +inf */
769         else {
770             errno = EDOM;
771             return Py_NAN; /* log2(-inf) = nan, invalid-operation */
772         }
773     }
774 
775     if (x > 0.0) {
776 #ifdef HAVE_LOG2
777         return log2(x);
778 #else
779         double m;
780         int e;
781         m = frexp(x, &e);
782         /* We want log2(m * 2**e) == log(m) / log(2) + e.  Care is needed when
783          * x is just greater than 1.0: in that case e is 1, log(m) is negative,
784          * and we get significant cancellation error from the addition of
785          * log(m) / log(2) to e.  The slight rewrite of the expression below
786          * avoids this problem.
787          */
788         if (x >= 1.0) {
789             return log(2.0 * m) / log(2.0) + (e - 1);
790         }
791         else {
792             return log(m) / log(2.0) + e;
793         }
794 #endif
795     }
796     else if (x == 0.0) {
797         errno = EDOM;
798         return -Py_HUGE_VAL; /* log2(0) = -inf, divide-by-zero */
799     }
800     else {
801         errno = EDOM;
802         return Py_NAN; /* log2(-inf) = nan, invalid-operation */
803     }
804 }
805 
806 static double
m_log10(double x)807 m_log10(double x)
808 {
809     if (Py_IS_FINITE(x)) {
810         if (x > 0.0)
811             return log10(x);
812         errno = EDOM;
813         if (x == 0.0)
814             return -Py_HUGE_VAL; /* log10(0) = -inf */
815         else
816             return Py_NAN; /* log10(-ve) = nan */
817     }
818     else if (Py_IS_NAN(x))
819         return x; /* log10(nan) = nan */
820     else if (x > 0.0)
821         return x; /* log10(inf) = inf */
822     else {
823         errno = EDOM;
824         return Py_NAN; /* log10(-inf) = nan */
825     }
826 }
827 
828 
829 static PyObject *
math_gcd(PyObject * module,PyObject * const * args,Py_ssize_t nargs)830 math_gcd(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
831 {
832     PyObject *res, *x;
833     Py_ssize_t i;
834 
835     if (nargs == 0) {
836         return PyLong_FromLong(0);
837     }
838     res = PyNumber_Index(args[0]);
839     if (res == NULL) {
840         return NULL;
841     }
842     if (nargs == 1) {
843         Py_SETREF(res, PyNumber_Absolute(res));
844         return res;
845     }
846     for (i = 1; i < nargs; i++) {
847         x = PyNumber_Index(args[i]);
848         if (x == NULL) {
849             Py_DECREF(res);
850             return NULL;
851         }
852         if (res == _PyLong_One) {
853             /* Fast path: just check arguments.
854                It is okay to use identity comparison here. */
855             Py_DECREF(x);
856             continue;
857         }
858         Py_SETREF(res, _PyLong_GCD(res, x));
859         Py_DECREF(x);
860         if (res == NULL) {
861             return NULL;
862         }
863     }
864     return res;
865 }
866 
867 PyDoc_STRVAR(math_gcd_doc,
868 "gcd($module, *integers)\n"
869 "--\n"
870 "\n"
871 "Greatest Common Divisor.");
872 
873 
874 static PyObject *
long_lcm(PyObject * a,PyObject * b)875 long_lcm(PyObject *a, PyObject *b)
876 {
877     PyObject *g, *m, *f, *ab;
878 
879     if (Py_SIZE(a) == 0 || Py_SIZE(b) == 0) {
880         return PyLong_FromLong(0);
881     }
882     g = _PyLong_GCD(a, b);
883     if (g == NULL) {
884         return NULL;
885     }
886     f = PyNumber_FloorDivide(a, g);
887     Py_DECREF(g);
888     if (f == NULL) {
889         return NULL;
890     }
891     m = PyNumber_Multiply(f, b);
892     Py_DECREF(f);
893     if (m == NULL) {
894         return NULL;
895     }
896     ab = PyNumber_Absolute(m);
897     Py_DECREF(m);
898     return ab;
899 }
900 
901 
902 static PyObject *
math_lcm(PyObject * module,PyObject * const * args,Py_ssize_t nargs)903 math_lcm(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
904 {
905     PyObject *res, *x;
906     Py_ssize_t i;
907 
908     if (nargs == 0) {
909         return PyLong_FromLong(1);
910     }
911     res = PyNumber_Index(args[0]);
912     if (res == NULL) {
913         return NULL;
914     }
915     if (nargs == 1) {
916         Py_SETREF(res, PyNumber_Absolute(res));
917         return res;
918     }
919     for (i = 1; i < nargs; i++) {
920         x = PyNumber_Index(args[i]);
921         if (x == NULL) {
922             Py_DECREF(res);
923             return NULL;
924         }
925         if (res == _PyLong_Zero) {
926             /* Fast path: just check arguments.
927                It is okay to use identity comparison here. */
928             Py_DECREF(x);
929             continue;
930         }
931         Py_SETREF(res, long_lcm(res, x));
932         Py_DECREF(x);
933         if (res == NULL) {
934             return NULL;
935         }
936     }
937     return res;
938 }
939 
940 
941 PyDoc_STRVAR(math_lcm_doc,
942 "lcm($module, *integers)\n"
943 "--\n"
944 "\n"
945 "Least Common Multiple.");
946 
947 
948 /* Call is_error when errno != 0, and where x is the result libm
949  * returned.  is_error will usually set up an exception and return
950  * true (1), but may return false (0) without setting up an exception.
951  */
952 static int
is_error(double x)953 is_error(double x)
954 {
955     int result = 1;     /* presumption of guilt */
956     assert(errno);      /* non-zero errno is a precondition for calling */
957     if (errno == EDOM)
958         PyErr_SetString(PyExc_ValueError, "math domain error");
959 
960     else if (errno == ERANGE) {
961         /* ANSI C generally requires libm functions to set ERANGE
962          * on overflow, but also generally *allows* them to set
963          * ERANGE on underflow too.  There's no consistency about
964          * the latter across platforms.
965          * Alas, C99 never requires that errno be set.
966          * Here we suppress the underflow errors (libm functions
967          * should return a zero on underflow, and +- HUGE_VAL on
968          * overflow, so testing the result for zero suffices to
969          * distinguish the cases).
970          *
971          * On some platforms (Ubuntu/ia64) it seems that errno can be
972          * set to ERANGE for subnormal results that do *not* underflow
973          * to zero.  So to be safe, we'll ignore ERANGE whenever the
974          * function result is less than one in absolute value.
975          */
976         if (fabs(x) < 1.0)
977             result = 0;
978         else
979             PyErr_SetString(PyExc_OverflowError,
980                             "math range error");
981     }
982     else
983         /* Unexpected math error */
984         PyErr_SetFromErrno(PyExc_ValueError);
985     return result;
986 }
987 
988 /*
989    math_1 is used to wrap a libm function f that takes a double
990    argument and returns a double.
991 
992    The error reporting follows these rules, which are designed to do
993    the right thing on C89/C99 platforms and IEEE 754/non IEEE 754
994    platforms.
995 
996    - a NaN result from non-NaN inputs causes ValueError to be raised
997    - an infinite result from finite inputs causes OverflowError to be
998      raised if can_overflow is 1, or raises ValueError if can_overflow
999      is 0.
1000    - if the result is finite and errno == EDOM then ValueError is
1001      raised
1002    - if the result is finite and nonzero and errno == ERANGE then
1003      OverflowError is raised
1004 
1005    The last rule is used to catch overflow on platforms which follow
1006    C89 but for which HUGE_VAL is not an infinity.
1007 
1008    For the majority of one-argument functions these rules are enough
1009    to ensure that Python's functions behave as specified in 'Annex F'
1010    of the C99 standard, with the 'invalid' and 'divide-by-zero'
1011    floating-point exceptions mapping to Python's ValueError and the
1012    'overflow' floating-point exception mapping to OverflowError.
1013    math_1 only works for functions that don't have singularities *and*
1014    the possibility of overflow; fortunately, that covers everything we
1015    care about right now.
1016 */
1017 
1018 static PyObject *
math_1_to_whatever(PyObject * arg,double (* func)(double),PyObject * (* from_double_func)(double),int can_overflow)1019 math_1_to_whatever(PyObject *arg, double (*func) (double),
1020                    PyObject *(*from_double_func) (double),
1021                    int can_overflow)
1022 {
1023     double x, r;
1024     x = PyFloat_AsDouble(arg);
1025     if (x == -1.0 && PyErr_Occurred())
1026         return NULL;
1027     errno = 0;
1028     r = (*func)(x);
1029     if (Py_IS_NAN(r) && !Py_IS_NAN(x)) {
1030         PyErr_SetString(PyExc_ValueError,
1031                         "math domain error"); /* invalid arg */
1032         return NULL;
1033     }
1034     if (Py_IS_INFINITY(r) && Py_IS_FINITE(x)) {
1035         if (can_overflow)
1036             PyErr_SetString(PyExc_OverflowError,
1037                             "math range error"); /* overflow */
1038         else
1039             PyErr_SetString(PyExc_ValueError,
1040                             "math domain error"); /* singularity */
1041         return NULL;
1042     }
1043     if (Py_IS_FINITE(r) && errno && is_error(r))
1044         /* this branch unnecessary on most platforms */
1045         return NULL;
1046 
1047     return (*from_double_func)(r);
1048 }
1049 
1050 /* variant of math_1, to be used when the function being wrapped is known to
1051    set errno properly (that is, errno = EDOM for invalid or divide-by-zero,
1052    errno = ERANGE for overflow). */
1053 
1054 static PyObject *
math_1a(PyObject * arg,double (* func)(double))1055 math_1a(PyObject *arg, double (*func) (double))
1056 {
1057     double x, r;
1058     x = PyFloat_AsDouble(arg);
1059     if (x == -1.0 && PyErr_Occurred())
1060         return NULL;
1061     errno = 0;
1062     r = (*func)(x);
1063     if (errno && is_error(r))
1064         return NULL;
1065     return PyFloat_FromDouble(r);
1066 }
1067 
1068 /*
1069    math_2 is used to wrap a libm function f that takes two double
1070    arguments and returns a double.
1071 
1072    The error reporting follows these rules, which are designed to do
1073    the right thing on C89/C99 platforms and IEEE 754/non IEEE 754
1074    platforms.
1075 
1076    - a NaN result from non-NaN inputs causes ValueError to be raised
1077    - an infinite result from finite inputs causes OverflowError to be
1078      raised.
1079    - if the result is finite and errno == EDOM then ValueError is
1080      raised
1081    - if the result is finite and nonzero and errno == ERANGE then
1082      OverflowError is raised
1083 
1084    The last rule is used to catch overflow on platforms which follow
1085    C89 but for which HUGE_VAL is not an infinity.
1086 
1087    For most two-argument functions (copysign, fmod, hypot, atan2)
1088    these rules are enough to ensure that Python's functions behave as
1089    specified in 'Annex F' of the C99 standard, with the 'invalid' and
1090    'divide-by-zero' floating-point exceptions mapping to Python's
1091    ValueError and the 'overflow' floating-point exception mapping to
1092    OverflowError.
1093 */
1094 
1095 static PyObject *
math_1(PyObject * arg,double (* func)(double),int can_overflow)1096 math_1(PyObject *arg, double (*func) (double), int can_overflow)
1097 {
1098     return math_1_to_whatever(arg, func, PyFloat_FromDouble, can_overflow);
1099 }
1100 
1101 static PyObject *
math_2(PyObject * const * args,Py_ssize_t nargs,double (* func)(double,double),const char * funcname)1102 math_2(PyObject *const *args, Py_ssize_t nargs,
1103        double (*func) (double, double), const char *funcname)
1104 {
1105     double x, y, r;
1106     if (!_PyArg_CheckPositional(funcname, nargs, 2, 2))
1107         return NULL;
1108     x = PyFloat_AsDouble(args[0]);
1109     if (x == -1.0 && PyErr_Occurred()) {
1110         return NULL;
1111     }
1112     y = PyFloat_AsDouble(args[1]);
1113     if (y == -1.0 && PyErr_Occurred()) {
1114         return NULL;
1115     }
1116     errno = 0;
1117     r = (*func)(x, y);
1118     if (Py_IS_NAN(r)) {
1119         if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
1120             errno = EDOM;
1121         else
1122             errno = 0;
1123     }
1124     else if (Py_IS_INFINITY(r)) {
1125         if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
1126             errno = ERANGE;
1127         else
1128             errno = 0;
1129     }
1130     if (errno && is_error(r))
1131         return NULL;
1132     else
1133         return PyFloat_FromDouble(r);
1134 }
1135 
1136 #define FUNC1(funcname, func, can_overflow, docstring)                  \
1137     static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
1138         return math_1(args, func, can_overflow);                            \
1139     }\
1140     PyDoc_STRVAR(math_##funcname##_doc, docstring);
1141 
1142 #define FUNC1A(funcname, func, docstring)                               \
1143     static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
1144         return math_1a(args, func);                                     \
1145     }\
1146     PyDoc_STRVAR(math_##funcname##_doc, docstring);
1147 
1148 #define FUNC2(funcname, func, docstring) \
1149     static PyObject * math_##funcname(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { \
1150         return math_2(args, nargs, func, #funcname); \
1151     }\
1152     PyDoc_STRVAR(math_##funcname##_doc, docstring);
1153 
1154 FUNC1(acos, acos, 0,
1155       "acos($module, x, /)\n--\n\n"
1156       "Return the arc cosine (measured in radians) of x.\n\n"
1157       "The result is between 0 and pi.")
1158 FUNC1(acosh, m_acosh, 0,
1159       "acosh($module, x, /)\n--\n\n"
1160       "Return the inverse hyperbolic cosine of x.")
1161 FUNC1(asin, asin, 0,
1162       "asin($module, x, /)\n--\n\n"
1163       "Return the arc sine (measured in radians) of x.\n\n"
1164       "The result is between -pi/2 and pi/2.")
1165 FUNC1(asinh, m_asinh, 0,
1166       "asinh($module, x, /)\n--\n\n"
1167       "Return the inverse hyperbolic sine of x.")
1168 FUNC1(atan, atan, 0,
1169       "atan($module, x, /)\n--\n\n"
1170       "Return the arc tangent (measured in radians) of x.\n\n"
1171       "The result is between -pi/2 and pi/2.")
1172 FUNC2(atan2, m_atan2,
1173       "atan2($module, y, x, /)\n--\n\n"
1174       "Return the arc tangent (measured in radians) of y/x.\n\n"
1175       "Unlike atan(y/x), the signs of both x and y are considered.")
1176 FUNC1(atanh, m_atanh, 0,
1177       "atanh($module, x, /)\n--\n\n"
1178       "Return the inverse hyperbolic tangent of x.")
1179 
1180 /*[clinic input]
1181 math.ceil
1182 
1183     x as number: object
1184     /
1185 
1186 Return the ceiling of x as an Integral.
1187 
1188 This is the smallest integer >= x.
1189 [clinic start generated code]*/
1190 
1191 static PyObject *
math_ceil(PyObject * module,PyObject * number)1192 math_ceil(PyObject *module, PyObject *number)
1193 /*[clinic end generated code: output=6c3b8a78bc201c67 input=2725352806399cab]*/
1194 {
1195     _Py_IDENTIFIER(__ceil__);
1196 
1197     if (!PyFloat_CheckExact(number)) {
1198         PyObject *method = _PyObject_LookupSpecial(number, &PyId___ceil__);
1199         if (method != NULL) {
1200             PyObject *result = _PyObject_CallNoArg(method);
1201             Py_DECREF(method);
1202             return result;
1203         }
1204         if (PyErr_Occurred())
1205             return NULL;
1206     }
1207     double x = PyFloat_AsDouble(number);
1208     if (x == -1.0 && PyErr_Occurred())
1209         return NULL;
1210 
1211     return PyLong_FromDouble(ceil(x));
1212 }
1213 
1214 FUNC2(copysign, copysign,
1215       "copysign($module, x, y, /)\n--\n\n"
1216        "Return a float with the magnitude (absolute value) of x but the sign of y.\n\n"
1217       "On platforms that support signed zeros, copysign(1.0, -0.0)\n"
1218       "returns -1.0.\n")
1219 FUNC1(cos, cos, 0,
1220       "cos($module, x, /)\n--\n\n"
1221       "Return the cosine of x (measured in radians).")
1222 FUNC1(cosh, cosh, 1,
1223       "cosh($module, x, /)\n--\n\n"
1224       "Return the hyperbolic cosine of x.")
1225 FUNC1A(erf, m_erf,
1226        "erf($module, x, /)\n--\n\n"
1227        "Error function at x.")
1228 FUNC1A(erfc, m_erfc,
1229        "erfc($module, x, /)\n--\n\n"
1230        "Complementary error function at x.")
1231 FUNC1(exp, exp, 1,
1232       "exp($module, x, /)\n--\n\n"
1233       "Return e raised to the power of x.")
1234 FUNC1(expm1, m_expm1, 1,
1235       "expm1($module, x, /)\n--\n\n"
1236       "Return exp(x)-1.\n\n"
1237       "This function avoids the loss of precision involved in the direct "
1238       "evaluation of exp(x)-1 for small x.")
1239 FUNC1(fabs, fabs, 0,
1240       "fabs($module, x, /)\n--\n\n"
1241       "Return the absolute value of the float x.")
1242 
1243 /*[clinic input]
1244 math.floor
1245 
1246     x as number: object
1247     /
1248 
1249 Return the floor of x as an Integral.
1250 
1251 This is the largest integer <= x.
1252 [clinic start generated code]*/
1253 
1254 static PyObject *
math_floor(PyObject * module,PyObject * number)1255 math_floor(PyObject *module, PyObject *number)
1256 /*[clinic end generated code: output=c6a65c4884884b8a input=63af6b5d7ebcc3d6]*/
1257 {
1258     double x;
1259 
1260     _Py_IDENTIFIER(__floor__);
1261 
1262     if (PyFloat_CheckExact(number)) {
1263         x = PyFloat_AS_DOUBLE(number);
1264     }
1265     else
1266     {
1267         PyObject *method = _PyObject_LookupSpecial(number, &PyId___floor__);
1268         if (method != NULL) {
1269             PyObject *result = _PyObject_CallNoArg(method);
1270             Py_DECREF(method);
1271             return result;
1272         }
1273         if (PyErr_Occurred())
1274             return NULL;
1275         x = PyFloat_AsDouble(number);
1276         if (x == -1.0 && PyErr_Occurred())
1277             return NULL;
1278     }
1279     return PyLong_FromDouble(floor(x));
1280 }
1281 
1282 FUNC1A(gamma, m_tgamma,
1283       "gamma($module, x, /)\n--\n\n"
1284       "Gamma function at x.")
1285 FUNC1A(lgamma, m_lgamma,
1286       "lgamma($module, x, /)\n--\n\n"
1287       "Natural logarithm of absolute value of Gamma function at x.")
1288 FUNC1(log1p, m_log1p, 0,
1289       "log1p($module, x, /)\n--\n\n"
1290       "Return the natural logarithm of 1+x (base e).\n\n"
1291       "The result is computed in a way which is accurate for x near zero.")
1292 FUNC2(remainder, m_remainder,
1293       "remainder($module, x, y, /)\n--\n\n"
1294       "Difference between x and the closest integer multiple of y.\n\n"
1295       "Return x - n*y where n*y is the closest integer multiple of y.\n"
1296       "In the case where x is exactly halfway between two multiples of\n"
1297       "y, the nearest even value of n is used. The result is always exact.")
1298 FUNC1(sin, sin, 0,
1299       "sin($module, x, /)\n--\n\n"
1300       "Return the sine of x (measured in radians).")
1301 FUNC1(sinh, sinh, 1,
1302       "sinh($module, x, /)\n--\n\n"
1303       "Return the hyperbolic sine of x.")
1304 FUNC1(sqrt, sqrt, 0,
1305       "sqrt($module, x, /)\n--\n\n"
1306       "Return the square root of x.")
1307 FUNC1(tan, tan, 0,
1308       "tan($module, x, /)\n--\n\n"
1309       "Return the tangent of x (measured in radians).")
1310 FUNC1(tanh, tanh, 0,
1311       "tanh($module, x, /)\n--\n\n"
1312       "Return the hyperbolic tangent of x.")
1313 
1314 /* Precision summation function as msum() by Raymond Hettinger in
1315    <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090>,
1316    enhanced with the exact partials sum and roundoff from Mark
1317    Dickinson's post at <http://bugs.python.org/file10357/msum4.py>.
1318    See those links for more details, proofs and other references.
1319 
1320    Note 1: IEEE 754R floating point semantics are assumed,
1321    but the current implementation does not re-establish special
1322    value semantics across iterations (i.e. handling -Inf + Inf).
1323 
1324    Note 2:  No provision is made for intermediate overflow handling;
1325    therefore, sum([1e+308, 1e-308, 1e+308]) returns 1e+308 while
1326    sum([1e+308, 1e+308, 1e-308]) raises an OverflowError due to the
1327    overflow of the first partial sum.
1328 
1329    Note 3: The intermediate values lo, yr, and hi are declared volatile so
1330    aggressive compilers won't algebraically reduce lo to always be exactly 0.0.
1331    Also, the volatile declaration forces the values to be stored in memory as
1332    regular doubles instead of extended long precision (80-bit) values.  This
1333    prevents double rounding because any addition or subtraction of two doubles
1334    can be resolved exactly into double-sized hi and lo values.  As long as the
1335    hi value gets forced into a double before yr and lo are computed, the extra
1336    bits in downstream extended precision operations (x87 for example) will be
1337    exactly zero and therefore can be losslessly stored back into a double,
1338    thereby preventing double rounding.
1339 
1340    Note 4: A similar implementation is in Modules/cmathmodule.c.
1341    Be sure to update both when making changes.
1342 
1343    Note 5: The signature of math.fsum() differs from builtins.sum()
1344    because the start argument doesn't make sense in the context of
1345    accurate summation.  Since the partials table is collapsed before
1346    returning a result, sum(seq2, start=sum(seq1)) may not equal the
1347    accurate result returned by sum(itertools.chain(seq1, seq2)).
1348 */
1349 
1350 #define NUM_PARTIALS  32  /* initial partials array size, on stack */
1351 
1352 /* Extend the partials array p[] by doubling its size. */
1353 static int                          /* non-zero on error */
_fsum_realloc(double ** p_ptr,Py_ssize_t n,double * ps,Py_ssize_t * m_ptr)1354 _fsum_realloc(double **p_ptr, Py_ssize_t  n,
1355              double  *ps,    Py_ssize_t *m_ptr)
1356 {
1357     void *v = NULL;
1358     Py_ssize_t m = *m_ptr;
1359 
1360     m += m;  /* double */
1361     if (n < m && (size_t)m < ((size_t)PY_SSIZE_T_MAX / sizeof(double))) {
1362         double *p = *p_ptr;
1363         if (p == ps) {
1364             v = PyMem_Malloc(sizeof(double) * m);
1365             if (v != NULL)
1366                 memcpy(v, ps, sizeof(double) * n);
1367         }
1368         else
1369             v = PyMem_Realloc(p, sizeof(double) * m);
1370     }
1371     if (v == NULL) {        /* size overflow or no memory */
1372         PyErr_SetString(PyExc_MemoryError, "math.fsum partials");
1373         return 1;
1374     }
1375     *p_ptr = (double*) v;
1376     *m_ptr = m;
1377     return 0;
1378 }
1379 
1380 /* Full precision summation of a sequence of floats.
1381 
1382    def msum(iterable):
1383        partials = []  # sorted, non-overlapping partial sums
1384        for x in iterable:
1385            i = 0
1386            for y in partials:
1387                if abs(x) < abs(y):
1388                    x, y = y, x
1389                hi = x + y
1390                lo = y - (hi - x)
1391                if lo:
1392                    partials[i] = lo
1393                    i += 1
1394                x = hi
1395            partials[i:] = [x]
1396        return sum_exact(partials)
1397 
1398    Rounded x+y stored in hi with the roundoff stored in lo.  Together hi+lo
1399    are exactly equal to x+y.  The inner loop applies hi/lo summation to each
1400    partial so that the list of partial sums remains exact.
1401 
1402    Sum_exact() adds the partial sums exactly and correctly rounds the final
1403    result (using the round-half-to-even rule).  The items in partials remain
1404    non-zero, non-special, non-overlapping and strictly increasing in
1405    magnitude, but possibly not all having the same sign.
1406 
1407    Depends on IEEE 754 arithmetic guarantees and half-even rounding.
1408 */
1409 
1410 /*[clinic input]
1411 math.fsum
1412 
1413     seq: object
1414     /
1415 
1416 Return an accurate floating point sum of values in the iterable seq.
1417 
1418 Assumes IEEE-754 floating point arithmetic.
1419 [clinic start generated code]*/
1420 
1421 static PyObject *
math_fsum(PyObject * module,PyObject * seq)1422 math_fsum(PyObject *module, PyObject *seq)
1423 /*[clinic end generated code: output=ba5c672b87fe34fc input=c51b7d8caf6f6e82]*/
1424 {
1425     PyObject *item, *iter, *sum = NULL;
1426     Py_ssize_t i, j, n = 0, m = NUM_PARTIALS;
1427     double x, y, t, ps[NUM_PARTIALS], *p = ps;
1428     double xsave, special_sum = 0.0, inf_sum = 0.0;
1429     volatile double hi, yr, lo;
1430 
1431     iter = PyObject_GetIter(seq);
1432     if (iter == NULL)
1433         return NULL;
1434 
1435     for(;;) {           /* for x in iterable */
1436         assert(0 <= n && n <= m);
1437         assert((m == NUM_PARTIALS && p == ps) ||
1438                (m >  NUM_PARTIALS && p != NULL));
1439 
1440         item = PyIter_Next(iter);
1441         if (item == NULL) {
1442             if (PyErr_Occurred())
1443                 goto _fsum_error;
1444             break;
1445         }
1446         ASSIGN_DOUBLE(x, item, error_with_item);
1447         Py_DECREF(item);
1448 
1449         xsave = x;
1450         for (i = j = 0; j < n; j++) {       /* for y in partials */
1451             y = p[j];
1452             if (fabs(x) < fabs(y)) {
1453                 t = x; x = y; y = t;
1454             }
1455             hi = x + y;
1456             yr = hi - x;
1457             lo = y - yr;
1458             if (lo != 0.0)
1459                 p[i++] = lo;
1460             x = hi;
1461         }
1462 
1463         n = i;                              /* ps[i:] = [x] */
1464         if (x != 0.0) {
1465             if (! Py_IS_FINITE(x)) {
1466                 /* a nonfinite x could arise either as
1467                    a result of intermediate overflow, or
1468                    as a result of a nan or inf in the
1469                    summands */
1470                 if (Py_IS_FINITE(xsave)) {
1471                     PyErr_SetString(PyExc_OverflowError,
1472                           "intermediate overflow in fsum");
1473                     goto _fsum_error;
1474                 }
1475                 if (Py_IS_INFINITY(xsave))
1476                     inf_sum += xsave;
1477                 special_sum += xsave;
1478                 /* reset partials */
1479                 n = 0;
1480             }
1481             else if (n >= m && _fsum_realloc(&p, n, ps, &m))
1482                 goto _fsum_error;
1483             else
1484                 p[n++] = x;
1485         }
1486     }
1487 
1488     if (special_sum != 0.0) {
1489         if (Py_IS_NAN(inf_sum))
1490             PyErr_SetString(PyExc_ValueError,
1491                             "-inf + inf in fsum");
1492         else
1493             sum = PyFloat_FromDouble(special_sum);
1494         goto _fsum_error;
1495     }
1496 
1497     hi = 0.0;
1498     if (n > 0) {
1499         hi = p[--n];
1500         /* sum_exact(ps, hi) from the top, stop when the sum becomes
1501            inexact. */
1502         while (n > 0) {
1503             x = hi;
1504             y = p[--n];
1505             assert(fabs(y) < fabs(x));
1506             hi = x + y;
1507             yr = hi - x;
1508             lo = y - yr;
1509             if (lo != 0.0)
1510                 break;
1511         }
1512         /* Make half-even rounding work across multiple partials.
1513            Needed so that sum([1e-16, 1, 1e16]) will round-up the last
1514            digit to two instead of down to zero (the 1e-16 makes the 1
1515            slightly closer to two).  With a potential 1 ULP rounding
1516            error fixed-up, math.fsum() can guarantee commutativity. */
1517         if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) ||
1518                       (lo > 0.0 && p[n-1] > 0.0))) {
1519             y = lo * 2.0;
1520             x = hi + y;
1521             yr = x - hi;
1522             if (y == yr)
1523                 hi = x;
1524         }
1525     }
1526     sum = PyFloat_FromDouble(hi);
1527 
1528   _fsum_error:
1529     Py_DECREF(iter);
1530     if (p != ps)
1531         PyMem_Free(p);
1532     return sum;
1533 
1534   error_with_item:
1535     Py_DECREF(item);
1536     goto _fsum_error;
1537 }
1538 
1539 #undef NUM_PARTIALS
1540 
1541 
1542 static unsigned long
count_set_bits(unsigned long n)1543 count_set_bits(unsigned long n)
1544 {
1545     unsigned long count = 0;
1546     while (n != 0) {
1547         ++count;
1548         n &= n - 1; /* clear least significant bit */
1549     }
1550     return count;
1551 }
1552 
1553 /* Integer square root
1554 
1555 Given a nonnegative integer `n`, we want to compute the largest integer
1556 `a` for which `a * a <= n`, or equivalently the integer part of the exact
1557 square root of `n`.
1558 
1559 We use an adaptive-precision pure-integer version of Newton's iteration. Given
1560 a positive integer `n`, the algorithm produces at each iteration an integer
1561 approximation `a` to the square root of `n >> s` for some even integer `s`,
1562 with `s` decreasing as the iterations progress. On the final iteration, `s` is
1563 zero and we have an approximation to the square root of `n` itself.
1564 
1565 At every step, the approximation `a` is strictly within 1.0 of the true square
1566 root, so we have
1567 
1568     (a - 1)**2 < (n >> s) < (a + 1)**2
1569 
1570 After the final iteration, a check-and-correct step is needed to determine
1571 whether `a` or `a - 1` gives the desired integer square root of `n`.
1572 
1573 The algorithm is remarkable in its simplicity. There's no need for a
1574 per-iteration check-and-correct step, and termination is straightforward: the
1575 number of iterations is known in advance (it's exactly `floor(log2(log2(n)))`
1576 for `n > 1`). The only tricky part of the correctness proof is in establishing
1577 that the bound `(a - 1)**2 < (n >> s) < (a + 1)**2` is maintained from one
1578 iteration to the next. A sketch of the proof of this is given below.
1579 
1580 In addition to the proof sketch, a formal, computer-verified proof
1581 of correctness (using Lean) of an equivalent recursive algorithm can be found
1582 here:
1583 
1584     https://github.com/mdickinson/snippets/blob/master/proofs/isqrt/src/isqrt.lean
1585 
1586 
1587 Here's Python code equivalent to the C implementation below:
1588 
1589     def isqrt(n):
1590         """
1591         Return the integer part of the square root of the input.
1592         """
1593         n = operator.index(n)
1594 
1595         if n < 0:
1596             raise ValueError("isqrt() argument must be nonnegative")
1597         if n == 0:
1598             return 0
1599 
1600         c = (n.bit_length() - 1) // 2
1601         a = 1
1602         d = 0
1603         for s in reversed(range(c.bit_length())):
1604             # Loop invariant: (a-1)**2 < (n >> 2*(c - d)) < (a+1)**2
1605             e = d
1606             d = c >> s
1607             a = (a << d - e - 1) + (n >> 2*c - e - d + 1) // a
1608 
1609         return a - (a*a > n)
1610 
1611 
1612 Sketch of proof of correctness
1613 ------------------------------
1614 
1615 The delicate part of the correctness proof is showing that the loop invariant
1616 is preserved from one iteration to the next. That is, just before the line
1617 
1618     a = (a << d - e - 1) + (n >> 2*c - e - d + 1) // a
1619 
1620 is executed in the above code, we know that
1621 
1622     (1)  (a - 1)**2 < (n >> 2*(c - e)) < (a + 1)**2.
1623 
1624 (since `e` is always the value of `d` from the previous iteration). We must
1625 prove that after that line is executed, we have
1626 
1627     (a - 1)**2 < (n >> 2*(c - d)) < (a + 1)**2
1628 
1629 To facilitate the proof, we make some changes of notation. Write `m` for
1630 `n >> 2*(c-d)`, and write `b` for the new value of `a`, so
1631 
1632     b = (a << d - e - 1) + (n >> 2*c - e - d + 1) // a
1633 
1634 or equivalently:
1635 
1636     (2)  b = (a << d - e - 1) + (m >> d - e + 1) // a
1637 
1638 Then we can rewrite (1) as:
1639 
1640     (3)  (a - 1)**2 < (m >> 2*(d - e)) < (a + 1)**2
1641 
1642 and we must show that (b - 1)**2 < m < (b + 1)**2.
1643 
1644 From this point on, we switch to mathematical notation, so `/` means exact
1645 division rather than integer division and `^` is used for exponentiation. We
1646 use the `√` symbol for the exact square root. In (3), we can remove the
1647 implicit floor operation to give:
1648 
1649     (4)  (a - 1)^2 < m / 4^(d - e) < (a + 1)^2
1650 
1651 Taking square roots throughout (4), scaling by `2^(d-e)`, and rearranging gives
1652 
1653     (5)  0 <= | 2^(d-e)a - √m | < 2^(d-e)
1654 
1655 Squaring and dividing through by `2^(d-e+1) a` gives
1656 
1657     (6)  0 <= 2^(d-e-1) a + m / (2^(d-e+1) a) - √m < 2^(d-e-1) / a
1658 
1659 We'll show below that `2^(d-e-1) <= a`. Given that, we can replace the
1660 right-hand side of (6) with `1`, and now replacing the central
1661 term `m / (2^(d-e+1) a)` with its floor in (6) gives
1662 
1663     (7) -1 < 2^(d-e-1) a + m // 2^(d-e+1) a - √m < 1
1664 
1665 Or equivalently, from (2):
1666 
1667     (7) -1 < b - √m < 1
1668 
1669 and rearranging gives that `(b-1)^2 < m < (b+1)^2`, which is what we needed
1670 to prove.
1671 
1672 We're not quite done: we still have to prove the inequality `2^(d - e - 1) <=
1673 a` that was used to get line (7) above. From the definition of `c`, we have
1674 `4^c <= n`, which implies
1675 
1676     (8)  4^d <= m
1677 
1678 also, since `e == d >> 1`, `d` is at most `2e + 1`, from which it follows
1679 that `2d - 2e - 1 <= d` and hence that
1680 
1681     (9)  4^(2d - 2e - 1) <= m
1682 
1683 Dividing both sides by `4^(d - e)` gives
1684 
1685     (10)  4^(d - e - 1) <= m / 4^(d - e)
1686 
1687 But we know from (4) that `m / 4^(d-e) < (a + 1)^2`, hence
1688 
1689     (11)  4^(d - e - 1) < (a + 1)^2
1690 
1691 Now taking square roots of both sides and observing that both `2^(d-e-1)` and
1692 `a` are integers gives `2^(d - e - 1) <= a`, which is what we needed. This
1693 completes the proof sketch.
1694 
1695 */
1696 
1697 
1698 /* Approximate square root of a large 64-bit integer.
1699 
1700    Given `n` satisfying `2**62 <= n < 2**64`, return `a`
1701    satisfying `(a - 1)**2 < n < (a + 1)**2`. */
1702 
1703 static uint64_t
_approximate_isqrt(uint64_t n)1704 _approximate_isqrt(uint64_t n)
1705 {
1706     uint32_t u = 1U + (n >> 62);
1707     u = (u << 1) + (n >> 59) / u;
1708     u = (u << 3) + (n >> 53) / u;
1709     u = (u << 7) + (n >> 41) / u;
1710     return (u << 15) + (n >> 17) / u;
1711 }
1712 
1713 /*[clinic input]
1714 math.isqrt
1715 
1716     n: object
1717     /
1718 
1719 Return the integer part of the square root of the input.
1720 [clinic start generated code]*/
1721 
1722 static PyObject *
math_isqrt(PyObject * module,PyObject * n)1723 math_isqrt(PyObject *module, PyObject *n)
1724 /*[clinic end generated code: output=35a6f7f980beab26 input=5b6e7ae4fa6c43d6]*/
1725 {
1726     int a_too_large, c_bit_length;
1727     size_t c, d;
1728     uint64_t m, u;
1729     PyObject *a = NULL, *b;
1730 
1731     n = PyNumber_Index(n);
1732     if (n == NULL) {
1733         return NULL;
1734     }
1735 
1736     if (_PyLong_Sign(n) < 0) {
1737         PyErr_SetString(
1738             PyExc_ValueError,
1739             "isqrt() argument must be nonnegative");
1740         goto error;
1741     }
1742     if (_PyLong_Sign(n) == 0) {
1743         Py_DECREF(n);
1744         return PyLong_FromLong(0);
1745     }
1746 
1747     /* c = (n.bit_length() - 1) // 2 */
1748     c = _PyLong_NumBits(n);
1749     if (c == (size_t)(-1)) {
1750         goto error;
1751     }
1752     c = (c - 1U) / 2U;
1753 
1754     /* Fast path: if c <= 31 then n < 2**64 and we can compute directly with a
1755        fast, almost branch-free algorithm. In the final correction, we use `u*u
1756        - 1 >= m` instead of the simpler `u*u > m` in order to get the correct
1757        result in the corner case where `u=2**32`. */
1758     if (c <= 31U) {
1759         m = (uint64_t)PyLong_AsUnsignedLongLong(n);
1760         Py_DECREF(n);
1761         if (m == (uint64_t)(-1) && PyErr_Occurred()) {
1762             return NULL;
1763         }
1764         u = _approximate_isqrt(m << (62U - 2U*c)) >> (31U - c);
1765         u -= u * u - 1U >= m;
1766         return PyLong_FromUnsignedLongLong((unsigned long long)u);
1767     }
1768 
1769     /* Slow path: n >= 2**64. We perform the first five iterations in C integer
1770        arithmetic, then switch to using Python long integers. */
1771 
1772     /* From n >= 2**64 it follows that c.bit_length() >= 6. */
1773     c_bit_length = 6;
1774     while ((c >> c_bit_length) > 0U) {
1775         ++c_bit_length;
1776     }
1777 
1778     /* Initialise d and a. */
1779     d = c >> (c_bit_length - 5);
1780     b = _PyLong_Rshift(n, 2U*c - 62U);
1781     if (b == NULL) {
1782         goto error;
1783     }
1784     m = (uint64_t)PyLong_AsUnsignedLongLong(b);
1785     Py_DECREF(b);
1786     if (m == (uint64_t)(-1) && PyErr_Occurred()) {
1787         goto error;
1788     }
1789     u = _approximate_isqrt(m) >> (31U - d);
1790     a = PyLong_FromUnsignedLongLong((unsigned long long)u);
1791     if (a == NULL) {
1792         goto error;
1793     }
1794 
1795     for (int s = c_bit_length - 6; s >= 0; --s) {
1796         PyObject *q;
1797         size_t e = d;
1798 
1799         d = c >> s;
1800 
1801         /* q = (n >> 2*c - e - d + 1) // a */
1802         q = _PyLong_Rshift(n, 2U*c - d - e + 1U);
1803         if (q == NULL) {
1804             goto error;
1805         }
1806         Py_SETREF(q, PyNumber_FloorDivide(q, a));
1807         if (q == NULL) {
1808             goto error;
1809         }
1810 
1811         /* a = (a << d - 1 - e) + q */
1812         Py_SETREF(a, _PyLong_Lshift(a, d - 1U - e));
1813         if (a == NULL) {
1814             Py_DECREF(q);
1815             goto error;
1816         }
1817         Py_SETREF(a, PyNumber_Add(a, q));
1818         Py_DECREF(q);
1819         if (a == NULL) {
1820             goto error;
1821         }
1822     }
1823 
1824     /* The correct result is either a or a - 1. Figure out which, and
1825        decrement a if necessary. */
1826 
1827     /* a_too_large = n < a * a */
1828     b = PyNumber_Multiply(a, a);
1829     if (b == NULL) {
1830         goto error;
1831     }
1832     a_too_large = PyObject_RichCompareBool(n, b, Py_LT);
1833     Py_DECREF(b);
1834     if (a_too_large == -1) {
1835         goto error;
1836     }
1837 
1838     if (a_too_large) {
1839         Py_SETREF(a, PyNumber_Subtract(a, _PyLong_One));
1840     }
1841     Py_DECREF(n);
1842     return a;
1843 
1844   error:
1845     Py_XDECREF(a);
1846     Py_DECREF(n);
1847     return NULL;
1848 }
1849 
1850 /* Divide-and-conquer factorial algorithm
1851  *
1852  * Based on the formula and pseudo-code provided at:
1853  * http://www.luschny.de/math/factorial/binarysplitfact.html
1854  *
1855  * Faster algorithms exist, but they're more complicated and depend on
1856  * a fast prime factorization algorithm.
1857  *
1858  * Notes on the algorithm
1859  * ----------------------
1860  *
1861  * factorial(n) is written in the form 2**k * m, with m odd.  k and m are
1862  * computed separately, and then combined using a left shift.
1863  *
1864  * The function factorial_odd_part computes the odd part m (i.e., the greatest
1865  * odd divisor) of factorial(n), using the formula:
1866  *
1867  *   factorial_odd_part(n) =
1868  *
1869  *        product_{i >= 0} product_{0 < j <= n / 2**i, j odd} j
1870  *
1871  * Example: factorial_odd_part(20) =
1872  *
1873  *        (1) *
1874  *        (1) *
1875  *        (1 * 3 * 5) *
1876  *        (1 * 3 * 5 * 7 * 9)
1877  *        (1 * 3 * 5 * 7 * 9 * 11 * 13 * 15 * 17 * 19)
1878  *
1879  * Here i goes from large to small: the first term corresponds to i=4 (any
1880  * larger i gives an empty product), and the last term corresponds to i=0.
1881  * Each term can be computed from the last by multiplying by the extra odd
1882  * numbers required: e.g., to get from the penultimate term to the last one,
1883  * we multiply by (11 * 13 * 15 * 17 * 19).
1884  *
1885  * To see a hint of why this formula works, here are the same numbers as above
1886  * but with the even parts (i.e., the appropriate powers of 2) included.  For
1887  * each subterm in the product for i, we multiply that subterm by 2**i:
1888  *
1889  *   factorial(20) =
1890  *
1891  *        (16) *
1892  *        (8) *
1893  *        (4 * 12 * 20) *
1894  *        (2 * 6 * 10 * 14 * 18) *
1895  *        (1 * 3 * 5 * 7 * 9 * 11 * 13 * 15 * 17 * 19)
1896  *
1897  * The factorial_partial_product function computes the product of all odd j in
1898  * range(start, stop) for given start and stop.  It's used to compute the
1899  * partial products like (11 * 13 * 15 * 17 * 19) in the example above.  It
1900  * operates recursively, repeatedly splitting the range into two roughly equal
1901  * pieces until the subranges are small enough to be computed using only C
1902  * integer arithmetic.
1903  *
1904  * The two-valuation k (i.e., the exponent of the largest power of 2 dividing
1905  * the factorial) is computed independently in the main math_factorial
1906  * function.  By standard results, its value is:
1907  *
1908  *    two_valuation = n//2 + n//4 + n//8 + ....
1909  *
1910  * It can be shown (e.g., by complete induction on n) that two_valuation is
1911  * equal to n - count_set_bits(n), where count_set_bits(n) gives the number of
1912  * '1'-bits in the binary expansion of n.
1913  */
1914 
1915 /* factorial_partial_product: Compute product(range(start, stop, 2)) using
1916  * divide and conquer.  Assumes start and stop are odd and stop > start.
1917  * max_bits must be >= bit_length(stop - 2). */
1918 
1919 static PyObject *
factorial_partial_product(unsigned long start,unsigned long stop,unsigned long max_bits)1920 factorial_partial_product(unsigned long start, unsigned long stop,
1921                           unsigned long max_bits)
1922 {
1923     unsigned long midpoint, num_operands;
1924     PyObject *left = NULL, *right = NULL, *result = NULL;
1925 
1926     /* If the return value will fit an unsigned long, then we can
1927      * multiply in a tight, fast loop where each multiply is O(1).
1928      * Compute an upper bound on the number of bits required to store
1929      * the answer.
1930      *
1931      * Storing some integer z requires floor(lg(z))+1 bits, which is
1932      * conveniently the value returned by bit_length(z).  The
1933      * product x*y will require at most
1934      * bit_length(x) + bit_length(y) bits to store, based
1935      * on the idea that lg product = lg x + lg y.
1936      *
1937      * We know that stop - 2 is the largest number to be multiplied.  From
1938      * there, we have: bit_length(answer) <= num_operands *
1939      * bit_length(stop - 2)
1940      */
1941 
1942     num_operands = (stop - start) / 2;
1943     /* The "num_operands <= 8 * SIZEOF_LONG" check guards against the
1944      * unlikely case of an overflow in num_operands * max_bits. */
1945     if (num_operands <= 8 * SIZEOF_LONG &&
1946         num_operands * max_bits <= 8 * SIZEOF_LONG) {
1947         unsigned long j, total;
1948         for (total = start, j = start + 2; j < stop; j += 2)
1949             total *= j;
1950         return PyLong_FromUnsignedLong(total);
1951     }
1952 
1953     /* find midpoint of range(start, stop), rounded up to next odd number. */
1954     midpoint = (start + num_operands) | 1;
1955     left = factorial_partial_product(start, midpoint,
1956                                      _Py_bit_length(midpoint - 2));
1957     if (left == NULL)
1958         goto error;
1959     right = factorial_partial_product(midpoint, stop, max_bits);
1960     if (right == NULL)
1961         goto error;
1962     result = PyNumber_Multiply(left, right);
1963 
1964   error:
1965     Py_XDECREF(left);
1966     Py_XDECREF(right);
1967     return result;
1968 }
1969 
1970 /* factorial_odd_part:  compute the odd part of factorial(n). */
1971 
1972 static PyObject *
factorial_odd_part(unsigned long n)1973 factorial_odd_part(unsigned long n)
1974 {
1975     long i;
1976     unsigned long v, lower, upper;
1977     PyObject *partial, *tmp, *inner, *outer;
1978 
1979     inner = PyLong_FromLong(1);
1980     if (inner == NULL)
1981         return NULL;
1982     outer = inner;
1983     Py_INCREF(outer);
1984 
1985     upper = 3;
1986     for (i = _Py_bit_length(n) - 2; i >= 0; i--) {
1987         v = n >> i;
1988         if (v <= 2)
1989             continue;
1990         lower = upper;
1991         /* (v + 1) | 1 = least odd integer strictly larger than n / 2**i */
1992         upper = (v + 1) | 1;
1993         /* Here inner is the product of all odd integers j in the range (0,
1994            n/2**(i+1)].  The factorial_partial_product call below gives the
1995            product of all odd integers j in the range (n/2**(i+1), n/2**i]. */
1996         partial = factorial_partial_product(lower, upper, _Py_bit_length(upper-2));
1997         /* inner *= partial */
1998         if (partial == NULL)
1999             goto error;
2000         tmp = PyNumber_Multiply(inner, partial);
2001         Py_DECREF(partial);
2002         if (tmp == NULL)
2003             goto error;
2004         Py_DECREF(inner);
2005         inner = tmp;
2006         /* Now inner is the product of all odd integers j in the range (0,
2007            n/2**i], giving the inner product in the formula above. */
2008 
2009         /* outer *= inner; */
2010         tmp = PyNumber_Multiply(outer, inner);
2011         if (tmp == NULL)
2012             goto error;
2013         Py_DECREF(outer);
2014         outer = tmp;
2015     }
2016     Py_DECREF(inner);
2017     return outer;
2018 
2019   error:
2020     Py_DECREF(outer);
2021     Py_DECREF(inner);
2022     return NULL;
2023 }
2024 
2025 
2026 /* Lookup table for small factorial values */
2027 
2028 static const unsigned long SmallFactorials[] = {
2029     1, 1, 2, 6, 24, 120, 720, 5040, 40320,
2030     362880, 3628800, 39916800, 479001600,
2031 #if SIZEOF_LONG >= 8
2032     6227020800, 87178291200, 1307674368000,
2033     20922789888000, 355687428096000, 6402373705728000,
2034     121645100408832000, 2432902008176640000
2035 #endif
2036 };
2037 
2038 /*[clinic input]
2039 math.factorial
2040 
2041     x as arg: object
2042     /
2043 
2044 Find x!.
2045 
2046 Raise a ValueError if x is negative or non-integral.
2047 [clinic start generated code]*/
2048 
2049 static PyObject *
math_factorial(PyObject * module,PyObject * arg)2050 math_factorial(PyObject *module, PyObject *arg)
2051 /*[clinic end generated code: output=6686f26fae00e9ca input=6d1c8105c0d91fb4]*/
2052 {
2053     long x, two_valuation;
2054     int overflow;
2055     PyObject *result, *odd_part, *pyint_form;
2056 
2057     if (PyFloat_Check(arg)) {
2058         if (PyErr_WarnEx(PyExc_DeprecationWarning,
2059                          "Using factorial() with floats is deprecated",
2060                          1) < 0)
2061         {
2062             return NULL;
2063         }
2064         PyObject *lx;
2065         double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg);
2066         if (!(Py_IS_FINITE(dx) && dx == floor(dx))) {
2067             PyErr_SetString(PyExc_ValueError,
2068                             "factorial() only accepts integral values");
2069             return NULL;
2070         }
2071         lx = PyLong_FromDouble(dx);
2072         if (lx == NULL)
2073             return NULL;
2074         x = PyLong_AsLongAndOverflow(lx, &overflow);
2075         Py_DECREF(lx);
2076     }
2077     else {
2078         pyint_form = PyNumber_Index(arg);
2079         if (pyint_form == NULL) {
2080             return NULL;
2081         }
2082         x = PyLong_AsLongAndOverflow(pyint_form, &overflow);
2083         Py_DECREF(pyint_form);
2084     }
2085 
2086     if (x == -1 && PyErr_Occurred()) {
2087         return NULL;
2088     }
2089     else if (overflow == 1) {
2090         PyErr_Format(PyExc_OverflowError,
2091                      "factorial() argument should not exceed %ld",
2092                      LONG_MAX);
2093         return NULL;
2094     }
2095     else if (overflow == -1 || x < 0) {
2096         PyErr_SetString(PyExc_ValueError,
2097                         "factorial() not defined for negative values");
2098         return NULL;
2099     }
2100 
2101     /* use lookup table if x is small */
2102     if (x < (long)Py_ARRAY_LENGTH(SmallFactorials))
2103         return PyLong_FromUnsignedLong(SmallFactorials[x]);
2104 
2105     /* else express in the form odd_part * 2**two_valuation, and compute as
2106        odd_part << two_valuation. */
2107     odd_part = factorial_odd_part(x);
2108     if (odd_part == NULL)
2109         return NULL;
2110     two_valuation = x - count_set_bits(x);
2111     result = _PyLong_Lshift(odd_part, two_valuation);
2112     Py_DECREF(odd_part);
2113     return result;
2114 }
2115 
2116 
2117 /*[clinic input]
2118 math.trunc
2119 
2120     x: object
2121     /
2122 
2123 Truncates the Real x to the nearest Integral toward 0.
2124 
2125 Uses the __trunc__ magic method.
2126 [clinic start generated code]*/
2127 
2128 static PyObject *
math_trunc(PyObject * module,PyObject * x)2129 math_trunc(PyObject *module, PyObject *x)
2130 /*[clinic end generated code: output=34b9697b707e1031 input=2168b34e0a09134d]*/
2131 {
2132     _Py_IDENTIFIER(__trunc__);
2133     PyObject *trunc, *result;
2134 
2135     if (PyFloat_CheckExact(x)) {
2136         return PyFloat_Type.tp_as_number->nb_int(x);
2137     }
2138 
2139     if (Py_TYPE(x)->tp_dict == NULL) {
2140         if (PyType_Ready(Py_TYPE(x)) < 0)
2141             return NULL;
2142     }
2143 
2144     trunc = _PyObject_LookupSpecial(x, &PyId___trunc__);
2145     if (trunc == NULL) {
2146         if (!PyErr_Occurred())
2147             PyErr_Format(PyExc_TypeError,
2148                          "type %.100s doesn't define __trunc__ method",
2149                          Py_TYPE(x)->tp_name);
2150         return NULL;
2151     }
2152     result = _PyObject_CallNoArg(trunc);
2153     Py_DECREF(trunc);
2154     return result;
2155 }
2156 
2157 
2158 /*[clinic input]
2159 math.frexp
2160 
2161     x: double
2162     /
2163 
2164 Return the mantissa and exponent of x, as pair (m, e).
2165 
2166 m is a float and e is an int, such that x = m * 2.**e.
2167 If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
2168 [clinic start generated code]*/
2169 
2170 static PyObject *
math_frexp_impl(PyObject * module,double x)2171 math_frexp_impl(PyObject *module, double x)
2172 /*[clinic end generated code: output=03e30d252a15ad4a input=96251c9e208bc6e9]*/
2173 {
2174     int i;
2175     /* deal with special cases directly, to sidestep platform
2176        differences */
2177     if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) {
2178         i = 0;
2179     }
2180     else {
2181         x = frexp(x, &i);
2182     }
2183     return Py_BuildValue("(di)", x, i);
2184 }
2185 
2186 
2187 /*[clinic input]
2188 math.ldexp
2189 
2190     x: double
2191     i: object
2192     /
2193 
2194 Return x * (2**i).
2195 
2196 This is essentially the inverse of frexp().
2197 [clinic start generated code]*/
2198 
2199 static PyObject *
math_ldexp_impl(PyObject * module,double x,PyObject * i)2200 math_ldexp_impl(PyObject *module, double x, PyObject *i)
2201 /*[clinic end generated code: output=b6892f3c2df9cc6a input=17d5970c1a40a8c1]*/
2202 {
2203     double r;
2204     long exp;
2205     int overflow;
2206 
2207     if (PyLong_Check(i)) {
2208         /* on overflow, replace exponent with either LONG_MAX
2209            or LONG_MIN, depending on the sign. */
2210         exp = PyLong_AsLongAndOverflow(i, &overflow);
2211         if (exp == -1 && PyErr_Occurred())
2212             return NULL;
2213         if (overflow)
2214             exp = overflow < 0 ? LONG_MIN : LONG_MAX;
2215     }
2216     else {
2217         PyErr_SetString(PyExc_TypeError,
2218                         "Expected an int as second argument to ldexp.");
2219         return NULL;
2220     }
2221 
2222     if (x == 0. || !Py_IS_FINITE(x)) {
2223         /* NaNs, zeros and infinities are returned unchanged */
2224         r = x;
2225         errno = 0;
2226     } else if (exp > INT_MAX) {
2227         /* overflow */
2228         r = copysign(Py_HUGE_VAL, x);
2229         errno = ERANGE;
2230     } else if (exp < INT_MIN) {
2231         /* underflow to +-0 */
2232         r = copysign(0., x);
2233         errno = 0;
2234     } else {
2235         errno = 0;
2236         r = ldexp(x, (int)exp);
2237         if (Py_IS_INFINITY(r))
2238             errno = ERANGE;
2239     }
2240 
2241     if (errno && is_error(r))
2242         return NULL;
2243     return PyFloat_FromDouble(r);
2244 }
2245 
2246 
2247 /*[clinic input]
2248 math.modf
2249 
2250     x: double
2251     /
2252 
2253 Return the fractional and integer parts of x.
2254 
2255 Both results carry the sign of x and are floats.
2256 [clinic start generated code]*/
2257 
2258 static PyObject *
math_modf_impl(PyObject * module,double x)2259 math_modf_impl(PyObject *module, double x)
2260 /*[clinic end generated code: output=90cee0260014c3c0 input=b4cfb6786afd9035]*/
2261 {
2262     double y;
2263     /* some platforms don't do the right thing for NaNs and
2264        infinities, so we take care of special cases directly. */
2265     if (!Py_IS_FINITE(x)) {
2266         if (Py_IS_INFINITY(x))
2267             return Py_BuildValue("(dd)", copysign(0., x), x);
2268         else if (Py_IS_NAN(x))
2269             return Py_BuildValue("(dd)", x, x);
2270     }
2271 
2272     errno = 0;
2273     x = modf(x, &y);
2274     return Py_BuildValue("(dd)", x, y);
2275 }
2276 
2277 
2278 /* A decent logarithm is easy to compute even for huge ints, but libm can't
2279    do that by itself -- loghelper can.  func is log or log10, and name is
2280    "log" or "log10".  Note that overflow of the result isn't possible: an int
2281    can contain no more than INT_MAX * SHIFT bits, so has value certainly less
2282    than 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
2283    small enough to fit in an IEEE single.  log and log10 are even smaller.
2284    However, intermediate overflow is possible for an int if the number of bits
2285    in that int is larger than PY_SSIZE_T_MAX. */
2286 
2287 static PyObject*
loghelper(PyObject * arg,double (* func)(double),const char * funcname)2288 loghelper(PyObject* arg, double (*func)(double), const char *funcname)
2289 {
2290     /* If it is int, do it ourselves. */
2291     if (PyLong_Check(arg)) {
2292         double x, result;
2293         Py_ssize_t e;
2294 
2295         /* Negative or zero inputs give a ValueError. */
2296         if (Py_SIZE(arg) <= 0) {
2297             PyErr_SetString(PyExc_ValueError,
2298                             "math domain error");
2299             return NULL;
2300         }
2301 
2302         x = PyLong_AsDouble(arg);
2303         if (x == -1.0 && PyErr_Occurred()) {
2304             if (!PyErr_ExceptionMatches(PyExc_OverflowError))
2305                 return NULL;
2306             /* Here the conversion to double overflowed, but it's possible
2307                to compute the log anyway.  Clear the exception and continue. */
2308             PyErr_Clear();
2309             x = _PyLong_Frexp((PyLongObject *)arg, &e);
2310             if (x == -1.0 && PyErr_Occurred())
2311                 return NULL;
2312             /* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */
2313             result = func(x) + func(2.0) * e;
2314         }
2315         else
2316             /* Successfully converted x to a double. */
2317             result = func(x);
2318         return PyFloat_FromDouble(result);
2319     }
2320 
2321     /* Else let libm handle it by itself. */
2322     return math_1(arg, func, 0);
2323 }
2324 
2325 
2326 /*[clinic input]
2327 math.log
2328 
2329     x:    object
2330     [
2331     base: object(c_default="NULL") = math.e
2332     ]
2333     /
2334 
2335 Return the logarithm of x to the given base.
2336 
2337 If the base not specified, returns the natural logarithm (base e) of x.
2338 [clinic start generated code]*/
2339 
2340 static PyObject *
math_log_impl(PyObject * module,PyObject * x,int group_right_1,PyObject * base)2341 math_log_impl(PyObject *module, PyObject *x, int group_right_1,
2342               PyObject *base)
2343 /*[clinic end generated code: output=7b5a39e526b73fc9 input=0f62d5726cbfebbd]*/
2344 {
2345     PyObject *num, *den;
2346     PyObject *ans;
2347 
2348     num = loghelper(x, m_log, "log");
2349     if (num == NULL || base == NULL)
2350         return num;
2351 
2352     den = loghelper(base, m_log, "log");
2353     if (den == NULL) {
2354         Py_DECREF(num);
2355         return NULL;
2356     }
2357 
2358     ans = PyNumber_TrueDivide(num, den);
2359     Py_DECREF(num);
2360     Py_DECREF(den);
2361     return ans;
2362 }
2363 
2364 
2365 /*[clinic input]
2366 math.log2
2367 
2368     x: object
2369     /
2370 
2371 Return the base 2 logarithm of x.
2372 [clinic start generated code]*/
2373 
2374 static PyObject *
math_log2(PyObject * module,PyObject * x)2375 math_log2(PyObject *module, PyObject *x)
2376 /*[clinic end generated code: output=5425899a4d5d6acb input=08321262bae4f39b]*/
2377 {
2378     return loghelper(x, m_log2, "log2");
2379 }
2380 
2381 
2382 /*[clinic input]
2383 math.log10
2384 
2385     x: object
2386     /
2387 
2388 Return the base 10 logarithm of x.
2389 [clinic start generated code]*/
2390 
2391 static PyObject *
math_log10(PyObject * module,PyObject * x)2392 math_log10(PyObject *module, PyObject *x)
2393 /*[clinic end generated code: output=be72a64617df9c6f input=b2469d02c6469e53]*/
2394 {
2395     return loghelper(x, m_log10, "log10");
2396 }
2397 
2398 
2399 /*[clinic input]
2400 math.fmod
2401 
2402     x: double
2403     y: double
2404     /
2405 
2406 Return fmod(x, y), according to platform C.
2407 
2408 x % y may differ.
2409 [clinic start generated code]*/
2410 
2411 static PyObject *
math_fmod_impl(PyObject * module,double x,double y)2412 math_fmod_impl(PyObject *module, double x, double y)
2413 /*[clinic end generated code: output=7559d794343a27b5 input=4f84caa8cfc26a03]*/
2414 {
2415     double r;
2416     /* fmod(x, +/-Inf) returns x for finite x. */
2417     if (Py_IS_INFINITY(y) && Py_IS_FINITE(x))
2418         return PyFloat_FromDouble(x);
2419     errno = 0;
2420     r = fmod(x, y);
2421     if (Py_IS_NAN(r)) {
2422         if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
2423             errno = EDOM;
2424         else
2425             errno = 0;
2426     }
2427     if (errno && is_error(r))
2428         return NULL;
2429     else
2430         return PyFloat_FromDouble(r);
2431 }
2432 
2433 /*
2434 Given an *n* length *vec* of values and a value *max*, compute:
2435 
2436     max * sqrt(sum((x / max) ** 2 for x in vec))
2437 
2438 The value of the *max* variable must be non-negative and
2439 equal to the absolute value of the largest magnitude
2440 entry in the vector.  If n==0, then *max* should be 0.0.
2441 If an infinity is present in the vec, *max* should be INF.
2442 
2443 The *found_nan* variable indicates whether some member of
2444 the *vec* is a NaN.
2445 
2446 To improve accuracy and to increase the number of cases where
2447 vector_norm() is commutative, we use a variant of Neumaier
2448 summation specialized to exploit that we always know that
2449 |csum| >= |x|.
2450 
2451 The *csum* variable tracks the cumulative sum and *frac* tracks
2452 the cumulative fractional errors at each step.  Since this
2453 variant assumes that |csum| >= |x| at each step, we establish
2454 the precondition by starting the accumulation from 1.0 which
2455 represents the largest possible value of (x/max)**2.
2456 
2457 After the loop is finished, the initial 1.0 is subtracted out
2458 for a net zero effect on the final sum.  Since *csum* will be
2459 greater than 1.0, the subtraction of 1.0 will not cause
2460 fractional digits to be dropped from *csum*.
2461 
2462 */
2463 
2464 static inline double
vector_norm(Py_ssize_t n,double * vec,double max,int found_nan)2465 vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
2466 {
2467     double x, csum = 1.0, oldcsum, frac = 0.0;
2468     Py_ssize_t i;
2469 
2470     if (Py_IS_INFINITY(max)) {
2471         return max;
2472     }
2473     if (found_nan) {
2474         return Py_NAN;
2475     }
2476     if (max == 0.0 || n <= 1) {
2477         return max;
2478     }
2479     for (i=0 ; i < n ; i++) {
2480         x = vec[i];
2481         assert(Py_IS_FINITE(x) && fabs(x) <= max);
2482         x /= max;
2483         x = x*x;
2484         oldcsum = csum;
2485         csum += x;
2486         assert(csum >= x);
2487         frac += (oldcsum - csum) + x;
2488     }
2489     return max * sqrt(csum - 1.0 + frac);
2490 }
2491 
2492 #define NUM_STACK_ELEMS 16
2493 
2494 /*[clinic input]
2495 math.dist
2496 
2497     p: object
2498     q: object
2499     /
2500 
2501 Return the Euclidean distance between two points p and q.
2502 
2503 The points should be specified as sequences (or iterables) of
2504 coordinates.  Both inputs must have the same dimension.
2505 
2506 Roughly equivalent to:
2507     sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
2508 [clinic start generated code]*/
2509 
2510 static PyObject *
math_dist_impl(PyObject * module,PyObject * p,PyObject * q)2511 math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
2512 /*[clinic end generated code: output=56bd9538d06bbcfe input=74e85e1b6092e68e]*/
2513 {
2514     PyObject *item;
2515     double max = 0.0;
2516     double x, px, qx, result;
2517     Py_ssize_t i, m, n;
2518     int found_nan = 0, p_allocated = 0, q_allocated = 0;
2519     double diffs_on_stack[NUM_STACK_ELEMS];
2520     double *diffs = diffs_on_stack;
2521 
2522     if (!PyTuple_Check(p)) {
2523         p = PySequence_Tuple(p);
2524         if (p == NULL) {
2525             return NULL;
2526         }
2527         p_allocated = 1;
2528     }
2529     if (!PyTuple_Check(q)) {
2530         q = PySequence_Tuple(q);
2531         if (q == NULL) {
2532             if (p_allocated) {
2533                 Py_DECREF(p);
2534             }
2535             return NULL;
2536         }
2537         q_allocated = 1;
2538     }
2539 
2540     m = PyTuple_GET_SIZE(p);
2541     n = PyTuple_GET_SIZE(q);
2542     if (m != n) {
2543         PyErr_SetString(PyExc_ValueError,
2544                         "both points must have the same number of dimensions");
2545         return NULL;
2546 
2547     }
2548     if (n > NUM_STACK_ELEMS) {
2549         diffs = (double *) PyObject_Malloc(n * sizeof(double));
2550         if (diffs == NULL) {
2551             return PyErr_NoMemory();
2552         }
2553     }
2554     for (i=0 ; i<n ; i++) {
2555         item = PyTuple_GET_ITEM(p, i);
2556         ASSIGN_DOUBLE(px, item, error_exit);
2557         item = PyTuple_GET_ITEM(q, i);
2558         ASSIGN_DOUBLE(qx, item, error_exit);
2559         x = fabs(px - qx);
2560         diffs[i] = x;
2561         found_nan |= Py_IS_NAN(x);
2562         if (x > max) {
2563             max = x;
2564         }
2565     }
2566     result = vector_norm(n, diffs, max, found_nan);
2567     if (diffs != diffs_on_stack) {
2568         PyObject_Free(diffs);
2569     }
2570     if (p_allocated) {
2571         Py_DECREF(p);
2572     }
2573     if (q_allocated) {
2574         Py_DECREF(q);
2575     }
2576     return PyFloat_FromDouble(result);
2577 
2578   error_exit:
2579     if (diffs != diffs_on_stack) {
2580         PyObject_Free(diffs);
2581     }
2582     if (p_allocated) {
2583         Py_DECREF(p);
2584     }
2585     if (q_allocated) {
2586         Py_DECREF(q);
2587     }
2588     return NULL;
2589 }
2590 
2591 /* AC: cannot convert yet, waiting for *args support */
2592 static PyObject *
math_hypot(PyObject * self,PyObject * const * args,Py_ssize_t nargs)2593 math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
2594 {
2595     Py_ssize_t i;
2596     PyObject *item;
2597     double max = 0.0;
2598     double x, result;
2599     int found_nan = 0;
2600     double coord_on_stack[NUM_STACK_ELEMS];
2601     double *coordinates = coord_on_stack;
2602 
2603     if (nargs > NUM_STACK_ELEMS) {
2604         coordinates = (double *) PyObject_Malloc(nargs * sizeof(double));
2605         if (coordinates == NULL) {
2606             return PyErr_NoMemory();
2607         }
2608     }
2609     for (i = 0; i < nargs; i++) {
2610         item = args[i];
2611         ASSIGN_DOUBLE(x, item, error_exit);
2612         x = fabs(x);
2613         coordinates[i] = x;
2614         found_nan |= Py_IS_NAN(x);
2615         if (x > max) {
2616             max = x;
2617         }
2618     }
2619     result = vector_norm(nargs, coordinates, max, found_nan);
2620     if (coordinates != coord_on_stack) {
2621         PyObject_Free(coordinates);
2622     }
2623     return PyFloat_FromDouble(result);
2624 
2625   error_exit:
2626     if (coordinates != coord_on_stack) {
2627         PyObject_Free(coordinates);
2628     }
2629     return NULL;
2630 }
2631 
2632 #undef NUM_STACK_ELEMS
2633 
2634 PyDoc_STRVAR(math_hypot_doc,
2635              "hypot(*coordinates) -> value\n\n\
2636 Multidimensional Euclidean distance from the origin to a point.\n\
2637 \n\
2638 Roughly equivalent to:\n\
2639     sqrt(sum(x**2 for x in coordinates))\n\
2640 \n\
2641 For a two dimensional point (x, y), gives the hypotenuse\n\
2642 using the Pythagorean theorem:  sqrt(x*x + y*y).\n\
2643 \n\
2644 For example, the hypotenuse of a 3/4/5 right triangle is:\n\
2645 \n\
2646     >>> hypot(3.0, 4.0)\n\
2647     5.0\n\
2648 ");
2649 
2650 /* pow can't use math_2, but needs its own wrapper: the problem is
2651    that an infinite result can arise either as a result of overflow
2652    (in which case OverflowError should be raised) or as a result of
2653    e.g. 0.**-5. (for which ValueError needs to be raised.)
2654 */
2655 
2656 /*[clinic input]
2657 math.pow
2658 
2659     x: double
2660     y: double
2661     /
2662 
2663 Return x**y (x to the power of y).
2664 [clinic start generated code]*/
2665 
2666 static PyObject *
math_pow_impl(PyObject * module,double x,double y)2667 math_pow_impl(PyObject *module, double x, double y)
2668 /*[clinic end generated code: output=fff93e65abccd6b0 input=c26f1f6075088bfd]*/
2669 {
2670     double r;
2671     int odd_y;
2672 
2673     /* deal directly with IEEE specials, to cope with problems on various
2674        platforms whose semantics don't exactly match C99 */
2675     r = 0.; /* silence compiler warning */
2676     if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) {
2677         errno = 0;
2678         if (Py_IS_NAN(x))
2679             r = y == 0. ? 1. : x; /* NaN**0 = 1 */
2680         else if (Py_IS_NAN(y))
2681             r = x == 1. ? 1. : y; /* 1**NaN = 1 */
2682         else if (Py_IS_INFINITY(x)) {
2683             odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0;
2684             if (y > 0.)
2685                 r = odd_y ? x : fabs(x);
2686             else if (y == 0.)
2687                 r = 1.;
2688             else /* y < 0. */
2689                 r = odd_y ? copysign(0., x) : 0.;
2690         }
2691         else if (Py_IS_INFINITY(y)) {
2692             if (fabs(x) == 1.0)
2693                 r = 1.;
2694             else if (y > 0. && fabs(x) > 1.0)
2695                 r = y;
2696             else if (y < 0. && fabs(x) < 1.0) {
2697                 r = -y; /* result is +inf */
2698                 if (x == 0.) /* 0**-inf: divide-by-zero */
2699                     errno = EDOM;
2700             }
2701             else
2702                 r = 0.;
2703         }
2704     }
2705     else {
2706         /* let libm handle finite**finite */
2707         errno = 0;
2708         r = pow(x, y);
2709         /* a NaN result should arise only from (-ve)**(finite
2710            non-integer); in this case we want to raise ValueError. */
2711         if (!Py_IS_FINITE(r)) {
2712             if (Py_IS_NAN(r)) {
2713                 errno = EDOM;
2714             }
2715             /*
2716                an infinite result here arises either from:
2717                (A) (+/-0.)**negative (-> divide-by-zero)
2718                (B) overflow of x**y with x and y finite
2719             */
2720             else if (Py_IS_INFINITY(r)) {
2721                 if (x == 0.)
2722                     errno = EDOM;
2723                 else
2724                     errno = ERANGE;
2725             }
2726         }
2727     }
2728 
2729     if (errno && is_error(r))
2730         return NULL;
2731     else
2732         return PyFloat_FromDouble(r);
2733 }
2734 
2735 
2736 static const double degToRad = Py_MATH_PI / 180.0;
2737 static const double radToDeg = 180.0 / Py_MATH_PI;
2738 
2739 /*[clinic input]
2740 math.degrees
2741 
2742     x: double
2743     /
2744 
2745 Convert angle x from radians to degrees.
2746 [clinic start generated code]*/
2747 
2748 static PyObject *
math_degrees_impl(PyObject * module,double x)2749 math_degrees_impl(PyObject *module, double x)
2750 /*[clinic end generated code: output=7fea78b294acd12f input=81e016555d6e3660]*/
2751 {
2752     return PyFloat_FromDouble(x * radToDeg);
2753 }
2754 
2755 
2756 /*[clinic input]
2757 math.radians
2758 
2759     x: double
2760     /
2761 
2762 Convert angle x from degrees to radians.
2763 [clinic start generated code]*/
2764 
2765 static PyObject *
math_radians_impl(PyObject * module,double x)2766 math_radians_impl(PyObject *module, double x)
2767 /*[clinic end generated code: output=34daa47caf9b1590 input=91626fc489fe3d63]*/
2768 {
2769     return PyFloat_FromDouble(x * degToRad);
2770 }
2771 
2772 
2773 /*[clinic input]
2774 math.isfinite
2775 
2776     x: double
2777     /
2778 
2779 Return True if x is neither an infinity nor a NaN, and False otherwise.
2780 [clinic start generated code]*/
2781 
2782 static PyObject *
math_isfinite_impl(PyObject * module,double x)2783 math_isfinite_impl(PyObject *module, double x)
2784 /*[clinic end generated code: output=8ba1f396440c9901 input=46967d254812e54a]*/
2785 {
2786     return PyBool_FromLong((long)Py_IS_FINITE(x));
2787 }
2788 
2789 
2790 /*[clinic input]
2791 math.isnan
2792 
2793     x: double
2794     /
2795 
2796 Return True if x is a NaN (not a number), and False otherwise.
2797 [clinic start generated code]*/
2798 
2799 static PyObject *
math_isnan_impl(PyObject * module,double x)2800 math_isnan_impl(PyObject *module, double x)
2801 /*[clinic end generated code: output=f537b4d6df878c3e input=935891e66083f46a]*/
2802 {
2803     return PyBool_FromLong((long)Py_IS_NAN(x));
2804 }
2805 
2806 
2807 /*[clinic input]
2808 math.isinf
2809 
2810     x: double
2811     /
2812 
2813 Return True if x is a positive or negative infinity, and False otherwise.
2814 [clinic start generated code]*/
2815 
2816 static PyObject *
math_isinf_impl(PyObject * module,double x)2817 math_isinf_impl(PyObject *module, double x)
2818 /*[clinic end generated code: output=9f00cbec4de7b06b input=32630e4212cf961f]*/
2819 {
2820     return PyBool_FromLong((long)Py_IS_INFINITY(x));
2821 }
2822 
2823 
2824 /*[clinic input]
2825 math.isclose -> bool
2826 
2827     a: double
2828     b: double
2829     *
2830     rel_tol: double = 1e-09
2831         maximum difference for being considered "close", relative to the
2832         magnitude of the input values
2833     abs_tol: double = 0.0
2834         maximum difference for being considered "close", regardless of the
2835         magnitude of the input values
2836 
2837 Determine whether two floating point numbers are close in value.
2838 
2839 Return True if a is close in value to b, and False otherwise.
2840 
2841 For the values to be considered close, the difference between them
2842 must be smaller than at least one of the tolerances.
2843 
2844 -inf, inf and NaN behave similarly to the IEEE 754 Standard.  That
2845 is, NaN is not close to anything, even itself.  inf and -inf are
2846 only close to themselves.
2847 [clinic start generated code]*/
2848 
2849 static int
math_isclose_impl(PyObject * module,double a,double b,double rel_tol,double abs_tol)2850 math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
2851                   double abs_tol)
2852 /*[clinic end generated code: output=b73070207511952d input=f28671871ea5bfba]*/
2853 {
2854     double diff = 0.0;
2855 
2856     /* sanity check on the inputs */
2857     if (rel_tol < 0.0 || abs_tol < 0.0 ) {
2858         PyErr_SetString(PyExc_ValueError,
2859                         "tolerances must be non-negative");
2860         return -1;
2861     }
2862 
2863     if ( a == b ) {
2864         /* short circuit exact equality -- needed to catch two infinities of
2865            the same sign. And perhaps speeds things up a bit sometimes.
2866         */
2867         return 1;
2868     }
2869 
2870     /* This catches the case of two infinities of opposite sign, or
2871        one infinity and one finite number. Two infinities of opposite
2872        sign would otherwise have an infinite relative tolerance.
2873        Two infinities of the same sign are caught by the equality check
2874        above.
2875     */
2876 
2877     if (Py_IS_INFINITY(a) || Py_IS_INFINITY(b)) {
2878         return 0;
2879     }
2880 
2881     /* now do the regular computation
2882        this is essentially the "weak" test from the Boost library
2883     */
2884 
2885     diff = fabs(b - a);
2886 
2887     return (((diff <= fabs(rel_tol * b)) ||
2888              (diff <= fabs(rel_tol * a))) ||
2889             (diff <= abs_tol));
2890 }
2891 
2892 static inline int
_check_long_mult_overflow(long a,long b)2893 _check_long_mult_overflow(long a, long b) {
2894 
2895     /* From Python2's int_mul code:
2896 
2897     Integer overflow checking for * is painful:  Python tried a couple ways, but
2898     they didn't work on all platforms, or failed in endcases (a product of
2899     -sys.maxint-1 has been a particular pain).
2900 
2901     Here's another way:
2902 
2903     The native long product x*y is either exactly right or *way* off, being
2904     just the last n bits of the true product, where n is the number of bits
2905     in a long (the delivered product is the true product plus i*2**n for
2906     some integer i).
2907 
2908     The native double product (double)x * (double)y is subject to three
2909     rounding errors:  on a sizeof(long)==8 box, each cast to double can lose
2910     info, and even on a sizeof(long)==4 box, the multiplication can lose info.
2911     But, unlike the native long product, it's not in *range* trouble:  even
2912     if sizeof(long)==32 (256-bit longs), the product easily fits in the
2913     dynamic range of a double.  So the leading 50 (or so) bits of the double
2914     product are correct.
2915 
2916     We check these two ways against each other, and declare victory if they're
2917     approximately the same.  Else, because the native long product is the only
2918     one that can lose catastrophic amounts of information, it's the native long
2919     product that must have overflowed.
2920 
2921     */
2922 
2923     long longprod = (long)((unsigned long)a * b);
2924     double doubleprod = (double)a * (double)b;
2925     double doubled_longprod = (double)longprod;
2926 
2927     if (doubled_longprod == doubleprod) {
2928         return 0;
2929     }
2930 
2931     const double diff = doubled_longprod - doubleprod;
2932     const double absdiff = diff >= 0.0 ? diff : -diff;
2933     const double absprod = doubleprod >= 0.0 ? doubleprod : -doubleprod;
2934 
2935     if (32.0 * absdiff <= absprod) {
2936         return 0;
2937     }
2938 
2939     return 1;
2940 }
2941 
2942 /*[clinic input]
2943 math.prod
2944 
2945     iterable: object
2946     /
2947     *
2948     start: object(c_default="NULL") = 1
2949 
2950 Calculate the product of all the elements in the input iterable.
2951 
2952 The default start value for the product is 1.
2953 
2954 When the iterable is empty, return the start value.  This function is
2955 intended specifically for use with numeric values and may reject
2956 non-numeric types.
2957 [clinic start generated code]*/
2958 
2959 static PyObject *
math_prod_impl(PyObject * module,PyObject * iterable,PyObject * start)2960 math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
2961 /*[clinic end generated code: output=36153bedac74a198 input=4c5ab0682782ed54]*/
2962 {
2963     PyObject *result = start;
2964     PyObject *temp, *item, *iter;
2965 
2966     iter = PyObject_GetIter(iterable);
2967     if (iter == NULL) {
2968         return NULL;
2969     }
2970 
2971     if (result == NULL) {
2972         result = PyLong_FromLong(1);
2973         if (result == NULL) {
2974             Py_DECREF(iter);
2975             return NULL;
2976         }
2977     } else {
2978         Py_INCREF(result);
2979     }
2980 #ifndef SLOW_PROD
2981     /* Fast paths for integers keeping temporary products in C.
2982      * Assumes all inputs are the same type.
2983      * If the assumption fails, default to use PyObjects instead.
2984     */
2985     if (PyLong_CheckExact(result)) {
2986         int overflow;
2987         long i_result = PyLong_AsLongAndOverflow(result, &overflow);
2988         /* If this already overflowed, don't even enter the loop. */
2989         if (overflow == 0) {
2990             Py_DECREF(result);
2991             result = NULL;
2992         }
2993         /* Loop over all the items in the iterable until we finish, we overflow
2994          * or we found a non integer element */
2995         while(result == NULL) {
2996             item = PyIter_Next(iter);
2997             if (item == NULL) {
2998                 Py_DECREF(iter);
2999                 if (PyErr_Occurred()) {
3000                     return NULL;
3001                 }
3002                 return PyLong_FromLong(i_result);
3003             }
3004             if (PyLong_CheckExact(item)) {
3005                 long b = PyLong_AsLongAndOverflow(item, &overflow);
3006                 if (overflow == 0 && !_check_long_mult_overflow(i_result, b)) {
3007                     long x = i_result * b;
3008                     i_result = x;
3009                     Py_DECREF(item);
3010                     continue;
3011                 }
3012             }
3013             /* Either overflowed or is not an int.
3014              * Restore real objects and process normally */
3015             result = PyLong_FromLong(i_result);
3016             if (result == NULL) {
3017                 Py_DECREF(item);
3018                 Py_DECREF(iter);
3019                 return NULL;
3020             }
3021             temp = PyNumber_Multiply(result, item);
3022             Py_DECREF(result);
3023             Py_DECREF(item);
3024             result = temp;
3025             if (result == NULL) {
3026                 Py_DECREF(iter);
3027                 return NULL;
3028             }
3029         }
3030     }
3031 
3032     /* Fast paths for floats keeping temporary products in C.
3033      * Assumes all inputs are the same type.
3034      * If the assumption fails, default to use PyObjects instead.
3035     */
3036     if (PyFloat_CheckExact(result)) {
3037         double f_result = PyFloat_AS_DOUBLE(result);
3038         Py_DECREF(result);
3039         result = NULL;
3040         while(result == NULL) {
3041             item = PyIter_Next(iter);
3042             if (item == NULL) {
3043                 Py_DECREF(iter);
3044                 if (PyErr_Occurred()) {
3045                     return NULL;
3046                 }
3047                 return PyFloat_FromDouble(f_result);
3048             }
3049             if (PyFloat_CheckExact(item)) {
3050                 f_result *= PyFloat_AS_DOUBLE(item);
3051                 Py_DECREF(item);
3052                 continue;
3053             }
3054             if (PyLong_CheckExact(item)) {
3055                 long value;
3056                 int overflow;
3057                 value = PyLong_AsLongAndOverflow(item, &overflow);
3058                 if (!overflow) {
3059                     f_result *= (double)value;
3060                     Py_DECREF(item);
3061                     continue;
3062                 }
3063             }
3064             result = PyFloat_FromDouble(f_result);
3065             if (result == NULL) {
3066                 Py_DECREF(item);
3067                 Py_DECREF(iter);
3068                 return NULL;
3069             }
3070             temp = PyNumber_Multiply(result, item);
3071             Py_DECREF(result);
3072             Py_DECREF(item);
3073             result = temp;
3074             if (result == NULL) {
3075                 Py_DECREF(iter);
3076                 return NULL;
3077             }
3078         }
3079     }
3080 #endif
3081     /* Consume rest of the iterable (if any) that could not be handled
3082      * by specialized functions above.*/
3083     for(;;) {
3084         item = PyIter_Next(iter);
3085         if (item == NULL) {
3086             /* error, or end-of-sequence */
3087             if (PyErr_Occurred()) {
3088                 Py_DECREF(result);
3089                 result = NULL;
3090             }
3091             break;
3092         }
3093         temp = PyNumber_Multiply(result, item);
3094         Py_DECREF(result);
3095         Py_DECREF(item);
3096         result = temp;
3097         if (result == NULL)
3098             break;
3099     }
3100     Py_DECREF(iter);
3101     return result;
3102 }
3103 
3104 
3105 /*[clinic input]
3106 math.perm
3107 
3108     n: object
3109     k: object = None
3110     /
3111 
3112 Number of ways to choose k items from n items without repetition and with order.
3113 
3114 Evaluates to n! / (n - k)! when k <= n and evaluates
3115 to zero when k > n.
3116 
3117 If k is not specified or is None, then k defaults to n
3118 and the function returns n!.
3119 
3120 Raises TypeError if either of the arguments are not integers.
3121 Raises ValueError if either of the arguments are negative.
3122 [clinic start generated code]*/
3123 
3124 static PyObject *
math_perm_impl(PyObject * module,PyObject * n,PyObject * k)3125 math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
3126 /*[clinic end generated code: output=e021a25469653e23 input=5311c5a00f359b53]*/
3127 {
3128     PyObject *result = NULL, *factor = NULL;
3129     int overflow, cmp;
3130     long long i, factors;
3131 
3132     if (k == Py_None) {
3133         return math_factorial(module, n);
3134     }
3135     n = PyNumber_Index(n);
3136     if (n == NULL) {
3137         return NULL;
3138     }
3139     if (!PyLong_CheckExact(n)) {
3140         Py_SETREF(n, _PyLong_Copy((PyLongObject *)n));
3141         if (n == NULL) {
3142             return NULL;
3143         }
3144     }
3145     k = PyNumber_Index(k);
3146     if (k == NULL) {
3147         Py_DECREF(n);
3148         return NULL;
3149     }
3150     if (!PyLong_CheckExact(k)) {
3151         Py_SETREF(k, _PyLong_Copy((PyLongObject *)k));
3152         if (k == NULL) {
3153             Py_DECREF(n);
3154             return NULL;
3155         }
3156     }
3157 
3158     if (Py_SIZE(n) < 0) {
3159         PyErr_SetString(PyExc_ValueError,
3160                         "n must be a non-negative integer");
3161         goto error;
3162     }
3163     if (Py_SIZE(k) < 0) {
3164         PyErr_SetString(PyExc_ValueError,
3165                         "k must be a non-negative integer");
3166         goto error;
3167     }
3168 
3169     cmp = PyObject_RichCompareBool(n, k, Py_LT);
3170     if (cmp != 0) {
3171         if (cmp > 0) {
3172             result = PyLong_FromLong(0);
3173             goto done;
3174         }
3175         goto error;
3176     }
3177 
3178     factors = PyLong_AsLongLongAndOverflow(k, &overflow);
3179     if (overflow > 0) {
3180         PyErr_Format(PyExc_OverflowError,
3181                      "k must not exceed %lld",
3182                      LLONG_MAX);
3183         goto error;
3184     }
3185     else if (factors == -1) {
3186         /* k is nonnegative, so a return value of -1 can only indicate error */
3187         goto error;
3188     }
3189 
3190     if (factors == 0) {
3191         result = PyLong_FromLong(1);
3192         goto done;
3193     }
3194 
3195     result = n;
3196     Py_INCREF(result);
3197     if (factors == 1) {
3198         goto done;
3199     }
3200 
3201     factor = n;
3202     Py_INCREF(factor);
3203     for (i = 1; i < factors; ++i) {
3204         Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_One));
3205         if (factor == NULL) {
3206             goto error;
3207         }
3208         Py_SETREF(result, PyNumber_Multiply(result, factor));
3209         if (result == NULL) {
3210             goto error;
3211         }
3212     }
3213     Py_DECREF(factor);
3214 
3215 done:
3216     Py_DECREF(n);
3217     Py_DECREF(k);
3218     return result;
3219 
3220 error:
3221     Py_XDECREF(factor);
3222     Py_XDECREF(result);
3223     Py_DECREF(n);
3224     Py_DECREF(k);
3225     return NULL;
3226 }
3227 
3228 
3229 /*[clinic input]
3230 math.comb
3231 
3232     n: object
3233     k: object
3234     /
3235 
3236 Number of ways to choose k items from n items without repetition and without order.
3237 
3238 Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates
3239 to zero when k > n.
3240 
3241 Also called the binomial coefficient because it is equivalent
3242 to the coefficient of k-th term in polynomial expansion of the
3243 expression (1 + x)**n.
3244 
3245 Raises TypeError if either of the arguments are not integers.
3246 Raises ValueError if either of the arguments are negative.
3247 
3248 [clinic start generated code]*/
3249 
3250 static PyObject *
math_comb_impl(PyObject * module,PyObject * n,PyObject * k)3251 math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
3252 /*[clinic end generated code: output=bd2cec8d854f3493 input=9a05315af2518709]*/
3253 {
3254     PyObject *result = NULL, *factor = NULL, *temp;
3255     int overflow, cmp;
3256     long long i, factors;
3257 
3258     n = PyNumber_Index(n);
3259     if (n == NULL) {
3260         return NULL;
3261     }
3262     if (!PyLong_CheckExact(n)) {
3263         Py_SETREF(n, _PyLong_Copy((PyLongObject *)n));
3264         if (n == NULL) {
3265             return NULL;
3266         }
3267     }
3268     k = PyNumber_Index(k);
3269     if (k == NULL) {
3270         Py_DECREF(n);
3271         return NULL;
3272     }
3273     if (!PyLong_CheckExact(k)) {
3274         Py_SETREF(k, _PyLong_Copy((PyLongObject *)k));
3275         if (k == NULL) {
3276             Py_DECREF(n);
3277             return NULL;
3278         }
3279     }
3280 
3281     if (Py_SIZE(n) < 0) {
3282         PyErr_SetString(PyExc_ValueError,
3283                         "n must be a non-negative integer");
3284         goto error;
3285     }
3286     if (Py_SIZE(k) < 0) {
3287         PyErr_SetString(PyExc_ValueError,
3288                         "k must be a non-negative integer");
3289         goto error;
3290     }
3291 
3292     /* k = min(k, n - k) */
3293     temp = PyNumber_Subtract(n, k);
3294     if (temp == NULL) {
3295         goto error;
3296     }
3297     if (Py_SIZE(temp) < 0) {
3298         Py_DECREF(temp);
3299         result = PyLong_FromLong(0);
3300         goto done;
3301     }
3302     cmp = PyObject_RichCompareBool(temp, k, Py_LT);
3303     if (cmp > 0) {
3304         Py_SETREF(k, temp);
3305     }
3306     else {
3307         Py_DECREF(temp);
3308         if (cmp < 0) {
3309             goto error;
3310         }
3311     }
3312 
3313     factors = PyLong_AsLongLongAndOverflow(k, &overflow);
3314     if (overflow > 0) {
3315         PyErr_Format(PyExc_OverflowError,
3316                      "min(n - k, k) must not exceed %lld",
3317                      LLONG_MAX);
3318         goto error;
3319     }
3320     if (factors == -1) {
3321         /* k is nonnegative, so a return value of -1 can only indicate error */
3322         goto error;
3323     }
3324 
3325     if (factors == 0) {
3326         result = PyLong_FromLong(1);
3327         goto done;
3328     }
3329 
3330     result = n;
3331     Py_INCREF(result);
3332     if (factors == 1) {
3333         goto done;
3334     }
3335 
3336     factor = n;
3337     Py_INCREF(factor);
3338     for (i = 1; i < factors; ++i) {
3339         Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_One));
3340         if (factor == NULL) {
3341             goto error;
3342         }
3343         Py_SETREF(result, PyNumber_Multiply(result, factor));
3344         if (result == NULL) {
3345             goto error;
3346         }
3347 
3348         temp = PyLong_FromUnsignedLongLong((unsigned long long)i + 1);
3349         if (temp == NULL) {
3350             goto error;
3351         }
3352         Py_SETREF(result, PyNumber_FloorDivide(result, temp));
3353         Py_DECREF(temp);
3354         if (result == NULL) {
3355             goto error;
3356         }
3357     }
3358     Py_DECREF(factor);
3359 
3360 done:
3361     Py_DECREF(n);
3362     Py_DECREF(k);
3363     return result;
3364 
3365 error:
3366     Py_XDECREF(factor);
3367     Py_XDECREF(result);
3368     Py_DECREF(n);
3369     Py_DECREF(k);
3370     return NULL;
3371 }
3372 
3373 
3374 /*[clinic input]
3375 math.nextafter
3376 
3377     x: double
3378     y: double
3379     /
3380 
3381 Return the next floating-point value after x towards y.
3382 [clinic start generated code]*/
3383 
3384 static PyObject *
math_nextafter_impl(PyObject * module,double x,double y)3385 math_nextafter_impl(PyObject *module, double x, double y)
3386 /*[clinic end generated code: output=750c8266c1c540ce input=02b2d50cd1d9f9b6]*/
3387 {
3388 #if defined(_AIX)
3389     if (x == y) {
3390         /* On AIX 7.1, libm nextafter(-0.0, +0.0) returns -0.0.
3391            Bug fixed in bos.adt.libm 7.2.2.0 by APAR IV95512. */
3392         return PyFloat_FromDouble(y);
3393     }
3394 #endif
3395     return PyFloat_FromDouble(nextafter(x, y));
3396 }
3397 
3398 
3399 /*[clinic input]
3400 math.ulp -> double
3401 
3402     x: double
3403     /
3404 
3405 Return the value of the least significant bit of the float x.
3406 [clinic start generated code]*/
3407 
3408 static double
math_ulp_impl(PyObject * module,double x)3409 math_ulp_impl(PyObject *module, double x)
3410 /*[clinic end generated code: output=f5207867a9384dd4 input=31f9bfbbe373fcaa]*/
3411 {
3412     if (Py_IS_NAN(x)) {
3413         return x;
3414     }
3415     x = fabs(x);
3416     if (Py_IS_INFINITY(x)) {
3417         return x;
3418     }
3419     double inf = m_inf();
3420     double x2 = nextafter(x, inf);
3421     if (Py_IS_INFINITY(x2)) {
3422         /* special case: x is the largest positive representable float */
3423         x2 = nextafter(x, -inf);
3424         return x - x2;
3425     }
3426     return x2 - x;
3427 }
3428 
3429 static int
math_exec(PyObject * module)3430 math_exec(PyObject *module)
3431 {
3432     if (PyModule_AddObject(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
3433         return -1;
3434     }
3435     if (PyModule_AddObject(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
3436         return -1;
3437     }
3438     // 2pi
3439     if (PyModule_AddObject(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
3440         return -1;
3441     }
3442     if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(m_inf())) < 0) {
3443         return -1;
3444     }
3445 #if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
3446     if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(m_nan())) < 0) {
3447         return -1;
3448     }
3449 #endif
3450     return 0;
3451 }
3452 
3453 static PyMethodDef math_methods[] = {
3454     {"acos",            math_acos,      METH_O,         math_acos_doc},
3455     {"acosh",           math_acosh,     METH_O,         math_acosh_doc},
3456     {"asin",            math_asin,      METH_O,         math_asin_doc},
3457     {"asinh",           math_asinh,     METH_O,         math_asinh_doc},
3458     {"atan",            math_atan,      METH_O,         math_atan_doc},
3459     {"atan2",           (PyCFunction)(void(*)(void))math_atan2,     METH_FASTCALL,  math_atan2_doc},
3460     {"atanh",           math_atanh,     METH_O,         math_atanh_doc},
3461     MATH_CEIL_METHODDEF
3462     {"copysign",        (PyCFunction)(void(*)(void))math_copysign,  METH_FASTCALL,  math_copysign_doc},
3463     {"cos",             math_cos,       METH_O,         math_cos_doc},
3464     {"cosh",            math_cosh,      METH_O,         math_cosh_doc},
3465     MATH_DEGREES_METHODDEF
3466     MATH_DIST_METHODDEF
3467     {"erf",             math_erf,       METH_O,         math_erf_doc},
3468     {"erfc",            math_erfc,      METH_O,         math_erfc_doc},
3469     {"exp",             math_exp,       METH_O,         math_exp_doc},
3470     {"expm1",           math_expm1,     METH_O,         math_expm1_doc},
3471     {"fabs",            math_fabs,      METH_O,         math_fabs_doc},
3472     MATH_FACTORIAL_METHODDEF
3473     MATH_FLOOR_METHODDEF
3474     MATH_FMOD_METHODDEF
3475     MATH_FREXP_METHODDEF
3476     MATH_FSUM_METHODDEF
3477     {"gamma",           math_gamma,     METH_O,         math_gamma_doc},
3478     {"gcd",             (PyCFunction)(void(*)(void))math_gcd,       METH_FASTCALL,  math_gcd_doc},
3479     {"hypot",           (PyCFunction)(void(*)(void))math_hypot,     METH_FASTCALL,  math_hypot_doc},
3480     MATH_ISCLOSE_METHODDEF
3481     MATH_ISFINITE_METHODDEF
3482     MATH_ISINF_METHODDEF
3483     MATH_ISNAN_METHODDEF
3484     MATH_ISQRT_METHODDEF
3485     {"lcm",             (PyCFunction)(void(*)(void))math_lcm,       METH_FASTCALL,  math_lcm_doc},
3486     MATH_LDEXP_METHODDEF
3487     {"lgamma",          math_lgamma,    METH_O,         math_lgamma_doc},
3488     MATH_LOG_METHODDEF
3489     {"log1p",           math_log1p,     METH_O,         math_log1p_doc},
3490     MATH_LOG10_METHODDEF
3491     MATH_LOG2_METHODDEF
3492     MATH_MODF_METHODDEF
3493     MATH_POW_METHODDEF
3494     MATH_RADIANS_METHODDEF
3495     {"remainder",       (PyCFunction)(void(*)(void))math_remainder, METH_FASTCALL,  math_remainder_doc},
3496     {"sin",             math_sin,       METH_O,         math_sin_doc},
3497     {"sinh",            math_sinh,      METH_O,         math_sinh_doc},
3498     {"sqrt",            math_sqrt,      METH_O,         math_sqrt_doc},
3499     {"tan",             math_tan,       METH_O,         math_tan_doc},
3500     {"tanh",            math_tanh,      METH_O,         math_tanh_doc},
3501     MATH_TRUNC_METHODDEF
3502     MATH_PROD_METHODDEF
3503     MATH_PERM_METHODDEF
3504     MATH_COMB_METHODDEF
3505     MATH_NEXTAFTER_METHODDEF
3506     MATH_ULP_METHODDEF
3507     {NULL,              NULL}           /* sentinel */
3508 };
3509 
3510 static PyModuleDef_Slot math_slots[] = {
3511     {Py_mod_exec, math_exec},
3512     {0, NULL}
3513 };
3514 
3515 PyDoc_STRVAR(module_doc,
3516 "This module provides access to the mathematical functions\n"
3517 "defined by the C standard.");
3518 
3519 static struct PyModuleDef mathmodule = {
3520     PyModuleDef_HEAD_INIT,
3521     .m_name = "math",
3522     .m_doc = module_doc,
3523     .m_size = 0,
3524     .m_methods = math_methods,
3525     .m_slots = math_slots,
3526 };
3527 
3528 PyMODINIT_FUNC
PyInit_math(void)3529 PyInit_math(void)
3530 {
3531     return PyModuleDef_Init(&mathmodule);
3532 }
3533