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