Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Math module -- standard C math library functions, pi and e */ |
| 2 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 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 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 55 | #include "Python.h" |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 56 | #include "_math.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 58 | #ifdef _OSF_SOURCE |
| 59 | /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */ |
| 60 | extern double copysign(double, double); |
| 61 | #endif |
| 62 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 63 | /* |
| 64 | sin(pi*x), giving accurate results for all finite x (especially x |
| 65 | integral or close to an integer). This is here for use in the |
| 66 | reflection formula for the gamma function. It conforms to IEEE |
| 67 | 754-2008 for finite arguments, but not for infinities or nans. |
| 68 | */ |
Tim Peters | a40c793 | 2001-09-05 22:36:56 +0000 | [diff] [blame] | 69 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 70 | static const double pi = 3.141592653589793238462643383279502884197; |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 71 | static const double sqrtpi = 1.772453850905516027298167483341145182798; |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 72 | |
| 73 | static double |
| 74 | sinpi(double x) |
| 75 | { |
| 76 | double y, r; |
| 77 | int n; |
| 78 | /* this function should only ever be called for finite arguments */ |
| 79 | assert(Py_IS_FINITE(x)); |
| 80 | y = fmod(fabs(x), 2.0); |
| 81 | n = (int)round(2.0*y); |
| 82 | assert(0 <= n && n <= 4); |
| 83 | switch (n) { |
| 84 | case 0: |
| 85 | r = sin(pi*y); |
| 86 | break; |
| 87 | case 1: |
| 88 | r = cos(pi*(y-0.5)); |
| 89 | break; |
| 90 | case 2: |
| 91 | /* N.B. -sin(pi*(y-1.0)) is *not* equivalent: it would give |
| 92 | -0.0 instead of 0.0 when y == 1.0. */ |
| 93 | r = sin(pi*(1.0-y)); |
| 94 | break; |
| 95 | case 3: |
| 96 | r = -cos(pi*(y-1.5)); |
| 97 | break; |
| 98 | case 4: |
| 99 | r = sin(pi*(y-2.0)); |
| 100 | break; |
| 101 | default: |
| 102 | assert(0); /* should never get here */ |
| 103 | r = -1.23e200; /* silence gcc warning */ |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 104 | } |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 105 | return copysign(1.0, x)*r; |
| 106 | } |
| 107 | |
| 108 | /* Implementation of the real gamma function. In extensive but non-exhaustive |
| 109 | random tests, this function proved accurate to within <= 10 ulps across the |
| 110 | entire float domain. Note that accuracy may depend on the quality of the |
| 111 | system math functions, the pow function in particular. Special cases |
| 112 | follow C99 annex F. The parameters and method are tailored to platforms |
| 113 | whose double format is the IEEE 754 binary64 format. |
| 114 | |
| 115 | Method: for x > 0.0 we use the Lanczos approximation with parameters N=13 |
| 116 | and g=6.024680040776729583740234375; these parameters are amongst those |
| 117 | used by the Boost library. Following Boost (again), we re-express the |
| 118 | Lanczos sum as a rational function, and compute it that way. The |
| 119 | coefficients below were computed independently using MPFR, and have been |
| 120 | double-checked against the coefficients in the Boost source code. |
| 121 | |
| 122 | For x < 0.0 we use the reflection formula. |
| 123 | |
| 124 | There's one minor tweak that deserves explanation: Lanczos' formula for |
| 125 | Gamma(x) involves computing pow(x+g-0.5, x-0.5) / exp(x+g-0.5). For many x |
| 126 | values, x+g-0.5 can be represented exactly. However, in cases where it |
| 127 | can't be represented exactly the small error in x+g-0.5 can be magnified |
| 128 | significantly by the pow and exp calls, especially for large x. A cheap |
| 129 | correction is to multiply by (1 + e*g/(x+g-0.5)), where e is the error |
| 130 | involved in the computation of x+g-0.5 (that is, e = computed value of |
| 131 | x+g-0.5 - exact value of x+g-0.5). Here's the proof: |
| 132 | |
| 133 | Correction factor |
| 134 | ----------------- |
| 135 | Write x+g-0.5 = y-e, where y is exactly representable as an IEEE 754 |
| 136 | double, and e is tiny. Then: |
| 137 | |
| 138 | pow(x+g-0.5,x-0.5)/exp(x+g-0.5) = pow(y-e, x-0.5)/exp(y-e) |
| 139 | = pow(y, x-0.5)/exp(y) * C, |
| 140 | |
| 141 | where the correction_factor C is given by |
| 142 | |
| 143 | C = pow(1-e/y, x-0.5) * exp(e) |
| 144 | |
| 145 | Since e is tiny, pow(1-e/y, x-0.5) ~ 1-(x-0.5)*e/y, and exp(x) ~ 1+e, so: |
| 146 | |
| 147 | C ~ (1-(x-0.5)*e/y) * (1+e) ~ 1 + e*(y-(x-0.5))/y |
| 148 | |
| 149 | But y-(x-0.5) = g+e, and g+e ~ g. So we get C ~ 1 + e*g/y, and |
| 150 | |
| 151 | pow(x+g-0.5,x-0.5)/exp(x+g-0.5) ~ pow(y, x-0.5)/exp(y) * (1 + e*g/y), |
| 152 | |
| 153 | Note that for accuracy, when computing r*C it's better to do |
| 154 | |
| 155 | r + e*g/y*r; |
| 156 | |
| 157 | than |
| 158 | |
| 159 | r * (1 + e*g/y); |
| 160 | |
| 161 | since the addition in the latter throws away most of the bits of |
| 162 | information in e*g/y. |
| 163 | */ |
| 164 | |
| 165 | #define LANCZOS_N 13 |
| 166 | static const double lanczos_g = 6.024680040776729583740234375; |
| 167 | static const double lanczos_g_minus_half = 5.524680040776729583740234375; |
| 168 | static const double lanczos_num_coeffs[LANCZOS_N] = { |
| 169 | 23531376880.410759688572007674451636754734846804940, |
| 170 | 42919803642.649098768957899047001988850926355848959, |
| 171 | 35711959237.355668049440185451547166705960488635843, |
| 172 | 17921034426.037209699919755754458931112671403265390, |
| 173 | 6039542586.3520280050642916443072979210699388420708, |
| 174 | 1439720407.3117216736632230727949123939715485786772, |
| 175 | 248874557.86205415651146038641322942321632125127801, |
| 176 | 31426415.585400194380614231628318205362874684987640, |
| 177 | 2876370.6289353724412254090516208496135991145378768, |
| 178 | 186056.26539522349504029498971604569928220784236328, |
| 179 | 8071.6720023658162106380029022722506138218516325024, |
| 180 | 210.82427775157934587250973392071336271166969580291, |
| 181 | 2.5066282746310002701649081771338373386264310793408 |
| 182 | }; |
| 183 | |
| 184 | /* denominator is x*(x+1)*...*(x+LANCZOS_N-2) */ |
| 185 | static const double lanczos_den_coeffs[LANCZOS_N] = { |
| 186 | 0.0, 39916800.0, 120543840.0, 150917976.0, 105258076.0, 45995730.0, |
| 187 | 13339535.0, 2637558.0, 357423.0, 32670.0, 1925.0, 66.0, 1.0}; |
| 188 | |
| 189 | /* gamma values for small positive integers, 1 though NGAMMA_INTEGRAL */ |
| 190 | #define NGAMMA_INTEGRAL 23 |
| 191 | static const double gamma_integral[NGAMMA_INTEGRAL] = { |
| 192 | 1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0, 5040.0, 40320.0, 362880.0, |
| 193 | 3628800.0, 39916800.0, 479001600.0, 6227020800.0, 87178291200.0, |
| 194 | 1307674368000.0, 20922789888000.0, 355687428096000.0, |
| 195 | 6402373705728000.0, 121645100408832000.0, 2432902008176640000.0, |
| 196 | 51090942171709440000.0, 1124000727777607680000.0, |
| 197 | }; |
| 198 | |
| 199 | /* Lanczos' sum L_g(x), for positive x */ |
| 200 | |
| 201 | static double |
| 202 | lanczos_sum(double x) |
| 203 | { |
| 204 | double num = 0.0, den = 0.0; |
| 205 | int i; |
| 206 | assert(x > 0.0); |
| 207 | /* evaluate the rational function lanczos_sum(x). For large |
| 208 | x, the obvious algorithm risks overflow, so we instead |
| 209 | rescale the denominator and numerator of the rational |
| 210 | function by x**(1-LANCZOS_N) and treat this as a |
| 211 | rational function in 1/x. This also reduces the error for |
| 212 | larger x values. The choice of cutoff point (5.0 below) is |
| 213 | somewhat arbitrary; in tests, smaller cutoff values than |
| 214 | this resulted in lower accuracy. */ |
| 215 | if (x < 5.0) { |
| 216 | for (i = LANCZOS_N; --i >= 0; ) { |
| 217 | num = num * x + lanczos_num_coeffs[i]; |
| 218 | den = den * x + lanczos_den_coeffs[i]; |
| 219 | } |
| 220 | } |
| 221 | else { |
| 222 | for (i = 0; i < LANCZOS_N; i++) { |
| 223 | num = num / x + lanczos_num_coeffs[i]; |
| 224 | den = den / x + lanczos_den_coeffs[i]; |
| 225 | } |
| 226 | } |
| 227 | return num/den; |
| 228 | } |
| 229 | |
| 230 | static double |
| 231 | m_tgamma(double x) |
| 232 | { |
| 233 | double absx, r, y, z, sqrtpow; |
| 234 | |
| 235 | /* special cases */ |
| 236 | if (!Py_IS_FINITE(x)) { |
| 237 | if (Py_IS_NAN(x) || x > 0.0) |
| 238 | return x; /* tgamma(nan) = nan, tgamma(inf) = inf */ |
| 239 | else { |
| 240 | errno = EDOM; |
| 241 | return Py_NAN; /* tgamma(-inf) = nan, invalid */ |
| 242 | } |
| 243 | } |
| 244 | if (x == 0.0) { |
| 245 | errno = EDOM; |
| 246 | return 1.0/x; /* tgamma(+-0.0) = +-inf, divide-by-zero */ |
| 247 | } |
| 248 | |
| 249 | /* integer arguments */ |
| 250 | if (x == floor(x)) { |
| 251 | if (x < 0.0) { |
| 252 | errno = EDOM; /* tgamma(n) = nan, invalid for */ |
| 253 | return Py_NAN; /* negative integers n */ |
| 254 | } |
| 255 | if (x <= NGAMMA_INTEGRAL) |
| 256 | return gamma_integral[(int)x - 1]; |
| 257 | } |
| 258 | absx = fabs(x); |
| 259 | |
| 260 | /* tiny arguments: tgamma(x) ~ 1/x for x near 0 */ |
| 261 | if (absx < 1e-20) { |
| 262 | r = 1.0/x; |
| 263 | if (Py_IS_INFINITY(r)) |
| 264 | errno = ERANGE; |
| 265 | return r; |
| 266 | } |
| 267 | |
| 268 | /* large arguments: assuming IEEE 754 doubles, tgamma(x) overflows for |
| 269 | x > 200, and underflows to +-0.0 for x < -200, not a negative |
| 270 | integer. */ |
| 271 | if (absx > 200.0) { |
| 272 | if (x < 0.0) { |
| 273 | return 0.0/sinpi(x); |
| 274 | } |
| 275 | else { |
| 276 | errno = ERANGE; |
| 277 | return Py_HUGE_VAL; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | y = absx + lanczos_g_minus_half; |
| 282 | /* compute error in sum */ |
| 283 | if (absx > lanczos_g_minus_half) { |
| 284 | /* note: the correction can be foiled by an optimizing |
| 285 | compiler that (incorrectly) thinks that an expression like |
| 286 | a + b - a - b can be optimized to 0.0. This shouldn't |
| 287 | happen in a standards-conforming compiler. */ |
| 288 | double q = y - absx; |
| 289 | z = q - lanczos_g_minus_half; |
| 290 | } |
| 291 | else { |
| 292 | double q = y - lanczos_g_minus_half; |
| 293 | z = q - absx; |
| 294 | } |
| 295 | z = z * lanczos_g / y; |
| 296 | if (x < 0.0) { |
| 297 | r = -pi / sinpi(absx) / absx * exp(y) / lanczos_sum(absx); |
| 298 | r -= z * r; |
| 299 | if (absx < 140.0) { |
| 300 | r /= pow(y, absx - 0.5); |
| 301 | } |
| 302 | else { |
| 303 | sqrtpow = pow(y, absx / 2.0 - 0.25); |
| 304 | r /= sqrtpow; |
| 305 | r /= sqrtpow; |
| 306 | } |
| 307 | } |
| 308 | else { |
| 309 | r = lanczos_sum(absx) / exp(y); |
| 310 | r += z * r; |
| 311 | if (absx < 140.0) { |
| 312 | r *= pow(y, absx - 0.5); |
| 313 | } |
| 314 | else { |
| 315 | sqrtpow = pow(y, absx / 2.0 - 0.25); |
| 316 | r *= sqrtpow; |
| 317 | r *= sqrtpow; |
| 318 | } |
| 319 | } |
| 320 | if (Py_IS_INFINITY(r)) |
| 321 | errno = ERANGE; |
| 322 | return r; |
Guido van Rossum | 8832b62 | 1991-12-16 15:44:24 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 325 | /* |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 326 | lgamma: natural log of the absolute value of the Gamma function. |
| 327 | For large arguments, Lanczos' formula works extremely well here. |
| 328 | */ |
| 329 | |
| 330 | static double |
| 331 | m_lgamma(double x) |
| 332 | { |
| 333 | double r, absx; |
| 334 | |
| 335 | /* special cases */ |
| 336 | if (!Py_IS_FINITE(x)) { |
| 337 | if (Py_IS_NAN(x)) |
| 338 | return x; /* lgamma(nan) = nan */ |
| 339 | else |
| 340 | return Py_HUGE_VAL; /* lgamma(+-inf) = +inf */ |
| 341 | } |
| 342 | |
| 343 | /* integer arguments */ |
| 344 | if (x == floor(x) && x <= 2.0) { |
| 345 | if (x <= 0.0) { |
| 346 | errno = EDOM; /* lgamma(n) = inf, divide-by-zero for */ |
| 347 | return Py_HUGE_VAL; /* integers n <= 0 */ |
| 348 | } |
| 349 | else { |
| 350 | return 0.0; /* lgamma(1) = lgamma(2) = 0.0 */ |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | absx = fabs(x); |
| 355 | /* tiny arguments: lgamma(x) ~ -log(fabs(x)) for small x */ |
| 356 | if (absx < 1e-20) |
| 357 | return -log(absx); |
| 358 | |
| 359 | /* Lanczos' formula */ |
| 360 | if (x > 0.0) { |
| 361 | /* we could save a fraction of a ulp in accuracy by having a |
| 362 | second set of numerator coefficients for lanczos_sum that |
| 363 | absorbed the exp(-lanczos_g) term, and throwing out the |
| 364 | lanczos_g subtraction below; it's probably not worth it. */ |
| 365 | r = log(lanczos_sum(x)) - lanczos_g + |
| 366 | (x-0.5)*(log(x+lanczos_g-0.5)-1); |
| 367 | } |
| 368 | else { |
| 369 | r = log(pi) - log(fabs(sinpi(absx))) - log(absx) - |
| 370 | (log(lanczos_sum(absx)) - lanczos_g + |
| 371 | (absx-0.5)*(log(absx+lanczos_g-0.5)-1)); |
| 372 | } |
| 373 | if (Py_IS_INFINITY(r)) |
| 374 | errno = ERANGE; |
| 375 | return r; |
| 376 | } |
| 377 | |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 378 | /* |
| 379 | Implementations of the error function erf(x) and the complementary error |
| 380 | function erfc(x). |
| 381 | |
| 382 | Method: following 'Numerical Recipes' by Flannery, Press et. al. (2nd ed., |
| 383 | Cambridge University Press), we use a series approximation for erf for |
| 384 | small x, and a continued fraction approximation for erfc(x) for larger x; |
| 385 | combined with the relations erf(-x) = -erf(x) and erfc(x) = 1.0 - erf(x), |
| 386 | this gives us erf(x) and erfc(x) for all x. |
| 387 | |
| 388 | The series expansion used is: |
| 389 | |
| 390 | erf(x) = x*exp(-x*x)/sqrt(pi) * [ |
| 391 | 2/1 + 4/3 x**2 + 8/15 x**4 + 16/105 x**6 + ...] |
| 392 | |
| 393 | The coefficient of x**(2k-2) here is 4**k*factorial(k)/factorial(2*k). |
| 394 | This series converges well for smallish x, but slowly for larger x. |
| 395 | |
| 396 | The continued fraction expansion used is: |
| 397 | |
| 398 | erfc(x) = x*exp(-x*x)/sqrt(pi) * [1/(0.5 + x**2 -) 0.5/(2.5 + x**2 - ) |
| 399 | 3.0/(4.5 + x**2 - ) 7.5/(6.5 + x**2 - ) ...] |
| 400 | |
| 401 | after the first term, the general term has the form: |
| 402 | |
| 403 | k*(k-0.5)/(2*k+0.5 + x**2 - ...). |
| 404 | |
| 405 | This expansion converges fast for larger x, but convergence becomes |
| 406 | infinitely slow as x approaches 0.0. The (somewhat naive) continued |
| 407 | fraction evaluation algorithm used below also risks overflow for large x; |
| 408 | but for large x, erfc(x) == 0.0 to within machine precision. (For |
| 409 | example, erfc(30.0) is approximately 2.56e-393). |
| 410 | |
| 411 | Parameters: use series expansion for abs(x) < ERF_SERIES_CUTOFF and |
| 412 | continued fraction expansion for ERF_SERIES_CUTOFF <= abs(x) < |
| 413 | ERFC_CONTFRAC_CUTOFF. ERFC_SERIES_TERMS and ERFC_CONTFRAC_TERMS are the |
| 414 | numbers of terms to use for the relevant expansions. */ |
| 415 | |
| 416 | #define ERF_SERIES_CUTOFF 1.5 |
| 417 | #define ERF_SERIES_TERMS 25 |
| 418 | #define ERFC_CONTFRAC_CUTOFF 30.0 |
| 419 | #define ERFC_CONTFRAC_TERMS 50 |
| 420 | |
| 421 | /* |
| 422 | Error function, via power series. |
| 423 | |
| 424 | Given a finite float x, return an approximation to erf(x). |
| 425 | Converges reasonably fast for small x. |
| 426 | */ |
| 427 | |
| 428 | static double |
| 429 | m_erf_series(double x) |
| 430 | { |
| 431 | double x2, acc, fk; |
| 432 | int i; |
| 433 | |
| 434 | x2 = x * x; |
| 435 | acc = 0.0; |
| 436 | fk = (double)ERF_SERIES_TERMS + 0.5; |
| 437 | for (i = 0; i < ERF_SERIES_TERMS; i++) { |
| 438 | acc = 2.0 + x2 * acc / fk; |
| 439 | fk -= 1.0; |
| 440 | } |
| 441 | return acc * x * exp(-x2) / sqrtpi; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | Complementary error function, via continued fraction expansion. |
| 446 | |
| 447 | Given a positive float x, return an approximation to erfc(x). Converges |
| 448 | reasonably fast for x large (say, x > 2.0), and should be safe from |
| 449 | overflow if x and nterms are not too large. On an IEEE 754 machine, with x |
| 450 | <= 30.0, we're safe up to nterms = 100. For x >= 30.0, erfc(x) is smaller |
| 451 | than the smallest representable nonzero float. */ |
| 452 | |
| 453 | static double |
| 454 | m_erfc_contfrac(double x) |
| 455 | { |
| 456 | double x2, a, da, p, p_last, q, q_last, b; |
| 457 | int i; |
| 458 | |
| 459 | if (x >= ERFC_CONTFRAC_CUTOFF) |
| 460 | return 0.0; |
| 461 | |
| 462 | x2 = x*x; |
| 463 | a = 0.0; |
| 464 | da = 0.5; |
| 465 | p = 1.0; p_last = 0.0; |
| 466 | q = da + x2; q_last = 1.0; |
| 467 | for (i = 0; i < ERFC_CONTFRAC_TERMS; i++) { |
| 468 | double temp; |
| 469 | a += da; |
| 470 | da += 2.0; |
| 471 | b = da + x2; |
| 472 | temp = p; p = b*p - a*p_last; p_last = temp; |
| 473 | temp = q; q = b*q - a*q_last; q_last = temp; |
| 474 | } |
| 475 | return p / q * x * exp(-x2) / sqrtpi; |
| 476 | } |
| 477 | |
| 478 | /* Error function erf(x), for general x */ |
| 479 | |
| 480 | static double |
| 481 | m_erf(double x) |
| 482 | { |
| 483 | double absx, cf; |
| 484 | |
| 485 | if (Py_IS_NAN(x)) |
| 486 | return x; |
| 487 | absx = fabs(x); |
| 488 | if (absx < ERF_SERIES_CUTOFF) |
| 489 | return m_erf_series(x); |
| 490 | else { |
| 491 | cf = m_erfc_contfrac(absx); |
| 492 | return x > 0.0 ? 1.0 - cf : cf - 1.0; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /* Complementary error function erfc(x), for general x. */ |
| 497 | |
| 498 | static double |
| 499 | m_erfc(double x) |
| 500 | { |
| 501 | double absx, cf; |
| 502 | |
| 503 | if (Py_IS_NAN(x)) |
| 504 | return x; |
| 505 | absx = fabs(x); |
| 506 | if (absx < ERF_SERIES_CUTOFF) |
| 507 | return 1.0 - m_erf_series(x); |
| 508 | else { |
| 509 | cf = m_erfc_contfrac(absx); |
| 510 | return x > 0.0 ? cf : 2.0 - cf; |
| 511 | } |
| 512 | } |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 513 | |
| 514 | /* |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 515 | wrapper for atan2 that deals directly with special cases before |
| 516 | delegating to the platform libm for the remaining cases. This |
| 517 | is necessary to get consistent behaviour across platforms. |
| 518 | Windows, FreeBSD and alpha Tru64 are amongst platforms that don't |
| 519 | always follow C99. |
| 520 | */ |
| 521 | |
| 522 | static double |
| 523 | m_atan2(double y, double x) |
| 524 | { |
| 525 | if (Py_IS_NAN(x) || Py_IS_NAN(y)) |
| 526 | return Py_NAN; |
| 527 | if (Py_IS_INFINITY(y)) { |
| 528 | if (Py_IS_INFINITY(x)) { |
| 529 | if (copysign(1., x) == 1.) |
| 530 | /* atan2(+-inf, +inf) == +-pi/4 */ |
| 531 | return copysign(0.25*Py_MATH_PI, y); |
| 532 | else |
| 533 | /* atan2(+-inf, -inf) == +-pi*3/4 */ |
| 534 | return copysign(0.75*Py_MATH_PI, y); |
| 535 | } |
| 536 | /* atan2(+-inf, x) == +-pi/2 for finite x */ |
| 537 | return copysign(0.5*Py_MATH_PI, y); |
| 538 | } |
| 539 | if (Py_IS_INFINITY(x) || y == 0.) { |
| 540 | if (copysign(1., x) == 1.) |
| 541 | /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */ |
| 542 | return copysign(0., y); |
| 543 | else |
| 544 | /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */ |
| 545 | return copysign(Py_MATH_PI, y); |
| 546 | } |
| 547 | return atan2(y, x); |
| 548 | } |
| 549 | |
| 550 | /* |
Mark Dickinson | e675f08 | 2008-12-11 21:56:00 +0000 | [diff] [blame] | 551 | Various platforms (Solaris, OpenBSD) do nonstandard things for log(0), |
| 552 | log(-ve), log(NaN). Here are wrappers for log and log10 that deal with |
| 553 | special values directly, passing positive non-special values through to |
| 554 | the system log/log10. |
| 555 | */ |
| 556 | |
| 557 | static double |
| 558 | m_log(double x) |
| 559 | { |
| 560 | if (Py_IS_FINITE(x)) { |
| 561 | if (x > 0.0) |
| 562 | return log(x); |
| 563 | errno = EDOM; |
| 564 | if (x == 0.0) |
| 565 | return -Py_HUGE_VAL; /* log(0) = -inf */ |
| 566 | else |
| 567 | return Py_NAN; /* log(-ve) = nan */ |
| 568 | } |
| 569 | else if (Py_IS_NAN(x)) |
| 570 | return x; /* log(nan) = nan */ |
| 571 | else if (x > 0.0) |
| 572 | return x; /* log(inf) = inf */ |
| 573 | else { |
| 574 | errno = EDOM; |
| 575 | return Py_NAN; /* log(-inf) = nan */ |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | static double |
| 580 | m_log10(double x) |
| 581 | { |
| 582 | if (Py_IS_FINITE(x)) { |
| 583 | if (x > 0.0) |
| 584 | return log10(x); |
| 585 | errno = EDOM; |
| 586 | if (x == 0.0) |
| 587 | return -Py_HUGE_VAL; /* log10(0) = -inf */ |
| 588 | else |
| 589 | return Py_NAN; /* log10(-ve) = nan */ |
| 590 | } |
| 591 | else if (Py_IS_NAN(x)) |
| 592 | return x; /* log10(nan) = nan */ |
| 593 | else if (x > 0.0) |
| 594 | return x; /* log10(inf) = inf */ |
| 595 | else { |
| 596 | errno = EDOM; |
| 597 | return Py_NAN; /* log10(-inf) = nan */ |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 602 | /* Call is_error when errno != 0, and where x is the result libm |
| 603 | * returned. is_error will usually set up an exception and return |
| 604 | * true (1), but may return false (0) without setting up an exception. |
| 605 | */ |
| 606 | static int |
| 607 | is_error(double x) |
| 608 | { |
| 609 | int result = 1; /* presumption of guilt */ |
| 610 | assert(errno); /* non-zero errno is a precondition for calling */ |
| 611 | if (errno == EDOM) |
| 612 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 613 | |
| 614 | else if (errno == ERANGE) { |
| 615 | /* ANSI C generally requires libm functions to set ERANGE |
| 616 | * on overflow, but also generally *allows* them to set |
| 617 | * ERANGE on underflow too. There's no consistency about |
| 618 | * the latter across platforms. |
| 619 | * Alas, C99 never requires that errno be set. |
| 620 | * Here we suppress the underflow errors (libm functions |
| 621 | * should return a zero on underflow, and +- HUGE_VAL on |
| 622 | * overflow, so testing the result for zero suffices to |
| 623 | * distinguish the cases). |
| 624 | * |
| 625 | * On some platforms (Ubuntu/ia64) it seems that errno can be |
| 626 | * set to ERANGE for subnormal results that do *not* underflow |
| 627 | * to zero. So to be safe, we'll ignore ERANGE whenever the |
| 628 | * function result is less than one in absolute value. |
| 629 | */ |
| 630 | if (fabs(x) < 1.0) |
| 631 | result = 0; |
| 632 | else |
| 633 | PyErr_SetString(PyExc_OverflowError, |
| 634 | "math range error"); |
| 635 | } |
| 636 | else |
| 637 | /* Unexpected math error */ |
| 638 | PyErr_SetFromErrno(PyExc_ValueError); |
| 639 | return result; |
| 640 | } |
| 641 | |
Mark Dickinson | e675f08 | 2008-12-11 21:56:00 +0000 | [diff] [blame] | 642 | /* |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 643 | math_1 is used to wrap a libm function f that takes a double |
| 644 | arguments and returns a double. |
| 645 | |
| 646 | The error reporting follows these rules, which are designed to do |
| 647 | the right thing on C89/C99 platforms and IEEE 754/non IEEE 754 |
| 648 | platforms. |
| 649 | |
| 650 | - a NaN result from non-NaN inputs causes ValueError to be raised |
| 651 | - an infinite result from finite inputs causes OverflowError to be |
| 652 | raised if can_overflow is 1, or raises ValueError if can_overflow |
| 653 | is 0. |
| 654 | - if the result is finite and errno == EDOM then ValueError is |
| 655 | raised |
| 656 | - if the result is finite and nonzero and errno == ERANGE then |
| 657 | OverflowError is raised |
| 658 | |
| 659 | The last rule is used to catch overflow on platforms which follow |
| 660 | C89 but for which HUGE_VAL is not an infinity. |
| 661 | |
| 662 | For the majority of one-argument functions these rules are enough |
| 663 | to ensure that Python's functions behave as specified in 'Annex F' |
| 664 | of the C99 standard, with the 'invalid' and 'divide-by-zero' |
| 665 | floating-point exceptions mapping to Python's ValueError and the |
| 666 | 'overflow' floating-point exception mapping to OverflowError. |
| 667 | math_1 only works for functions that don't have singularities *and* |
| 668 | the possibility of overflow; fortunately, that covers everything we |
| 669 | care about right now. |
| 670 | */ |
| 671 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 672 | static PyObject * |
Jeffrey Yasskin | c215583 | 2008-01-05 20:03:11 +0000 | [diff] [blame] | 673 | math_1_to_whatever(PyObject *arg, double (*func) (double), |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 674 | PyObject *(*from_double_func) (double), |
| 675 | int can_overflow) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 676 | { |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 677 | double x, r; |
| 678 | x = PyFloat_AsDouble(arg); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 679 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 680 | return NULL; |
| 681 | errno = 0; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 682 | PyFPE_START_PROTECT("in math_1", return 0); |
| 683 | r = (*func)(x); |
| 684 | PyFPE_END_PROTECT(r); |
Mark Dickinson | a0de26c | 2008-04-30 23:30:57 +0000 | [diff] [blame] | 685 | if (Py_IS_NAN(r) && !Py_IS_NAN(x)) { |
| 686 | PyErr_SetString(PyExc_ValueError, |
Mark Dickinson | 66bada5 | 2008-06-18 10:04:31 +0000 | [diff] [blame] | 687 | "math domain error"); /* invalid arg */ |
Mark Dickinson | a0de26c | 2008-04-30 23:30:57 +0000 | [diff] [blame] | 688 | return NULL; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 689 | } |
Mark Dickinson | a0de26c | 2008-04-30 23:30:57 +0000 | [diff] [blame] | 690 | if (Py_IS_INFINITY(r) && Py_IS_FINITE(x)) { |
| 691 | if (can_overflow) |
| 692 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 66bada5 | 2008-06-18 10:04:31 +0000 | [diff] [blame] | 693 | "math range error"); /* overflow */ |
Mark Dickinson | b63aff1 | 2008-05-09 14:10:27 +0000 | [diff] [blame] | 694 | else |
| 695 | PyErr_SetString(PyExc_ValueError, |
Mark Dickinson | 66bada5 | 2008-06-18 10:04:31 +0000 | [diff] [blame] | 696 | "math domain error"); /* singularity */ |
Mark Dickinson | a0de26c | 2008-04-30 23:30:57 +0000 | [diff] [blame] | 697 | return NULL; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 698 | } |
Mark Dickinson | de42962 | 2008-05-01 00:19:23 +0000 | [diff] [blame] | 699 | if (Py_IS_FINITE(r) && errno && is_error(r)) |
| 700 | /* this branch unnecessary on most platforms */ |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 701 | return NULL; |
Mark Dickinson | de42962 | 2008-05-01 00:19:23 +0000 | [diff] [blame] | 702 | |
| 703 | return (*from_double_func)(r); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 706 | /* variant of math_1, to be used when the function being wrapped is known to |
| 707 | set errno properly (that is, errno = EDOM for invalid or divide-by-zero, |
| 708 | errno = ERANGE for overflow). */ |
| 709 | |
| 710 | static PyObject * |
| 711 | math_1a(PyObject *arg, double (*func) (double)) |
| 712 | { |
| 713 | double x, r; |
| 714 | x = PyFloat_AsDouble(arg); |
| 715 | if (x == -1.0 && PyErr_Occurred()) |
| 716 | return NULL; |
| 717 | errno = 0; |
| 718 | PyFPE_START_PROTECT("in math_1a", return 0); |
| 719 | r = (*func)(x); |
| 720 | PyFPE_END_PROTECT(r); |
| 721 | if (errno && is_error(r)) |
| 722 | return NULL; |
| 723 | return PyFloat_FromDouble(r); |
| 724 | } |
| 725 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 726 | /* |
| 727 | math_2 is used to wrap a libm function f that takes two double |
| 728 | arguments and returns a double. |
| 729 | |
| 730 | The error reporting follows these rules, which are designed to do |
| 731 | the right thing on C89/C99 platforms and IEEE 754/non IEEE 754 |
| 732 | platforms. |
| 733 | |
| 734 | - a NaN result from non-NaN inputs causes ValueError to be raised |
| 735 | - an infinite result from finite inputs causes OverflowError to be |
| 736 | raised. |
| 737 | - if the result is finite and errno == EDOM then ValueError is |
| 738 | raised |
| 739 | - if the result is finite and nonzero and errno == ERANGE then |
| 740 | OverflowError is raised |
| 741 | |
| 742 | The last rule is used to catch overflow on platforms which follow |
| 743 | C89 but for which HUGE_VAL is not an infinity. |
| 744 | |
| 745 | For most two-argument functions (copysign, fmod, hypot, atan2) |
| 746 | these rules are enough to ensure that Python's functions behave as |
| 747 | specified in 'Annex F' of the C99 standard, with the 'invalid' and |
| 748 | 'divide-by-zero' floating-point exceptions mapping to Python's |
| 749 | ValueError and the 'overflow' floating-point exception mapping to |
| 750 | OverflowError. |
| 751 | */ |
| 752 | |
| 753 | static PyObject * |
| 754 | math_1(PyObject *arg, double (*func) (double), int can_overflow) |
| 755 | { |
| 756 | return math_1_to_whatever(arg, func, PyFloat_FromDouble, can_overflow); |
Jeffrey Yasskin | c215583 | 2008-01-05 20:03:11 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static PyObject * |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 760 | math_1_to_int(PyObject *arg, double (*func) (double), int can_overflow) |
Jeffrey Yasskin | c215583 | 2008-01-05 20:03:11 +0000 | [diff] [blame] | 761 | { |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 762 | return math_1_to_whatever(arg, func, PyLong_FromDouble, can_overflow); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 765 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 766 | math_2(PyObject *args, double (*func) (double, double), char *funcname) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 767 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 768 | PyObject *ox, *oy; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 769 | double x, y, r; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 770 | if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy)) |
| 771 | return NULL; |
| 772 | x = PyFloat_AsDouble(ox); |
| 773 | y = PyFloat_AsDouble(oy); |
| 774 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 775 | return NULL; |
| 776 | errno = 0; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 777 | PyFPE_START_PROTECT("in math_2", return 0); |
| 778 | r = (*func)(x, y); |
| 779 | PyFPE_END_PROTECT(r); |
| 780 | if (Py_IS_NAN(r)) { |
| 781 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 782 | errno = EDOM; |
| 783 | else |
| 784 | errno = 0; |
| 785 | } |
| 786 | else if (Py_IS_INFINITY(r)) { |
| 787 | if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 788 | errno = ERANGE; |
| 789 | else |
| 790 | errno = 0; |
| 791 | } |
| 792 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 793 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 794 | else |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 795 | return PyFloat_FromDouble(r); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 798 | #define FUNC1(funcname, func, can_overflow, docstring) \ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 799 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 800 | return math_1(args, func, can_overflow); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 801 | }\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 802 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 803 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 804 | #define FUNC1A(funcname, func, docstring) \ |
| 805 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
| 806 | return math_1a(args, func); \ |
| 807 | }\ |
| 808 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
| 809 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 810 | #define FUNC2(funcname, func, docstring) \ |
| 811 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 812 | return math_2(args, func, #funcname); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 813 | }\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 814 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 815 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 816 | FUNC1(acos, acos, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 817 | "acos(x)\n\nReturn the arc cosine (measured in radians) of x.") |
Mark Dickinson | f371859 | 2009-12-21 15:27:41 +0000 | [diff] [blame] | 818 | FUNC1(acosh, m_acosh, 0, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 819 | "acosh(x)\n\nReturn the hyperbolic arc cosine (measured in radians) of x.") |
| 820 | FUNC1(asin, asin, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 821 | "asin(x)\n\nReturn the arc sine (measured in radians) of x.") |
Mark Dickinson | f371859 | 2009-12-21 15:27:41 +0000 | [diff] [blame] | 822 | FUNC1(asinh, m_asinh, 0, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 823 | "asinh(x)\n\nReturn the hyperbolic arc sine (measured in radians) of x.") |
| 824 | FUNC1(atan, atan, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 825 | "atan(x)\n\nReturn the arc tangent (measured in radians) of x.") |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 826 | FUNC2(atan2, m_atan2, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 827 | "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n" |
| 828 | "Unlike atan(y/x), the signs of both x and y are considered.") |
Mark Dickinson | f371859 | 2009-12-21 15:27:41 +0000 | [diff] [blame] | 829 | FUNC1(atanh, m_atanh, 0, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 830 | "atanh(x)\n\nReturn the hyperbolic arc tangent (measured in radians) of x.") |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 831 | |
| 832 | static PyObject * math_ceil(PyObject *self, PyObject *number) { |
| 833 | static PyObject *ceil_str = NULL; |
| 834 | PyObject *method; |
| 835 | |
| 836 | if (ceil_str == NULL) { |
Christian Heimes | fe82e77 | 2008-01-28 02:38:20 +0000 | [diff] [blame] | 837 | ceil_str = PyUnicode_InternFromString("__ceil__"); |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 838 | if (ceil_str == NULL) |
| 839 | return NULL; |
| 840 | } |
| 841 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 842 | method = _PyType_Lookup(Py_TYPE(number), ceil_str); |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 843 | if (method == NULL) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 844 | return math_1_to_int(number, ceil, 0); |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 845 | else |
| 846 | return PyObject_CallFunction(method, "O", number); |
| 847 | } |
| 848 | |
| 849 | PyDoc_STRVAR(math_ceil_doc, |
Jeffrey Yasskin | c215583 | 2008-01-05 20:03:11 +0000 | [diff] [blame] | 850 | "ceil(x)\n\nReturn the ceiling of x as an int.\n" |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 851 | "This is the smallest integral value >= x."); |
| 852 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 853 | FUNC2(copysign, copysign, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 854 | "copysign(x, y)\n\nReturn x with the sign of y.") |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 855 | FUNC1(cos, cos, 0, |
| 856 | "cos(x)\n\nReturn the cosine of x (measured in radians).") |
| 857 | FUNC1(cosh, cosh, 1, |
| 858 | "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 859 | FUNC1A(erf, m_erf, |
| 860 | "erf(x)\n\nError function at x.") |
| 861 | FUNC1A(erfc, m_erfc, |
| 862 | "erfc(x)\n\nComplementary error function at x.") |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 863 | FUNC1(exp, exp, 1, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 864 | "exp(x)\n\nReturn e raised to the power of x.") |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 865 | FUNC1(expm1, m_expm1, 1, |
| 866 | "expm1(x)\n\nReturn exp(x)-1.\n" |
| 867 | "This function avoids the loss of precision involved in the direct " |
| 868 | "evaluation of exp(x)-1 for small x.") |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 869 | FUNC1(fabs, fabs, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 870 | "fabs(x)\n\nReturn the absolute value of the float x.") |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 871 | |
| 872 | static PyObject * math_floor(PyObject *self, PyObject *number) { |
| 873 | static PyObject *floor_str = NULL; |
| 874 | PyObject *method; |
| 875 | |
| 876 | if (floor_str == NULL) { |
Christian Heimes | fe82e77 | 2008-01-28 02:38:20 +0000 | [diff] [blame] | 877 | floor_str = PyUnicode_InternFromString("__floor__"); |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 878 | if (floor_str == NULL) |
| 879 | return NULL; |
| 880 | } |
| 881 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 882 | method = _PyType_Lookup(Py_TYPE(number), floor_str); |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 883 | if (method == NULL) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 884 | return math_1_to_int(number, floor, 0); |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 885 | else |
| 886 | return PyObject_CallFunction(method, "O", number); |
| 887 | } |
| 888 | |
| 889 | PyDoc_STRVAR(math_floor_doc, |
Jeffrey Yasskin | c215583 | 2008-01-05 20:03:11 +0000 | [diff] [blame] | 890 | "floor(x)\n\nReturn the floor of x as an int.\n" |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 891 | "This is the largest integral value <= x."); |
| 892 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 893 | FUNC1A(gamma, m_tgamma, |
| 894 | "gamma(x)\n\nGamma function at x.") |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 895 | FUNC1A(lgamma, m_lgamma, |
| 896 | "lgamma(x)\n\nNatural logarithm of absolute value of Gamma function at x.") |
Mark Dickinson | f371859 | 2009-12-21 15:27:41 +0000 | [diff] [blame] | 897 | FUNC1(log1p, m_log1p, 1, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 898 | "log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n" |
| 899 | "The result is computed in a way which is accurate for x near zero.") |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 900 | FUNC1(sin, sin, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 901 | "sin(x)\n\nReturn the sine of x (measured in radians).") |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 902 | FUNC1(sinh, sinh, 1, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 903 | "sinh(x)\n\nReturn the hyperbolic sine of x.") |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 904 | FUNC1(sqrt, sqrt, 0, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 905 | "sqrt(x)\n\nReturn the square root of x.") |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 906 | FUNC1(tan, tan, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 907 | "tan(x)\n\nReturn the tangent of x (measured in radians).") |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 908 | FUNC1(tanh, tanh, 0, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 909 | "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 910 | |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 911 | /* Precision summation function as msum() by Raymond Hettinger in |
| 912 | <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090>, |
| 913 | enhanced with the exact partials sum and roundoff from Mark |
| 914 | Dickinson's post at <http://bugs.python.org/file10357/msum4.py>. |
| 915 | See those links for more details, proofs and other references. |
| 916 | |
| 917 | Note 1: IEEE 754R floating point semantics are assumed, |
| 918 | but the current implementation does not re-establish special |
| 919 | value semantics across iterations (i.e. handling -Inf + Inf). |
| 920 | |
| 921 | Note 2: No provision is made for intermediate overflow handling; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 922 | therefore, sum([1e+308, 1e-308, 1e+308]) returns 1e+308 while |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 923 | sum([1e+308, 1e+308, 1e-308]) raises an OverflowError due to the |
| 924 | overflow of the first partial sum. |
| 925 | |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 926 | Note 3: The intermediate values lo, yr, and hi are declared volatile so |
| 927 | aggressive compilers won't algebraically reduce lo to always be exactly 0.0. |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 928 | Also, the volatile declaration forces the values to be stored in memory as |
| 929 | regular doubles instead of extended long precision (80-bit) values. This |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 930 | prevents double rounding because any addition or subtraction of two doubles |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 931 | can be resolved exactly into double-sized hi and lo values. As long as the |
| 932 | hi value gets forced into a double before yr and lo are computed, the extra |
| 933 | bits in downstream extended precision operations (x87 for example) will be |
| 934 | exactly zero and therefore can be losslessly stored back into a double, |
| 935 | thereby preventing double rounding. |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 936 | |
| 937 | Note 4: A similar implementation is in Modules/cmathmodule.c. |
| 938 | Be sure to update both when making changes. |
| 939 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 940 | Note 5: The signature of math.fsum() differs from __builtin__.sum() |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 941 | because the start argument doesn't make sense in the context of |
| 942 | accurate summation. Since the partials table is collapsed before |
| 943 | returning a result, sum(seq2, start=sum(seq1)) may not equal the |
| 944 | accurate result returned by sum(itertools.chain(seq1, seq2)). |
| 945 | */ |
| 946 | |
| 947 | #define NUM_PARTIALS 32 /* initial partials array size, on stack */ |
| 948 | |
| 949 | /* Extend the partials array p[] by doubling its size. */ |
| 950 | static int /* non-zero on error */ |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 951 | _fsum_realloc(double **p_ptr, Py_ssize_t n, |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 952 | double *ps, Py_ssize_t *m_ptr) |
| 953 | { |
| 954 | void *v = NULL; |
| 955 | Py_ssize_t m = *m_ptr; |
| 956 | |
| 957 | m += m; /* double */ |
| 958 | if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) { |
| 959 | double *p = *p_ptr; |
| 960 | if (p == ps) { |
| 961 | v = PyMem_Malloc(sizeof(double) * m); |
| 962 | if (v != NULL) |
| 963 | memcpy(v, ps, sizeof(double) * n); |
| 964 | } |
| 965 | else |
| 966 | v = PyMem_Realloc(p, sizeof(double) * m); |
| 967 | } |
| 968 | if (v == NULL) { /* size overflow or no memory */ |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 969 | PyErr_SetString(PyExc_MemoryError, "math.fsum partials"); |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 970 | return 1; |
| 971 | } |
| 972 | *p_ptr = (double*) v; |
| 973 | *m_ptr = m; |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | /* Full precision summation of a sequence of floats. |
| 978 | |
| 979 | def msum(iterable): |
| 980 | partials = [] # sorted, non-overlapping partial sums |
| 981 | for x in iterable: |
| 982 | i = 0 |
| 983 | for y in partials: |
| 984 | if abs(x) < abs(y): |
| 985 | x, y = y, x |
| 986 | hi = x + y |
| 987 | lo = y - (hi - x) |
| 988 | if lo: |
| 989 | partials[i] = lo |
| 990 | i += 1 |
| 991 | x = hi |
| 992 | partials[i:] = [x] |
| 993 | return sum_exact(partials) |
| 994 | |
| 995 | Rounded x+y stored in hi with the roundoff stored in lo. Together hi+lo |
| 996 | are exactly equal to x+y. The inner loop applies hi/lo summation to each |
| 997 | partial so that the list of partial sums remains exact. |
| 998 | |
| 999 | Sum_exact() adds the partial sums exactly and correctly rounds the final |
| 1000 | result (using the round-half-to-even rule). The items in partials remain |
| 1001 | non-zero, non-special, non-overlapping and strictly increasing in |
| 1002 | magnitude, but possibly not all having the same sign. |
| 1003 | |
| 1004 | Depends on IEEE 754 arithmetic guarantees and half-even rounding. |
| 1005 | */ |
| 1006 | |
| 1007 | static PyObject* |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1008 | math_fsum(PyObject *self, PyObject *seq) |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1009 | { |
| 1010 | PyObject *item, *iter, *sum = NULL; |
| 1011 | Py_ssize_t i, j, n = 0, m = NUM_PARTIALS; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1012 | double x, y, t, ps[NUM_PARTIALS], *p = ps; |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1013 | double xsave, special_sum = 0.0, inf_sum = 0.0; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1014 | volatile double hi, yr, lo; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1015 | |
| 1016 | iter = PyObject_GetIter(seq); |
| 1017 | if (iter == NULL) |
| 1018 | return NULL; |
| 1019 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1020 | PyFPE_START_PROTECT("fsum", Py_DECREF(iter); return NULL) |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1021 | |
| 1022 | for(;;) { /* for x in iterable */ |
| 1023 | assert(0 <= n && n <= m); |
| 1024 | assert((m == NUM_PARTIALS && p == ps) || |
| 1025 | (m > NUM_PARTIALS && p != NULL)); |
| 1026 | |
| 1027 | item = PyIter_Next(iter); |
| 1028 | if (item == NULL) { |
| 1029 | if (PyErr_Occurred()) |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1030 | goto _fsum_error; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1031 | break; |
| 1032 | } |
| 1033 | x = PyFloat_AsDouble(item); |
| 1034 | Py_DECREF(item); |
| 1035 | if (PyErr_Occurred()) |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1036 | goto _fsum_error; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1037 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1038 | xsave = x; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1039 | for (i = j = 0; j < n; j++) { /* for y in partials */ |
| 1040 | y = p[j]; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1041 | if (fabs(x) < fabs(y)) { |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1042 | t = x; x = y; y = t; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1043 | } |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1044 | hi = x + y; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1045 | yr = hi - x; |
| 1046 | lo = y - yr; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1047 | if (lo != 0.0) |
| 1048 | p[i++] = lo; |
| 1049 | x = hi; |
| 1050 | } |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1051 | |
| 1052 | n = i; /* ps[i:] = [x] */ |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1053 | if (x != 0.0) { |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1054 | if (! Py_IS_FINITE(x)) { |
| 1055 | /* a nonfinite x could arise either as |
| 1056 | a result of intermediate overflow, or |
| 1057 | as a result of a nan or inf in the |
| 1058 | summands */ |
| 1059 | if (Py_IS_FINITE(xsave)) { |
| 1060 | PyErr_SetString(PyExc_OverflowError, |
| 1061 | "intermediate overflow in fsum"); |
| 1062 | goto _fsum_error; |
| 1063 | } |
| 1064 | if (Py_IS_INFINITY(xsave)) |
| 1065 | inf_sum += xsave; |
| 1066 | special_sum += xsave; |
| 1067 | /* reset partials */ |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1068 | n = 0; |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1069 | } |
| 1070 | else if (n >= m && _fsum_realloc(&p, n, ps, &m)) |
| 1071 | goto _fsum_error; |
| 1072 | else |
| 1073 | p[n++] = x; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1077 | if (special_sum != 0.0) { |
| 1078 | if (Py_IS_NAN(inf_sum)) |
| 1079 | PyErr_SetString(PyExc_ValueError, |
| 1080 | "-inf + inf in fsum"); |
| 1081 | else |
| 1082 | sum = PyFloat_FromDouble(special_sum); |
| 1083 | goto _fsum_error; |
| 1084 | } |
| 1085 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1086 | hi = 0.0; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1087 | if (n > 0) { |
| 1088 | hi = p[--n]; |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1089 | /* sum_exact(ps, hi) from the top, stop when the sum becomes |
| 1090 | inexact. */ |
| 1091 | while (n > 0) { |
| 1092 | x = hi; |
| 1093 | y = p[--n]; |
| 1094 | assert(fabs(y) < fabs(x)); |
| 1095 | hi = x + y; |
| 1096 | yr = hi - x; |
| 1097 | lo = y - yr; |
| 1098 | if (lo != 0.0) |
| 1099 | break; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1100 | } |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1101 | /* Make half-even rounding work across multiple partials. |
| 1102 | Needed so that sum([1e-16, 1, 1e16]) will round-up the last |
| 1103 | digit to two instead of down to zero (the 1e-16 makes the 1 |
| 1104 | slightly closer to two). With a potential 1 ULP rounding |
| 1105 | error fixed-up, math.fsum() can guarantee commutativity. */ |
| 1106 | if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) || |
| 1107 | (lo > 0.0 && p[n-1] > 0.0))) { |
| 1108 | y = lo * 2.0; |
| 1109 | x = hi + y; |
| 1110 | yr = x - hi; |
| 1111 | if (y == yr) |
| 1112 | hi = x; |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1115 | sum = PyFloat_FromDouble(hi); |
| 1116 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1117 | _fsum_error: |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1118 | PyFPE_END_PROTECT(hi) |
| 1119 | Py_DECREF(iter); |
| 1120 | if (p != ps) |
| 1121 | PyMem_Free(p); |
| 1122 | return sum; |
| 1123 | } |
| 1124 | |
| 1125 | #undef NUM_PARTIALS |
| 1126 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1127 | PyDoc_STRVAR(math_fsum_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1128 | "fsum(iterable)\n\n\ |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1129 | Return an accurate floating point sum of values in the iterable.\n\ |
| 1130 | Assumes IEEE-754 floating point arithmetic."); |
| 1131 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 1132 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1133 | math_factorial(PyObject *self, PyObject *arg) |
| 1134 | { |
| 1135 | long i, x; |
| 1136 | PyObject *result, *iobj, *newresult; |
| 1137 | |
| 1138 | if (PyFloat_Check(arg)) { |
Mark Dickinson | da39dbf | 2009-12-20 14:07:47 +0000 | [diff] [blame] | 1139 | PyObject *lx; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1140 | double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg); |
Mark Dickinson | da39dbf | 2009-12-20 14:07:47 +0000 | [diff] [blame] | 1141 | if (!(Py_IS_FINITE(dx) && dx == floor(dx))) { |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1142 | PyErr_SetString(PyExc_ValueError, |
| 1143 | "factorial() only accepts integral values"); |
| 1144 | return NULL; |
| 1145 | } |
Mark Dickinson | da39dbf | 2009-12-20 14:07:47 +0000 | [diff] [blame] | 1146 | lx = PyLong_FromDouble(dx); |
| 1147 | if (lx == NULL) |
| 1148 | return NULL; |
| 1149 | x = PyLong_AsLong(lx); |
| 1150 | Py_DECREF(lx); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1151 | } |
Mark Dickinson | da39dbf | 2009-12-20 14:07:47 +0000 | [diff] [blame] | 1152 | else |
| 1153 | x = PyLong_AsLong(arg); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1154 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1155 | if (x == -1 && PyErr_Occurred()) |
| 1156 | return NULL; |
| 1157 | if (x < 0) { |
| 1158 | PyErr_SetString(PyExc_ValueError, |
| 1159 | "factorial() not defined for negative values"); |
| 1160 | return NULL; |
| 1161 | } |
| 1162 | |
| 1163 | result = (PyObject *)PyLong_FromLong(1); |
| 1164 | if (result == NULL) |
| 1165 | return NULL; |
| 1166 | for (i=1 ; i<=x ; i++) { |
| 1167 | iobj = (PyObject *)PyLong_FromLong(i); |
| 1168 | if (iobj == NULL) |
| 1169 | goto error; |
| 1170 | newresult = PyNumber_Multiply(result, iobj); |
| 1171 | Py_DECREF(iobj); |
| 1172 | if (newresult == NULL) |
| 1173 | goto error; |
| 1174 | Py_DECREF(result); |
| 1175 | result = newresult; |
| 1176 | } |
| 1177 | return result; |
| 1178 | |
| 1179 | error: |
| 1180 | Py_DECREF(result); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1181 | return NULL; |
| 1182 | } |
| 1183 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 1184 | PyDoc_STRVAR(math_factorial_doc, |
| 1185 | "factorial(x) -> Integral\n" |
| 1186 | "\n" |
| 1187 | "Find x!. Raise a ValueError if x is negative or non-integral."); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1188 | |
| 1189 | static PyObject * |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 1190 | math_trunc(PyObject *self, PyObject *number) |
| 1191 | { |
| 1192 | static PyObject *trunc_str = NULL; |
| 1193 | PyObject *trunc; |
| 1194 | |
| 1195 | if (Py_TYPE(number)->tp_dict == NULL) { |
| 1196 | if (PyType_Ready(Py_TYPE(number)) < 0) |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | |
| 1200 | if (trunc_str == NULL) { |
| 1201 | trunc_str = PyUnicode_InternFromString("__trunc__"); |
| 1202 | if (trunc_str == NULL) |
| 1203 | return NULL; |
| 1204 | } |
| 1205 | |
| 1206 | trunc = _PyType_Lookup(Py_TYPE(number), trunc_str); |
| 1207 | if (trunc == NULL) { |
| 1208 | PyErr_Format(PyExc_TypeError, |
| 1209 | "type %.100s doesn't define __trunc__ method", |
| 1210 | Py_TYPE(number)->tp_name); |
| 1211 | return NULL; |
| 1212 | } |
| 1213 | return PyObject_CallFunctionObjArgs(trunc, number, NULL); |
| 1214 | } |
| 1215 | |
| 1216 | PyDoc_STRVAR(math_trunc_doc, |
| 1217 | "trunc(x:Real) -> Integral\n" |
| 1218 | "\n" |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 1219 | "Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method."); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 1220 | |
| 1221 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1222 | math_frexp(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1223 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1224 | int i; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1225 | double x = PyFloat_AsDouble(arg); |
| 1226 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1227 | return NULL; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1228 | /* deal with special cases directly, to sidestep platform |
| 1229 | differences */ |
| 1230 | if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) { |
| 1231 | i = 0; |
| 1232 | } |
| 1233 | else { |
| 1234 | PyFPE_START_PROTECT("in math_frexp", return 0); |
| 1235 | x = frexp(x, &i); |
| 1236 | PyFPE_END_PROTECT(x); |
| 1237 | } |
| 1238 | return Py_BuildValue("(di)", x, i); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1241 | PyDoc_STRVAR(math_frexp_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 1242 | "frexp(x)\n" |
| 1243 | "\n" |
| 1244 | "Return the mantissa and exponent of x, as pair (m, e).\n" |
| 1245 | "m is a float and e is an int, such that x = m * 2.**e.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1246 | "If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1247 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 1248 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1249 | math_ldexp(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1250 | { |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1251 | double x, r; |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 1252 | PyObject *oexp; |
| 1253 | long exp; |
Mark Dickinson | fbbb9bd | 2010-01-03 12:16:06 +0000 | [diff] [blame] | 1254 | int overflow; |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 1255 | if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1256 | return NULL; |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 1257 | |
| 1258 | if (PyLong_Check(oexp)) { |
| 1259 | /* on overflow, replace exponent with either LONG_MAX |
| 1260 | or LONG_MIN, depending on the sign. */ |
Mark Dickinson | fbbb9bd | 2010-01-03 12:16:06 +0000 | [diff] [blame] | 1261 | exp = PyLong_AsLongAndOverflow(oexp, &overflow); |
| 1262 | if (exp == -1 && PyErr_Occurred()) |
| 1263 | return NULL; |
| 1264 | if (overflow) |
| 1265 | exp = overflow < 0 ? LONG_MIN : LONG_MAX; |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 1266 | } |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 1267 | else { |
| 1268 | PyErr_SetString(PyExc_TypeError, |
| 1269 | "Expected an int or long as second argument " |
| 1270 | "to ldexp."); |
| 1271 | return NULL; |
| 1272 | } |
| 1273 | |
| 1274 | if (x == 0. || !Py_IS_FINITE(x)) { |
| 1275 | /* NaNs, zeros and infinities are returned unchanged */ |
| 1276 | r = x; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1277 | errno = 0; |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 1278 | } else if (exp > INT_MAX) { |
| 1279 | /* overflow */ |
| 1280 | r = copysign(Py_HUGE_VAL, x); |
| 1281 | errno = ERANGE; |
| 1282 | } else if (exp < INT_MIN) { |
| 1283 | /* underflow to +-0 */ |
| 1284 | r = copysign(0., x); |
| 1285 | errno = 0; |
| 1286 | } else { |
| 1287 | errno = 0; |
| 1288 | PyFPE_START_PROTECT("in math_ldexp", return 0); |
| 1289 | r = ldexp(x, (int)exp); |
| 1290 | PyFPE_END_PROTECT(r); |
| 1291 | if (Py_IS_INFINITY(r)) |
| 1292 | errno = ERANGE; |
| 1293 | } |
| 1294 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1295 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 1296 | return NULL; |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 1297 | return PyFloat_FromDouble(r); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1300 | PyDoc_STRVAR(math_ldexp_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1301 | "ldexp(x, i)\n\n\ |
| 1302 | Return x * (2**i)."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1303 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 1304 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1305 | math_modf(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1306 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1307 | double y, x = PyFloat_AsDouble(arg); |
| 1308 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1309 | return NULL; |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1310 | /* some platforms don't do the right thing for NaNs and |
| 1311 | infinities, so we take care of special cases directly. */ |
| 1312 | if (!Py_IS_FINITE(x)) { |
| 1313 | if (Py_IS_INFINITY(x)) |
| 1314 | return Py_BuildValue("(dd)", copysign(0., x), x); |
| 1315 | else if (Py_IS_NAN(x)) |
| 1316 | return Py_BuildValue("(dd)", x, x); |
| 1317 | } |
| 1318 | |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1319 | errno = 0; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1320 | PyFPE_START_PROTECT("in math_modf", return 0); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1321 | x = modf(x, &y); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1322 | PyFPE_END_PROTECT(x); |
| 1323 | return Py_BuildValue("(dd)", x, y); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1324 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1325 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1326 | PyDoc_STRVAR(math_modf_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 1327 | "modf(x)\n" |
| 1328 | "\n" |
| 1329 | "Return the fractional and integer parts of x. Both results carry the sign\n" |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 1330 | "of x and are floats."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1331 | |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1332 | /* A decent logarithm is easy to compute even for huge longs, but libm can't |
| 1333 | do that by itself -- loghelper can. func is log or log10, and name is |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 1334 | "log" or "log10". Note that overflow of the result isn't possible: a long |
| 1335 | can contain no more than INT_MAX * SHIFT bits, so has value certainly less |
| 1336 | than 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1337 | small enough to fit in an IEEE single. log and log10 are even smaller. |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 1338 | However, intermediate overflow is possible for a long if the number of bits |
| 1339 | in that long is larger than PY_SSIZE_T_MAX. */ |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1340 | |
| 1341 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1342 | loghelper(PyObject* arg, double (*func)(double), char *funcname) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1343 | { |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1344 | /* If it is long, do it ourselves. */ |
| 1345 | if (PyLong_Check(arg)) { |
| 1346 | double x; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 1347 | Py_ssize_t e; |
| 1348 | x = _PyLong_Frexp((PyLongObject *)arg, &e); |
| 1349 | if (x == -1.0 && PyErr_Occurred()) |
| 1350 | return NULL; |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1351 | if (x <= 0.0) { |
| 1352 | PyErr_SetString(PyExc_ValueError, |
| 1353 | "math domain error"); |
| 1354 | return NULL; |
| 1355 | } |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 1356 | /* Special case for log(1), to make sure we get an |
| 1357 | exact result there. */ |
| 1358 | if (e == 1 && x == 0.5) |
| 1359 | return PyFloat_FromDouble(0.0); |
| 1360 | /* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */ |
| 1361 | x = func(x) + func(2.0) * e; |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1362 | return PyFloat_FromDouble(x); |
| 1363 | } |
| 1364 | |
| 1365 | /* Else let libm handle it by itself. */ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1366 | return math_1(arg, func, 0); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | static PyObject * |
| 1370 | math_log(PyObject *self, PyObject *args) |
| 1371 | { |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1372 | PyObject *arg; |
| 1373 | PyObject *base = NULL; |
| 1374 | PyObject *num, *den; |
| 1375 | PyObject *ans; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1376 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1377 | if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base)) |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1378 | return NULL; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1379 | |
Mark Dickinson | e675f08 | 2008-12-11 21:56:00 +0000 | [diff] [blame] | 1380 | num = loghelper(arg, m_log, "log"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1381 | if (num == NULL || base == NULL) |
| 1382 | return num; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1383 | |
Mark Dickinson | e675f08 | 2008-12-11 21:56:00 +0000 | [diff] [blame] | 1384 | den = loghelper(base, m_log, "log"); |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1385 | if (den == NULL) { |
| 1386 | Py_DECREF(num); |
| 1387 | return NULL; |
| 1388 | } |
| 1389 | |
Neal Norwitz | bcc0db8 | 2006-03-24 08:14:36 +0000 | [diff] [blame] | 1390 | ans = PyNumber_TrueDivide(num, den); |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1391 | Py_DECREF(num); |
| 1392 | Py_DECREF(den); |
| 1393 | return ans; |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1396 | PyDoc_STRVAR(math_log_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1397 | "log(x[, base])\n\n\ |
| 1398 | Return the logarithm of x to the given base.\n\ |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1399 | If the base not specified, returns the natural logarithm (base e) of x."); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1400 | |
| 1401 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1402 | math_log10(PyObject *self, PyObject *arg) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1403 | { |
Mark Dickinson | e675f08 | 2008-12-11 21:56:00 +0000 | [diff] [blame] | 1404 | return loghelper(arg, m_log10, "log10"); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1407 | PyDoc_STRVAR(math_log10_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1408 | "log10(x)\n\nReturn the base 10 logarithm of x."); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1409 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1410 | static PyObject * |
| 1411 | math_fmod(PyObject *self, PyObject *args) |
| 1412 | { |
| 1413 | PyObject *ox, *oy; |
| 1414 | double r, x, y; |
| 1415 | if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy)) |
| 1416 | return NULL; |
| 1417 | x = PyFloat_AsDouble(ox); |
| 1418 | y = PyFloat_AsDouble(oy); |
| 1419 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 1420 | return NULL; |
| 1421 | /* fmod(x, +/-Inf) returns x for finite x. */ |
| 1422 | if (Py_IS_INFINITY(y) && Py_IS_FINITE(x)) |
| 1423 | return PyFloat_FromDouble(x); |
| 1424 | errno = 0; |
| 1425 | PyFPE_START_PROTECT("in math_fmod", return 0); |
| 1426 | r = fmod(x, y); |
| 1427 | PyFPE_END_PROTECT(r); |
| 1428 | if (Py_IS_NAN(r)) { |
| 1429 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 1430 | errno = EDOM; |
| 1431 | else |
| 1432 | errno = 0; |
| 1433 | } |
| 1434 | if (errno && is_error(r)) |
| 1435 | return NULL; |
| 1436 | else |
| 1437 | return PyFloat_FromDouble(r); |
| 1438 | } |
| 1439 | |
| 1440 | PyDoc_STRVAR(math_fmod_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1441 | "fmod(x, y)\n\nReturn fmod(x, y), according to platform C." |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1442 | " x % y may differ."); |
| 1443 | |
| 1444 | static PyObject * |
| 1445 | math_hypot(PyObject *self, PyObject *args) |
| 1446 | { |
| 1447 | PyObject *ox, *oy; |
| 1448 | double r, x, y; |
| 1449 | if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy)) |
| 1450 | return NULL; |
| 1451 | x = PyFloat_AsDouble(ox); |
| 1452 | y = PyFloat_AsDouble(oy); |
| 1453 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 1454 | return NULL; |
| 1455 | /* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */ |
| 1456 | if (Py_IS_INFINITY(x)) |
| 1457 | return PyFloat_FromDouble(fabs(x)); |
| 1458 | if (Py_IS_INFINITY(y)) |
| 1459 | return PyFloat_FromDouble(fabs(y)); |
| 1460 | errno = 0; |
| 1461 | PyFPE_START_PROTECT("in math_hypot", return 0); |
| 1462 | r = hypot(x, y); |
| 1463 | PyFPE_END_PROTECT(r); |
| 1464 | if (Py_IS_NAN(r)) { |
| 1465 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 1466 | errno = EDOM; |
| 1467 | else |
| 1468 | errno = 0; |
| 1469 | } |
| 1470 | else if (Py_IS_INFINITY(r)) { |
| 1471 | if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 1472 | errno = ERANGE; |
| 1473 | else |
| 1474 | errno = 0; |
| 1475 | } |
| 1476 | if (errno && is_error(r)) |
| 1477 | return NULL; |
| 1478 | else |
| 1479 | return PyFloat_FromDouble(r); |
| 1480 | } |
| 1481 | |
| 1482 | PyDoc_STRVAR(math_hypot_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1483 | "hypot(x, y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y)."); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1484 | |
| 1485 | /* pow can't use math_2, but needs its own wrapper: the problem is |
| 1486 | that an infinite result can arise either as a result of overflow |
| 1487 | (in which case OverflowError should be raised) or as a result of |
| 1488 | e.g. 0.**-5. (for which ValueError needs to be raised.) |
| 1489 | */ |
| 1490 | |
| 1491 | static PyObject * |
| 1492 | math_pow(PyObject *self, PyObject *args) |
| 1493 | { |
| 1494 | PyObject *ox, *oy; |
| 1495 | double r, x, y; |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1496 | int odd_y; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1497 | |
| 1498 | if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy)) |
| 1499 | return NULL; |
| 1500 | x = PyFloat_AsDouble(ox); |
| 1501 | y = PyFloat_AsDouble(oy); |
| 1502 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 1503 | return NULL; |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1504 | |
| 1505 | /* deal directly with IEEE specials, to cope with problems on various |
| 1506 | platforms whose semantics don't exactly match C99 */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1507 | r = 0.; /* silence compiler warning */ |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1508 | if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) { |
| 1509 | errno = 0; |
| 1510 | if (Py_IS_NAN(x)) |
| 1511 | r = y == 0. ? 1. : x; /* NaN**0 = 1 */ |
| 1512 | else if (Py_IS_NAN(y)) |
| 1513 | r = x == 1. ? 1. : y; /* 1**NaN = 1 */ |
| 1514 | else if (Py_IS_INFINITY(x)) { |
| 1515 | odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0; |
| 1516 | if (y > 0.) |
| 1517 | r = odd_y ? x : fabs(x); |
| 1518 | else if (y == 0.) |
| 1519 | r = 1.; |
| 1520 | else /* y < 0. */ |
| 1521 | r = odd_y ? copysign(0., x) : 0.; |
| 1522 | } |
| 1523 | else if (Py_IS_INFINITY(y)) { |
| 1524 | if (fabs(x) == 1.0) |
| 1525 | r = 1.; |
| 1526 | else if (y > 0. && fabs(x) > 1.0) |
| 1527 | r = y; |
| 1528 | else if (y < 0. && fabs(x) < 1.0) { |
| 1529 | r = -y; /* result is +inf */ |
| 1530 | if (x == 0.) /* 0**-inf: divide-by-zero */ |
| 1531 | errno = EDOM; |
| 1532 | } |
| 1533 | else |
| 1534 | r = 0.; |
| 1535 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1536 | } |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1537 | else { |
| 1538 | /* let libm handle finite**finite */ |
| 1539 | errno = 0; |
| 1540 | PyFPE_START_PROTECT("in math_pow", return 0); |
| 1541 | r = pow(x, y); |
| 1542 | PyFPE_END_PROTECT(r); |
| 1543 | /* a NaN result should arise only from (-ve)**(finite |
| 1544 | non-integer); in this case we want to raise ValueError. */ |
| 1545 | if (!Py_IS_FINITE(r)) { |
| 1546 | if (Py_IS_NAN(r)) { |
| 1547 | errno = EDOM; |
| 1548 | } |
| 1549 | /* |
| 1550 | an infinite result here arises either from: |
| 1551 | (A) (+/-0.)**negative (-> divide-by-zero) |
| 1552 | (B) overflow of x**y with x and y finite |
| 1553 | */ |
| 1554 | else if (Py_IS_INFINITY(r)) { |
| 1555 | if (x == 0.) |
| 1556 | errno = EDOM; |
| 1557 | else |
| 1558 | errno = ERANGE; |
| 1559 | } |
| 1560 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | if (errno && is_error(r)) |
| 1564 | return NULL; |
| 1565 | else |
| 1566 | return PyFloat_FromDouble(r); |
| 1567 | } |
| 1568 | |
| 1569 | PyDoc_STRVAR(math_pow_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1570 | "pow(x, y)\n\nReturn x**y (x to the power of y)."); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1571 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1572 | static const double degToRad = Py_MATH_PI / 180.0; |
| 1573 | static const double radToDeg = 180.0 / Py_MATH_PI; |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1574 | |
| 1575 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1576 | math_degrees(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1577 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1578 | double x = PyFloat_AsDouble(arg); |
| 1579 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1580 | return NULL; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1581 | return PyFloat_FromDouble(x * radToDeg); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1584 | PyDoc_STRVAR(math_degrees_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1585 | "degrees(x)\n\n\ |
| 1586 | Convert angle x from radians to degrees."); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1587 | |
| 1588 | static PyObject * |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1589 | math_radians(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1590 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1591 | double x = PyFloat_AsDouble(arg); |
| 1592 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1593 | return NULL; |
| 1594 | return PyFloat_FromDouble(x * degToRad); |
| 1595 | } |
| 1596 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1597 | PyDoc_STRVAR(math_radians_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1598 | "radians(x)\n\n\ |
| 1599 | Convert angle x from degrees to radians."); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1600 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1601 | static PyObject * |
| 1602 | math_isnan(PyObject *self, PyObject *arg) |
| 1603 | { |
| 1604 | double x = PyFloat_AsDouble(arg); |
| 1605 | if (x == -1.0 && PyErr_Occurred()) |
| 1606 | return NULL; |
| 1607 | return PyBool_FromLong((long)Py_IS_NAN(x)); |
| 1608 | } |
| 1609 | |
| 1610 | PyDoc_STRVAR(math_isnan_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1611 | "isnan(x) -> bool\n\n\ |
| 1612 | Check if float x is not a number (NaN)."); |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1613 | |
| 1614 | static PyObject * |
| 1615 | math_isinf(PyObject *self, PyObject *arg) |
| 1616 | { |
| 1617 | double x = PyFloat_AsDouble(arg); |
| 1618 | if (x == -1.0 && PyErr_Occurred()) |
| 1619 | return NULL; |
| 1620 | return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
| 1621 | } |
| 1622 | |
| 1623 | PyDoc_STRVAR(math_isinf_doc, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 1624 | "isinf(x) -> bool\n\n\ |
| 1625 | Check if float x is infinite (positive or negative)."); |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1626 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 1627 | static PyMethodDef math_methods[] = { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1628 | {"acos", math_acos, METH_O, math_acos_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1629 | {"acosh", math_acosh, METH_O, math_acosh_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1630 | {"asin", math_asin, METH_O, math_asin_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1631 | {"asinh", math_asinh, METH_O, math_asinh_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1632 | {"atan", math_atan, METH_O, math_atan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1633 | {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1634 | {"atanh", math_atanh, METH_O, math_atanh_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1635 | {"ceil", math_ceil, METH_O, math_ceil_doc}, |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1636 | {"copysign", math_copysign, METH_VARARGS, math_copysign_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1637 | {"cos", math_cos, METH_O, math_cos_doc}, |
| 1638 | {"cosh", math_cosh, METH_O, math_cosh_doc}, |
| 1639 | {"degrees", math_degrees, METH_O, math_degrees_doc}, |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 1640 | {"erf", math_erf, METH_O, math_erf_doc}, |
| 1641 | {"erfc", math_erfc, METH_O, math_erfc_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1642 | {"exp", math_exp, METH_O, math_exp_doc}, |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 1643 | {"expm1", math_expm1, METH_O, math_expm1_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1644 | {"fabs", math_fabs, METH_O, math_fabs_doc}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1645 | {"factorial", math_factorial, METH_O, math_factorial_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1646 | {"floor", math_floor, METH_O, math_floor_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1647 | {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1648 | {"frexp", math_frexp, METH_O, math_frexp_doc}, |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1649 | {"fsum", math_fsum, METH_O, math_fsum_doc}, |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1650 | {"gamma", math_gamma, METH_O, math_gamma_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1651 | {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1652 | {"isinf", math_isinf, METH_O, math_isinf_doc}, |
| 1653 | {"isnan", math_isnan, METH_O, math_isnan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1654 | {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 1655 | {"lgamma", math_lgamma, METH_O, math_lgamma_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1656 | {"log", math_log, METH_VARARGS, math_log_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1657 | {"log1p", math_log1p, METH_O, math_log1p_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1658 | {"log10", math_log10, METH_O, math_log10_doc}, |
| 1659 | {"modf", math_modf, METH_O, math_modf_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1660 | {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1661 | {"radians", math_radians, METH_O, math_radians_doc}, |
| 1662 | {"sin", math_sin, METH_O, math_sin_doc}, |
| 1663 | {"sinh", math_sinh, METH_O, math_sinh_doc}, |
| 1664 | {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, |
| 1665 | {"tan", math_tan, METH_O, math_tan_doc}, |
| 1666 | {"tanh", math_tanh, METH_O, math_tanh_doc}, |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 1667 | {"trunc", math_trunc, METH_O, math_trunc_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1668 | {NULL, NULL} /* sentinel */ |
| 1669 | }; |
| 1670 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1671 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1672 | PyDoc_STRVAR(module_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 1673 | "This module is always available. It provides access to the\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1674 | "mathematical functions defined by the C standard."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1675 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1676 | |
| 1677 | static struct PyModuleDef mathmodule = { |
| 1678 | PyModuleDef_HEAD_INIT, |
| 1679 | "math", |
| 1680 | module_doc, |
| 1681 | -1, |
| 1682 | math_methods, |
| 1683 | NULL, |
| 1684 | NULL, |
| 1685 | NULL, |
| 1686 | NULL |
| 1687 | }; |
| 1688 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1689 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1690 | PyInit_math(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1691 | { |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1692 | PyObject *m; |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 1693 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1694 | m = PyModule_Create(&mathmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1695 | if (m == NULL) |
| 1696 | goto finally; |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 1697 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1698 | PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); |
| 1699 | PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 1700 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1701 | finally: |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1702 | return m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1703 | } |