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 | 6f34109 | 2008-04-18 23:13:07 +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 | 9cae178 | 2009-12-16 20:13:40 +0000 | [diff] [blame^] | 56 | #include "_math.h" |
Michael W. Hudson | 9ef852c | 2005-04-06 13:05:18 +0000 | [diff] [blame] | 57 | #include "longintrepr.h" /* just for SHIFT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | |
Neal Norwitz | 5f95a79 | 2008-01-25 08:04:16 +0000 | [diff] [blame] | 59 | #ifdef _OSF_SOURCE |
| 60 | /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */ |
| 61 | extern double copysign(double, double); |
| 62 | #endif |
| 63 | |
Mark Dickinson | b93fff0 | 2009-09-28 18:54:55 +0000 | [diff] [blame] | 64 | /* |
| 65 | sin(pi*x), giving accurate results for all finite x (especially x |
| 66 | integral or close to an integer). This is here for use in the |
| 67 | reflection formula for the gamma function. It conforms to IEEE |
| 68 | 754-2008 for finite arguments, but not for infinities or nans. |
| 69 | */ |
Tim Peters | a40c793 | 2001-09-05 22:36:56 +0000 | [diff] [blame] | 70 | |
Mark Dickinson | b93fff0 | 2009-09-28 18:54:55 +0000 | [diff] [blame] | 71 | static const double pi = 3.141592653589793238462643383279502884197; |
| 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 | b93fff0 | 2009-09-28 18:54:55 +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 | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 325 | /* |
Mark Dickinson | 9be87bc | 2009-12-11 17:29:33 +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 | |
| 378 | |
| 379 | /* |
Mark Dickinson | 92483cd | 2008-04-20 21:39:04 +0000 | [diff] [blame] | 380 | wrapper for atan2 that deals directly with special cases before |
| 381 | delegating to the platform libm for the remaining cases. This |
| 382 | is necessary to get consistent behaviour across platforms. |
| 383 | Windows, FreeBSD and alpha Tru64 are amongst platforms that don't |
| 384 | always follow C99. |
| 385 | */ |
| 386 | |
| 387 | static double |
| 388 | m_atan2(double y, double x) |
| 389 | { |
| 390 | if (Py_IS_NAN(x) || Py_IS_NAN(y)) |
| 391 | return Py_NAN; |
| 392 | if (Py_IS_INFINITY(y)) { |
| 393 | if (Py_IS_INFINITY(x)) { |
| 394 | if (copysign(1., x) == 1.) |
| 395 | /* atan2(+-inf, +inf) == +-pi/4 */ |
| 396 | return copysign(0.25*Py_MATH_PI, y); |
| 397 | else |
| 398 | /* atan2(+-inf, -inf) == +-pi*3/4 */ |
| 399 | return copysign(0.75*Py_MATH_PI, y); |
| 400 | } |
| 401 | /* atan2(+-inf, x) == +-pi/2 for finite x */ |
| 402 | return copysign(0.5*Py_MATH_PI, y); |
| 403 | } |
| 404 | if (Py_IS_INFINITY(x) || y == 0.) { |
| 405 | if (copysign(1., x) == 1.) |
| 406 | /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */ |
| 407 | return copysign(0., y); |
| 408 | else |
| 409 | /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */ |
| 410 | return copysign(Py_MATH_PI, y); |
| 411 | } |
| 412 | return atan2(y, x); |
| 413 | } |
| 414 | |
| 415 | /* |
Mark Dickinson | 4c96fa5 | 2008-12-11 19:28:08 +0000 | [diff] [blame] | 416 | Various platforms (Solaris, OpenBSD) do nonstandard things for log(0), |
| 417 | log(-ve), log(NaN). Here are wrappers for log and log10 that deal with |
| 418 | special values directly, passing positive non-special values through to |
| 419 | the system log/log10. |
| 420 | */ |
| 421 | |
| 422 | static double |
| 423 | m_log(double x) |
| 424 | { |
| 425 | if (Py_IS_FINITE(x)) { |
| 426 | if (x > 0.0) |
| 427 | return log(x); |
| 428 | errno = EDOM; |
| 429 | if (x == 0.0) |
| 430 | return -Py_HUGE_VAL; /* log(0) = -inf */ |
| 431 | else |
| 432 | return Py_NAN; /* log(-ve) = nan */ |
| 433 | } |
| 434 | else if (Py_IS_NAN(x)) |
| 435 | return x; /* log(nan) = nan */ |
| 436 | else if (x > 0.0) |
| 437 | return x; /* log(inf) = inf */ |
| 438 | else { |
| 439 | errno = EDOM; |
| 440 | return Py_NAN; /* log(-inf) = nan */ |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | static double |
| 445 | m_log10(double x) |
| 446 | { |
| 447 | if (Py_IS_FINITE(x)) { |
| 448 | if (x > 0.0) |
| 449 | return log10(x); |
| 450 | errno = EDOM; |
| 451 | if (x == 0.0) |
| 452 | return -Py_HUGE_VAL; /* log10(0) = -inf */ |
| 453 | else |
| 454 | return Py_NAN; /* log10(-ve) = nan */ |
| 455 | } |
| 456 | else if (Py_IS_NAN(x)) |
| 457 | return x; /* log10(nan) = nan */ |
| 458 | else if (x > 0.0) |
| 459 | return x; /* log10(inf) = inf */ |
| 460 | else { |
| 461 | errno = EDOM; |
| 462 | return Py_NAN; /* log10(-inf) = nan */ |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | |
Mark Dickinson | b93fff0 | 2009-09-28 18:54:55 +0000 | [diff] [blame] | 467 | /* Call is_error when errno != 0, and where x is the result libm |
| 468 | * returned. is_error will usually set up an exception and return |
| 469 | * true (1), but may return false (0) without setting up an exception. |
| 470 | */ |
| 471 | static int |
| 472 | is_error(double x) |
| 473 | { |
| 474 | int result = 1; /* presumption of guilt */ |
| 475 | assert(errno); /* non-zero errno is a precondition for calling */ |
| 476 | if (errno == EDOM) |
| 477 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 478 | |
| 479 | else if (errno == ERANGE) { |
| 480 | /* ANSI C generally requires libm functions to set ERANGE |
| 481 | * on overflow, but also generally *allows* them to set |
| 482 | * ERANGE on underflow too. There's no consistency about |
| 483 | * the latter across platforms. |
| 484 | * Alas, C99 never requires that errno be set. |
| 485 | * Here we suppress the underflow errors (libm functions |
| 486 | * should return a zero on underflow, and +- HUGE_VAL on |
| 487 | * overflow, so testing the result for zero suffices to |
| 488 | * distinguish the cases). |
| 489 | * |
| 490 | * On some platforms (Ubuntu/ia64) it seems that errno can be |
| 491 | * set to ERANGE for subnormal results that do *not* underflow |
| 492 | * to zero. So to be safe, we'll ignore ERANGE whenever the |
| 493 | * function result is less than one in absolute value. |
| 494 | */ |
| 495 | if (fabs(x) < 1.0) |
| 496 | result = 0; |
| 497 | else |
| 498 | PyErr_SetString(PyExc_OverflowError, |
| 499 | "math range error"); |
| 500 | } |
| 501 | else |
| 502 | /* Unexpected math error */ |
| 503 | PyErr_SetFromErrno(PyExc_ValueError); |
| 504 | return result; |
| 505 | } |
| 506 | |
Mark Dickinson | 4c96fa5 | 2008-12-11 19:28:08 +0000 | [diff] [blame] | 507 | /* |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 508 | math_1 is used to wrap a libm function f that takes a double |
| 509 | arguments and returns a double. |
| 510 | |
| 511 | The error reporting follows these rules, which are designed to do |
| 512 | the right thing on C89/C99 platforms and IEEE 754/non IEEE 754 |
| 513 | platforms. |
| 514 | |
| 515 | - a NaN result from non-NaN inputs causes ValueError to be raised |
| 516 | - an infinite result from finite inputs causes OverflowError to be |
| 517 | raised if can_overflow is 1, or raises ValueError if can_overflow |
| 518 | is 0. |
| 519 | - if the result is finite and errno == EDOM then ValueError is |
| 520 | raised |
| 521 | - if the result is finite and nonzero and errno == ERANGE then |
| 522 | OverflowError is raised |
| 523 | |
| 524 | The last rule is used to catch overflow on platforms which follow |
| 525 | C89 but for which HUGE_VAL is not an infinity. |
| 526 | |
| 527 | For the majority of one-argument functions these rules are enough |
| 528 | to ensure that Python's functions behave as specified in 'Annex F' |
| 529 | of the C99 standard, with the 'invalid' and 'divide-by-zero' |
| 530 | floating-point exceptions mapping to Python's ValueError and the |
| 531 | 'overflow' floating-point exception mapping to OverflowError. |
| 532 | math_1 only works for functions that don't have singularities *and* |
| 533 | the possibility of overflow; fortunately, that covers everything we |
| 534 | care about right now. |
| 535 | */ |
| 536 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 537 | static PyObject * |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 538 | math_1(PyObject *arg, double (*func) (double), int can_overflow) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 539 | { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 540 | double x, r; |
| 541 | x = PyFloat_AsDouble(arg); |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 542 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 543 | return NULL; |
| 544 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 545 | PyFPE_START_PROTECT("in math_1", return 0); |
| 546 | r = (*func)(x); |
| 547 | PyFPE_END_PROTECT(r); |
| 548 | if (Py_IS_NAN(r)) { |
| 549 | if (!Py_IS_NAN(x)) |
| 550 | errno = EDOM; |
| 551 | else |
| 552 | errno = 0; |
| 553 | } |
| 554 | else if (Py_IS_INFINITY(r)) { |
| 555 | if (Py_IS_FINITE(x)) |
| 556 | errno = can_overflow ? ERANGE : EDOM; |
| 557 | else |
| 558 | errno = 0; |
| 559 | } |
| 560 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 561 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 562 | else |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 563 | return PyFloat_FromDouble(r); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Mark Dickinson | b93fff0 | 2009-09-28 18:54:55 +0000 | [diff] [blame] | 566 | /* variant of math_1, to be used when the function being wrapped is known to |
| 567 | set errno properly (that is, errno = EDOM for invalid or divide-by-zero, |
| 568 | errno = ERANGE for overflow). */ |
| 569 | |
| 570 | static PyObject * |
| 571 | math_1a(PyObject *arg, double (*func) (double)) |
| 572 | { |
| 573 | double x, r; |
| 574 | x = PyFloat_AsDouble(arg); |
| 575 | if (x == -1.0 && PyErr_Occurred()) |
| 576 | return NULL; |
| 577 | errno = 0; |
| 578 | PyFPE_START_PROTECT("in math_1a", return 0); |
| 579 | r = (*func)(x); |
| 580 | PyFPE_END_PROTECT(r); |
| 581 | if (errno && is_error(r)) |
| 582 | return NULL; |
| 583 | return PyFloat_FromDouble(r); |
| 584 | } |
| 585 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 586 | /* |
| 587 | math_2 is used to wrap a libm function f that takes two double |
| 588 | arguments and returns a double. |
| 589 | |
| 590 | The error reporting follows these rules, which are designed to do |
| 591 | the right thing on C89/C99 platforms and IEEE 754/non IEEE 754 |
| 592 | platforms. |
| 593 | |
| 594 | - a NaN result from non-NaN inputs causes ValueError to be raised |
| 595 | - an infinite result from finite inputs causes OverflowError to be |
| 596 | raised. |
| 597 | - if the result is finite and errno == EDOM then ValueError is |
| 598 | raised |
| 599 | - if the result is finite and nonzero and errno == ERANGE then |
| 600 | OverflowError is raised |
| 601 | |
| 602 | The last rule is used to catch overflow on platforms which follow |
| 603 | C89 but for which HUGE_VAL is not an infinity. |
| 604 | |
| 605 | For most two-argument functions (copysign, fmod, hypot, atan2) |
| 606 | these rules are enough to ensure that Python's functions behave as |
| 607 | specified in 'Annex F' of the C99 standard, with the 'invalid' and |
| 608 | 'divide-by-zero' floating-point exceptions mapping to Python's |
| 609 | ValueError and the 'overflow' floating-point exception mapping to |
| 610 | OverflowError. |
| 611 | */ |
| 612 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 613 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 614 | math_2(PyObject *args, double (*func) (double, double), char *funcname) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 615 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 616 | PyObject *ox, *oy; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 617 | double x, y, r; |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 618 | if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy)) |
| 619 | return NULL; |
| 620 | x = PyFloat_AsDouble(ox); |
| 621 | y = PyFloat_AsDouble(oy); |
| 622 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 623 | return NULL; |
| 624 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 625 | PyFPE_START_PROTECT("in math_2", return 0); |
| 626 | r = (*func)(x, y); |
| 627 | PyFPE_END_PROTECT(r); |
| 628 | if (Py_IS_NAN(r)) { |
| 629 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 630 | errno = EDOM; |
| 631 | else |
| 632 | errno = 0; |
| 633 | } |
| 634 | else if (Py_IS_INFINITY(r)) { |
| 635 | if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 636 | errno = ERANGE; |
| 637 | else |
| 638 | errno = 0; |
| 639 | } |
| 640 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 641 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 642 | else |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 643 | return PyFloat_FromDouble(r); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 646 | #define FUNC1(funcname, func, can_overflow, docstring) \ |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 647 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 648 | return math_1(args, func, can_overflow); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 649 | }\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 650 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 651 | |
Mark Dickinson | b93fff0 | 2009-09-28 18:54:55 +0000 | [diff] [blame] | 652 | #define FUNC1A(funcname, func, docstring) \ |
| 653 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
| 654 | return math_1a(args, func); \ |
| 655 | }\ |
| 656 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
| 657 | |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 658 | #define FUNC2(funcname, func, docstring) \ |
| 659 | static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 660 | return math_2(args, func, #funcname); \ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 661 | }\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 662 | PyDoc_STRVAR(math_##funcname##_doc, docstring); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 663 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 664 | FUNC1(acos, acos, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 665 | "acos(x)\n\nReturn the arc cosine (measured in radians) of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 666 | FUNC1(acosh, acosh, 0, |
| 667 | "acosh(x)\n\nReturn the hyperbolic arc cosine (measured in radians) of x.") |
| 668 | FUNC1(asin, asin, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 669 | "asin(x)\n\nReturn the arc sine (measured in radians) of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 670 | FUNC1(asinh, asinh, 0, |
| 671 | "asinh(x)\n\nReturn the hyperbolic arc sine (measured in radians) of x.") |
| 672 | FUNC1(atan, atan, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 673 | "atan(x)\n\nReturn the arc tangent (measured in radians) of x.") |
Mark Dickinson | 92483cd | 2008-04-20 21:39:04 +0000 | [diff] [blame] | 674 | FUNC2(atan2, m_atan2, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 675 | "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n" |
| 676 | "Unlike atan(y/x), the signs of both x and y are considered.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 677 | FUNC1(atanh, atanh, 0, |
| 678 | "atanh(x)\n\nReturn the hyperbolic arc tangent (measured in radians) of x.") |
| 679 | FUNC1(ceil, ceil, 0, |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 680 | "ceil(x)\n\nReturn the ceiling of x as a float.\n" |
| 681 | "This is the smallest integral value >= x.") |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 682 | FUNC2(copysign, copysign, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 683 | "copysign(x, y)\n\nReturn x with the sign of y.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 684 | FUNC1(cos, cos, 0, |
| 685 | "cos(x)\n\nReturn the cosine of x (measured in radians).") |
| 686 | FUNC1(cosh, cosh, 1, |
| 687 | "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
| 688 | FUNC1(exp, exp, 1, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 689 | "exp(x)\n\nReturn e raised to the power of x.") |
Mark Dickinson | 9cae178 | 2009-12-16 20:13:40 +0000 | [diff] [blame^] | 690 | FUNC1(expm1, m_expm1, 1, |
| 691 | "expm1(x)\n\nReturn exp(x)-1.\n" |
| 692 | "This function avoids the loss of precision involved in the direct " |
| 693 | "evaluation of exp(x)-1 for small x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 694 | FUNC1(fabs, fabs, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 695 | "fabs(x)\n\nReturn the absolute value of the float x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 696 | FUNC1(floor, floor, 0, |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 697 | "floor(x)\n\nReturn the floor of x as a float.\n" |
| 698 | "This is the largest integral value <= x.") |
Mark Dickinson | b93fff0 | 2009-09-28 18:54:55 +0000 | [diff] [blame] | 699 | FUNC1A(gamma, m_tgamma, |
| 700 | "gamma(x)\n\nGamma function at x.") |
Mark Dickinson | 9be87bc | 2009-12-11 17:29:33 +0000 | [diff] [blame] | 701 | FUNC1A(lgamma, m_lgamma, |
| 702 | "lgamma(x)\n\nNatural logarithm of absolute value of Gamma function at x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 703 | FUNC1(log1p, log1p, 1, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 704 | "log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n" |
| 705 | "The result is computed in a way which is accurate for x near zero.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 706 | FUNC1(sin, sin, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 707 | "sin(x)\n\nReturn the sine of x (measured in radians).") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 708 | FUNC1(sinh, sinh, 1, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 709 | "sinh(x)\n\nReturn the hyperbolic sine of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 710 | FUNC1(sqrt, sqrt, 0, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 711 | "sqrt(x)\n\nReturn the square root of x.") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 712 | FUNC1(tan, tan, 0, |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 713 | "tan(x)\n\nReturn the tangent of x (measured in radians).") |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 714 | FUNC1(tanh, tanh, 0, |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 715 | "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 716 | |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 717 | /* Precision summation function as msum() by Raymond Hettinger in |
| 718 | <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090>, |
| 719 | enhanced with the exact partials sum and roundoff from Mark |
| 720 | Dickinson's post at <http://bugs.python.org/file10357/msum4.py>. |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 721 | See those links for more details, proofs and other references. |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 722 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 723 | Note 1: IEEE 754R floating point semantics are assumed, |
| 724 | but the current implementation does not re-establish special |
| 725 | value semantics across iterations (i.e. handling -Inf + Inf). |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 726 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 727 | Note 2: No provision is made for intermediate overflow handling; |
Raymond Hettinger | 2a9179a | 2008-05-29 08:38:23 +0000 | [diff] [blame] | 728 | therefore, sum([1e+308, 1e-308, 1e+308]) returns 1e+308 while |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 729 | sum([1e+308, 1e+308, 1e-308]) raises an OverflowError due to the |
| 730 | overflow of the first partial sum. |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 731 | |
Andrew M. Kuchling | 5f198be | 2008-06-20 02:11:42 +0000 | [diff] [blame] | 732 | Note 3: The intermediate values lo, yr, and hi are declared volatile so |
Mark Dickinson | 2fcd8c9 | 2008-06-20 15:26:19 +0000 | [diff] [blame] | 733 | aggressive compilers won't algebraically reduce lo to always be exactly 0.0. |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 734 | Also, the volatile declaration forces the values to be stored in memory as |
| 735 | regular doubles instead of extended long precision (80-bit) values. This |
Andrew M. Kuchling | 5f198be | 2008-06-20 02:11:42 +0000 | [diff] [blame] | 736 | prevents double rounding because any addition or subtraction of two doubles |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 737 | can be resolved exactly into double-sized hi and lo values. As long as the |
| 738 | hi value gets forced into a double before yr and lo are computed, the extra |
| 739 | bits in downstream extended precision operations (x87 for example) will be |
| 740 | exactly zero and therefore can be losslessly stored back into a double, |
| 741 | thereby preventing double rounding. |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 742 | |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 743 | Note 4: A similar implementation is in Modules/cmathmodule.c. |
| 744 | Be sure to update both when making changes. |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 745 | |
Mark Dickinson | ff3fdce | 2008-07-30 16:25:16 +0000 | [diff] [blame] | 746 | Note 5: The signature of math.fsum() differs from __builtin__.sum() |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 747 | because the start argument doesn't make sense in the context of |
| 748 | accurate summation. Since the partials table is collapsed before |
| 749 | returning a result, sum(seq2, start=sum(seq1)) may not equal the |
| 750 | accurate result returned by sum(itertools.chain(seq1, seq2)). |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 751 | */ |
| 752 | |
| 753 | #define NUM_PARTIALS 32 /* initial partials array size, on stack */ |
| 754 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 755 | /* Extend the partials array p[] by doubling its size. */ |
| 756 | static int /* non-zero on error */ |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 757 | _fsum_realloc(double **p_ptr, Py_ssize_t n, |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 758 | double *ps, Py_ssize_t *m_ptr) |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 759 | { |
| 760 | void *v = NULL; |
| 761 | Py_ssize_t m = *m_ptr; |
| 762 | |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 763 | m += m; /* double */ |
| 764 | if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) { |
| 765 | double *p = *p_ptr; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 766 | if (p == ps) { |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 767 | v = PyMem_Malloc(sizeof(double) * m); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 768 | if (v != NULL) |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 769 | memcpy(v, ps, sizeof(double) * n); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 770 | } |
| 771 | else |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 772 | v = PyMem_Realloc(p, sizeof(double) * m); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 773 | } |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 774 | if (v == NULL) { /* size overflow or no memory */ |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 775 | PyErr_SetString(PyExc_MemoryError, "math.fsum partials"); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 776 | return 1; |
| 777 | } |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 778 | *p_ptr = (double*) v; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 779 | *m_ptr = m; |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | /* Full precision summation of a sequence of floats. |
| 784 | |
| 785 | def msum(iterable): |
| 786 | partials = [] # sorted, non-overlapping partial sums |
| 787 | for x in iterable: |
| 788 | i = 0 |
| 789 | for y in partials: |
| 790 | if abs(x) < abs(y): |
| 791 | x, y = y, x |
| 792 | hi = x + y |
| 793 | lo = y - (hi - x) |
| 794 | if lo: |
| 795 | partials[i] = lo |
| 796 | i += 1 |
| 797 | x = hi |
| 798 | partials[i:] = [x] |
| 799 | return sum_exact(partials) |
| 800 | |
| 801 | Rounded x+y stored in hi with the roundoff stored in lo. Together hi+lo |
| 802 | are exactly equal to x+y. The inner loop applies hi/lo summation to each |
| 803 | partial so that the list of partial sums remains exact. |
| 804 | |
| 805 | Sum_exact() adds the partial sums exactly and correctly rounds the final |
| 806 | result (using the round-half-to-even rule). The items in partials remain |
| 807 | non-zero, non-special, non-overlapping and strictly increasing in |
| 808 | magnitude, but possibly not all having the same sign. |
| 809 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 810 | Depends on IEEE 754 arithmetic guarantees and half-even rounding. |
| 811 | */ |
| 812 | |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 813 | static PyObject* |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 814 | math_fsum(PyObject *self, PyObject *seq) |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 815 | { |
| 816 | PyObject *item, *iter, *sum = NULL; |
| 817 | Py_ssize_t i, j, n = 0, m = NUM_PARTIALS; |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 818 | double x, y, t, ps[NUM_PARTIALS], *p = ps; |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 819 | double xsave, special_sum = 0.0, inf_sum = 0.0; |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 820 | volatile double hi, yr, lo; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 821 | |
| 822 | iter = PyObject_GetIter(seq); |
| 823 | if (iter == NULL) |
| 824 | return NULL; |
| 825 | |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 826 | PyFPE_START_PROTECT("fsum", Py_DECREF(iter); return NULL) |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 827 | |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 828 | for(;;) { /* for x in iterable */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 829 | assert(0 <= n && n <= m); |
| 830 | assert((m == NUM_PARTIALS && p == ps) || |
| 831 | (m > NUM_PARTIALS && p != NULL)); |
| 832 | |
| 833 | item = PyIter_Next(iter); |
| 834 | if (item == NULL) { |
| 835 | if (PyErr_Occurred()) |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 836 | goto _fsum_error; |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 837 | break; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 838 | } |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 839 | x = PyFloat_AsDouble(item); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 840 | Py_DECREF(item); |
| 841 | if (PyErr_Occurred()) |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 842 | goto _fsum_error; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 843 | |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 844 | xsave = x; |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 845 | for (i = j = 0; j < n; j++) { /* for y in partials */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 846 | y = p[j]; |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 847 | if (fabs(x) < fabs(y)) { |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 848 | t = x; x = y; y = t; |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 849 | } |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 850 | hi = x + y; |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 851 | yr = hi - x; |
| 852 | lo = y - yr; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 853 | if (lo != 0.0) |
| 854 | p[i++] = lo; |
| 855 | x = hi; |
| 856 | } |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 857 | |
| 858 | n = i; /* ps[i:] = [x] */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 859 | if (x != 0.0) { |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 860 | if (! Py_IS_FINITE(x)) { |
| 861 | /* a nonfinite x could arise either as |
| 862 | a result of intermediate overflow, or |
| 863 | as a result of a nan or inf in the |
| 864 | summands */ |
| 865 | if (Py_IS_FINITE(xsave)) { |
| 866 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 867 | "intermediate overflow in fsum"); |
| 868 | goto _fsum_error; |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 869 | } |
| 870 | if (Py_IS_INFINITY(xsave)) |
| 871 | inf_sum += xsave; |
| 872 | special_sum += xsave; |
| 873 | /* reset partials */ |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 874 | n = 0; |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 875 | } |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 876 | else if (n >= m && _fsum_realloc(&p, n, ps, &m)) |
| 877 | goto _fsum_error; |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 878 | else |
| 879 | p[n++] = x; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 882 | |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 883 | if (special_sum != 0.0) { |
| 884 | if (Py_IS_NAN(inf_sum)) |
| 885 | PyErr_SetString(PyExc_ValueError, |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 886 | "-inf + inf in fsum"); |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 887 | else |
| 888 | sum = PyFloat_FromDouble(special_sum); |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 889 | goto _fsum_error; |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Raymond Hettinger | ef712d6 | 2008-05-30 18:20:50 +0000 | [diff] [blame] | 892 | hi = 0.0; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 893 | if (n > 0) { |
| 894 | hi = p[--n]; |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 895 | /* sum_exact(ps, hi) from the top, stop when the sum becomes |
| 896 | inexact. */ |
| 897 | while (n > 0) { |
| 898 | x = hi; |
| 899 | y = p[--n]; |
| 900 | assert(fabs(y) < fabs(x)); |
| 901 | hi = x + y; |
| 902 | yr = hi - x; |
| 903 | lo = y - yr; |
| 904 | if (lo != 0.0) |
| 905 | break; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 906 | } |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 907 | /* Make half-even rounding work across multiple partials. |
| 908 | Needed so that sum([1e-16, 1, 1e16]) will round-up the last |
| 909 | digit to two instead of down to zero (the 1e-16 makes the 1 |
| 910 | slightly closer to two). With a potential 1 ULP rounding |
Mark Dickinson | ff3fdce | 2008-07-30 16:25:16 +0000 | [diff] [blame] | 911 | error fixed-up, math.fsum() can guarantee commutativity. */ |
Mark Dickinson | abe0aee | 2008-07-30 12:01:41 +0000 | [diff] [blame] | 912 | if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) || |
| 913 | (lo > 0.0 && p[n-1] > 0.0))) { |
| 914 | y = lo * 2.0; |
| 915 | x = hi + y; |
| 916 | yr = x - hi; |
| 917 | if (y == yr) |
| 918 | hi = x; |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 919 | } |
| 920 | } |
Raymond Hettinger | d623414 | 2008-06-09 11:24:47 +0000 | [diff] [blame] | 921 | sum = PyFloat_FromDouble(hi); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 922 | |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 923 | _fsum_error: |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 924 | PyFPE_END_PROTECT(hi) |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 925 | Py_DECREF(iter); |
| 926 | if (p != ps) |
| 927 | PyMem_Free(p); |
| 928 | return sum; |
| 929 | } |
| 930 | |
| 931 | #undef NUM_PARTIALS |
| 932 | |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 933 | PyDoc_STRVAR(math_fsum_doc, |
Georg Brandl | 40777e6 | 2009-10-29 20:38:32 +0000 | [diff] [blame] | 934 | "fsum(iterable)\n\n\ |
Raymond Hettinger | 778d5cc | 2008-05-23 04:32:43 +0000 | [diff] [blame] | 935 | Return an accurate floating point sum of values in the iterable.\n\ |
| 936 | Assumes IEEE-754 floating point arithmetic."); |
Mark Dickinson | 99dfe92 | 2008-05-23 01:35:30 +0000 | [diff] [blame] | 937 | |
Raymond Hettinger | ecbdd2e | 2008-06-09 06:54:45 +0000 | [diff] [blame] | 938 | static PyObject * |
| 939 | math_factorial(PyObject *self, PyObject *arg) |
| 940 | { |
| 941 | long i, x; |
| 942 | PyObject *result, *iobj, *newresult; |
| 943 | |
| 944 | if (PyFloat_Check(arg)) { |
| 945 | double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg); |
| 946 | if (dx != floor(dx)) { |
| 947 | PyErr_SetString(PyExc_ValueError, |
| 948 | "factorial() only accepts integral values"); |
| 949 | return NULL; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | x = PyInt_AsLong(arg); |
| 954 | if (x == -1 && PyErr_Occurred()) |
| 955 | return NULL; |
| 956 | if (x < 0) { |
| 957 | PyErr_SetString(PyExc_ValueError, |
| 958 | "factorial() not defined for negative values"); |
| 959 | return NULL; |
| 960 | } |
| 961 | |
| 962 | result = (PyObject *)PyInt_FromLong(1); |
| 963 | if (result == NULL) |
| 964 | return NULL; |
| 965 | for (i=1 ; i<=x ; i++) { |
| 966 | iobj = (PyObject *)PyInt_FromLong(i); |
| 967 | if (iobj == NULL) |
| 968 | goto error; |
| 969 | newresult = PyNumber_Multiply(result, iobj); |
| 970 | Py_DECREF(iobj); |
| 971 | if (newresult == NULL) |
| 972 | goto error; |
| 973 | Py_DECREF(result); |
| 974 | result = newresult; |
| 975 | } |
| 976 | return result; |
| 977 | |
| 978 | error: |
| 979 | Py_DECREF(result); |
Raymond Hettinger | ecbdd2e | 2008-06-09 06:54:45 +0000 | [diff] [blame] | 980 | return NULL; |
| 981 | } |
| 982 | |
Benjamin Peterson | fed67fd | 2008-12-20 02:57:19 +0000 | [diff] [blame] | 983 | PyDoc_STRVAR(math_factorial_doc, |
| 984 | "factorial(x) -> Integral\n" |
| 985 | "\n" |
| 986 | "Find x!. Raise a ValueError if x is negative or non-integral."); |
Raymond Hettinger | ecbdd2e | 2008-06-09 06:54:45 +0000 | [diff] [blame] | 987 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 988 | static PyObject * |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 989 | math_trunc(PyObject *self, PyObject *number) |
| 990 | { |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 991 | return PyObject_CallMethod(number, "__trunc__", NULL); |
| 992 | } |
| 993 | |
| 994 | PyDoc_STRVAR(math_trunc_doc, |
| 995 | "trunc(x:Real) -> Integral\n" |
| 996 | "\n" |
Raymond Hettinger | fe424f7 | 2008-02-02 05:24:44 +0000 | [diff] [blame] | 997 | "Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method."); |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 998 | |
| 999 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1000 | math_frexp(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1001 | { |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1002 | int i; |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1003 | double x = PyFloat_AsDouble(arg); |
| 1004 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1005 | return NULL; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1006 | /* deal with special cases directly, to sidestep platform |
| 1007 | differences */ |
| 1008 | if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) { |
| 1009 | i = 0; |
| 1010 | } |
| 1011 | else { |
| 1012 | PyFPE_START_PROTECT("in math_frexp", return 0); |
| 1013 | x = frexp(x, &i); |
| 1014 | PyFPE_END_PROTECT(x); |
| 1015 | } |
| 1016 | return Py_BuildValue("(di)", x, i); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1019 | PyDoc_STRVAR(math_frexp_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 1020 | "frexp(x)\n" |
| 1021 | "\n" |
| 1022 | "Return the mantissa and exponent of x, as pair (m, e).\n" |
| 1023 | "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] | 1024 | "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] | 1025 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 1026 | static PyObject * |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1027 | math_ldexp(PyObject *self, PyObject *args) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1028 | { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1029 | double x, r; |
Mark Dickinson | f8476c1 | 2008-05-09 17:54:23 +0000 | [diff] [blame] | 1030 | PyObject *oexp; |
| 1031 | long exp; |
| 1032 | if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp)) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1033 | return NULL; |
Mark Dickinson | f8476c1 | 2008-05-09 17:54:23 +0000 | [diff] [blame] | 1034 | |
| 1035 | if (PyLong_Check(oexp)) { |
| 1036 | /* on overflow, replace exponent with either LONG_MAX |
| 1037 | or LONG_MIN, depending on the sign. */ |
| 1038 | exp = PyLong_AsLong(oexp); |
| 1039 | if (exp == -1 && PyErr_Occurred()) { |
| 1040 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 1041 | if (Py_SIZE(oexp) < 0) { |
| 1042 | exp = LONG_MIN; |
| 1043 | } |
| 1044 | else { |
| 1045 | exp = LONG_MAX; |
| 1046 | } |
| 1047 | PyErr_Clear(); |
| 1048 | } |
| 1049 | else { |
| 1050 | /* propagate any unexpected exception */ |
| 1051 | return NULL; |
| 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | else if (PyInt_Check(oexp)) { |
| 1056 | exp = PyInt_AS_LONG(oexp); |
| 1057 | } |
| 1058 | else { |
| 1059 | PyErr_SetString(PyExc_TypeError, |
| 1060 | "Expected an int or long as second argument " |
| 1061 | "to ldexp."); |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | |
| 1065 | if (x == 0. || !Py_IS_FINITE(x)) { |
| 1066 | /* NaNs, zeros and infinities are returned unchanged */ |
| 1067 | r = x; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1068 | errno = 0; |
Mark Dickinson | f8476c1 | 2008-05-09 17:54:23 +0000 | [diff] [blame] | 1069 | } else if (exp > INT_MAX) { |
| 1070 | /* overflow */ |
| 1071 | r = copysign(Py_HUGE_VAL, x); |
| 1072 | errno = ERANGE; |
| 1073 | } else if (exp < INT_MIN) { |
| 1074 | /* underflow to +-0 */ |
| 1075 | r = copysign(0., x); |
| 1076 | errno = 0; |
| 1077 | } else { |
| 1078 | errno = 0; |
| 1079 | PyFPE_START_PROTECT("in math_ldexp", return 0); |
| 1080 | r = ldexp(x, (int)exp); |
| 1081 | PyFPE_END_PROTECT(r); |
| 1082 | if (Py_IS_INFINITY(r)) |
| 1083 | errno = ERANGE; |
| 1084 | } |
| 1085 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1086 | if (errno && is_error(r)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 1087 | return NULL; |
Mark Dickinson | f8476c1 | 2008-05-09 17:54:23 +0000 | [diff] [blame] | 1088 | return PyFloat_FromDouble(r); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1091 | PyDoc_STRVAR(math_ldexp_doc, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 1092 | "ldexp(x, i)\n\n\ |
| 1093 | Return x * (2**i)."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1094 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 1095 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1096 | math_modf(PyObject *self, PyObject *arg) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1097 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1098 | double y, x = PyFloat_AsDouble(arg); |
| 1099 | if (x == -1.0 && PyErr_Occurred()) |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1100 | return NULL; |
Mark Dickinson | b2f7090 | 2008-04-20 01:39:24 +0000 | [diff] [blame] | 1101 | /* some platforms don't do the right thing for NaNs and |
| 1102 | infinities, so we take care of special cases directly. */ |
| 1103 | if (!Py_IS_FINITE(x)) { |
| 1104 | if (Py_IS_INFINITY(x)) |
| 1105 | return Py_BuildValue("(dd)", copysign(0., x), x); |
| 1106 | else if (Py_IS_NAN(x)) |
| 1107 | return Py_BuildValue("(dd)", x, x); |
| 1108 | } |
| 1109 | |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1110 | errno = 0; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1111 | PyFPE_START_PROTECT("in math_modf", return 0); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1112 | x = modf(x, &y); |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1113 | PyFPE_END_PROTECT(x); |
| 1114 | return Py_BuildValue("(dd)", x, y); |
Guido van Rossum | d18ad58 | 1991-10-24 14:57:21 +0000 | [diff] [blame] | 1115 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1116 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1117 | PyDoc_STRVAR(math_modf_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 1118 | "modf(x)\n" |
| 1119 | "\n" |
| 1120 | "Return the fractional and integer parts of x. Both results carry the sign\n" |
Benjamin Peterson | 9de7298 | 2008-12-20 22:49:24 +0000 | [diff] [blame] | 1121 | "of x and are floats."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1122 | |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1123 | /* A decent logarithm is easy to compute even for huge longs, but libm can't |
| 1124 | do that by itself -- loghelper can. func is log or log10, and name is |
| 1125 | "log" or "log10". Note that overflow isn't possible: a long can contain |
| 1126 | no more than INT_MAX * SHIFT bits, so has value certainly less than |
| 1127 | 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is |
| 1128 | small enough to fit in an IEEE single. log and log10 are even smaller. |
| 1129 | */ |
| 1130 | |
| 1131 | static PyObject* |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1132 | loghelper(PyObject* arg, double (*func)(double), char *funcname) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1133 | { |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1134 | /* If it is long, do it ourselves. */ |
| 1135 | if (PyLong_Check(arg)) { |
| 1136 | double x; |
| 1137 | int e; |
| 1138 | x = _PyLong_AsScaledDouble(arg, &e); |
| 1139 | if (x <= 0.0) { |
| 1140 | PyErr_SetString(PyExc_ValueError, |
| 1141 | "math domain error"); |
| 1142 | return NULL; |
| 1143 | } |
Christian Heimes | 543cabc | 2008-01-25 14:54:23 +0000 | [diff] [blame] | 1144 | /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~= |
| 1145 | log(x) + log(2) * e * PyLong_SHIFT. |
| 1146 | CAUTION: e*PyLong_SHIFT may overflow using int arithmetic, |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1147 | so force use of double. */ |
Christian Heimes | 543cabc | 2008-01-25 14:54:23 +0000 | [diff] [blame] | 1148 | x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1149 | return PyFloat_FromDouble(x); |
| 1150 | } |
| 1151 | |
| 1152 | /* Else let libm handle it by itself. */ |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1153 | return math_1(arg, func, 0); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | static PyObject * |
| 1157 | math_log(PyObject *self, PyObject *args) |
| 1158 | { |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1159 | PyObject *arg; |
| 1160 | PyObject *base = NULL; |
| 1161 | PyObject *num, *den; |
| 1162 | PyObject *ans; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1163 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1164 | if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base)) |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1165 | return NULL; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1166 | |
Mark Dickinson | 4c96fa5 | 2008-12-11 19:28:08 +0000 | [diff] [blame] | 1167 | num = loghelper(arg, m_log, "log"); |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1168 | if (num == NULL || base == NULL) |
| 1169 | return num; |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1170 | |
Mark Dickinson | 4c96fa5 | 2008-12-11 19:28:08 +0000 | [diff] [blame] | 1171 | den = loghelper(base, m_log, "log"); |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1172 | if (den == NULL) { |
| 1173 | Py_DECREF(num); |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | |
| 1177 | ans = PyNumber_Divide(num, den); |
| 1178 | Py_DECREF(num); |
| 1179 | Py_DECREF(den); |
| 1180 | return ans; |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1183 | PyDoc_STRVAR(math_log_doc, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 1184 | "log(x[, base])\n\n\ |
| 1185 | Return the logarithm of x to the given base.\n\ |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 1186 | 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] | 1187 | |
| 1188 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1189 | math_log10(PyObject *self, PyObject *arg) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1190 | { |
Mark Dickinson | 4c96fa5 | 2008-12-11 19:28:08 +0000 | [diff] [blame] | 1191 | return loghelper(arg, m_log10, "log10"); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1194 | PyDoc_STRVAR(math_log10_doc, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 1195 | "log10(x)\n\nReturn the base 10 logarithm of x."); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1196 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1197 | static PyObject * |
| 1198 | math_fmod(PyObject *self, PyObject *args) |
| 1199 | { |
| 1200 | PyObject *ox, *oy; |
| 1201 | double r, x, y; |
| 1202 | if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy)) |
| 1203 | return NULL; |
| 1204 | x = PyFloat_AsDouble(ox); |
| 1205 | y = PyFloat_AsDouble(oy); |
| 1206 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 1207 | return NULL; |
| 1208 | /* fmod(x, +/-Inf) returns x for finite x. */ |
| 1209 | if (Py_IS_INFINITY(y) && Py_IS_FINITE(x)) |
| 1210 | return PyFloat_FromDouble(x); |
| 1211 | errno = 0; |
| 1212 | PyFPE_START_PROTECT("in math_fmod", return 0); |
| 1213 | r = fmod(x, y); |
| 1214 | PyFPE_END_PROTECT(r); |
| 1215 | if (Py_IS_NAN(r)) { |
| 1216 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 1217 | errno = EDOM; |
| 1218 | else |
| 1219 | errno = 0; |
| 1220 | } |
| 1221 | if (errno && is_error(r)) |
| 1222 | return NULL; |
| 1223 | else |
| 1224 | return PyFloat_FromDouble(r); |
| 1225 | } |
| 1226 | |
| 1227 | PyDoc_STRVAR(math_fmod_doc, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 1228 | "fmod(x, y)\n\nReturn fmod(x, y), according to platform C." |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1229 | " x % y may differ."); |
| 1230 | |
| 1231 | static PyObject * |
| 1232 | math_hypot(PyObject *self, PyObject *args) |
| 1233 | { |
| 1234 | PyObject *ox, *oy; |
| 1235 | double r, x, y; |
| 1236 | if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy)) |
| 1237 | return NULL; |
| 1238 | x = PyFloat_AsDouble(ox); |
| 1239 | y = PyFloat_AsDouble(oy); |
| 1240 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 1241 | return NULL; |
| 1242 | /* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */ |
| 1243 | if (Py_IS_INFINITY(x)) |
| 1244 | return PyFloat_FromDouble(fabs(x)); |
| 1245 | if (Py_IS_INFINITY(y)) |
| 1246 | return PyFloat_FromDouble(fabs(y)); |
| 1247 | errno = 0; |
| 1248 | PyFPE_START_PROTECT("in math_hypot", return 0); |
| 1249 | r = hypot(x, y); |
| 1250 | PyFPE_END_PROTECT(r); |
| 1251 | if (Py_IS_NAN(r)) { |
| 1252 | if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 1253 | errno = EDOM; |
| 1254 | else |
| 1255 | errno = 0; |
| 1256 | } |
| 1257 | else if (Py_IS_INFINITY(r)) { |
| 1258 | if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 1259 | errno = ERANGE; |
| 1260 | else |
| 1261 | errno = 0; |
| 1262 | } |
| 1263 | if (errno && is_error(r)) |
| 1264 | return NULL; |
| 1265 | else |
| 1266 | return PyFloat_FromDouble(r); |
| 1267 | } |
| 1268 | |
| 1269 | PyDoc_STRVAR(math_hypot_doc, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 1270 | "hypot(x, y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y)."); |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1271 | |
| 1272 | /* pow can't use math_2, but needs its own wrapper: the problem is |
| 1273 | that an infinite result can arise either as a result of overflow |
| 1274 | (in which case OverflowError should be raised) or as a result of |
| 1275 | e.g. 0.**-5. (for which ValueError needs to be raised.) |
| 1276 | */ |
| 1277 | |
| 1278 | static PyObject * |
| 1279 | math_pow(PyObject *self, PyObject *args) |
| 1280 | { |
| 1281 | PyObject *ox, *oy; |
| 1282 | double r, x, y; |
Mark Dickinson | cec3f13 | 2008-04-20 04:13:13 +0000 | [diff] [blame] | 1283 | int odd_y; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1284 | |
| 1285 | if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy)) |
| 1286 | return NULL; |
| 1287 | x = PyFloat_AsDouble(ox); |
| 1288 | y = PyFloat_AsDouble(oy); |
| 1289 | if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 1290 | return NULL; |
Mark Dickinson | a1293eb | 2008-04-19 19:41:52 +0000 | [diff] [blame] | 1291 | |
Mark Dickinson | cec3f13 | 2008-04-20 04:13:13 +0000 | [diff] [blame] | 1292 | /* deal directly with IEEE specials, to cope with problems on various |
| 1293 | platforms whose semantics don't exactly match C99 */ |
Mark Dickinson | 0da94c8 | 2008-04-21 01:55:50 +0000 | [diff] [blame] | 1294 | r = 0.; /* silence compiler warning */ |
Mark Dickinson | cec3f13 | 2008-04-20 04:13:13 +0000 | [diff] [blame] | 1295 | if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) { |
| 1296 | errno = 0; |
| 1297 | if (Py_IS_NAN(x)) |
| 1298 | r = y == 0. ? 1. : x; /* NaN**0 = 1 */ |
| 1299 | else if (Py_IS_NAN(y)) |
| 1300 | r = x == 1. ? 1. : y; /* 1**NaN = 1 */ |
| 1301 | else if (Py_IS_INFINITY(x)) { |
| 1302 | odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0; |
| 1303 | if (y > 0.) |
| 1304 | r = odd_y ? x : fabs(x); |
| 1305 | else if (y == 0.) |
| 1306 | r = 1.; |
| 1307 | else /* y < 0. */ |
| 1308 | r = odd_y ? copysign(0., x) : 0.; |
| 1309 | } |
| 1310 | else if (Py_IS_INFINITY(y)) { |
| 1311 | if (fabs(x) == 1.0) |
| 1312 | r = 1.; |
| 1313 | else if (y > 0. && fabs(x) > 1.0) |
| 1314 | r = y; |
| 1315 | else if (y < 0. && fabs(x) < 1.0) { |
| 1316 | r = -y; /* result is +inf */ |
| 1317 | if (x == 0.) /* 0**-inf: divide-by-zero */ |
| 1318 | errno = EDOM; |
| 1319 | } |
| 1320 | else |
| 1321 | r = 0.; |
| 1322 | } |
Mark Dickinson | e941d97 | 2008-04-19 18:51:48 +0000 | [diff] [blame] | 1323 | } |
Mark Dickinson | cec3f13 | 2008-04-20 04:13:13 +0000 | [diff] [blame] | 1324 | else { |
| 1325 | /* let libm handle finite**finite */ |
| 1326 | errno = 0; |
| 1327 | PyFPE_START_PROTECT("in math_pow", return 0); |
| 1328 | r = pow(x, y); |
| 1329 | PyFPE_END_PROTECT(r); |
| 1330 | /* a NaN result should arise only from (-ve)**(finite |
| 1331 | non-integer); in this case we want to raise ValueError. */ |
| 1332 | if (!Py_IS_FINITE(r)) { |
| 1333 | if (Py_IS_NAN(r)) { |
| 1334 | errno = EDOM; |
| 1335 | } |
| 1336 | /* |
| 1337 | an infinite result here arises either from: |
| 1338 | (A) (+/-0.)**negative (-> divide-by-zero) |
| 1339 | (B) overflow of x**y with x and y finite |
| 1340 | */ |
| 1341 | else if (Py_IS_INFINITY(r)) { |
| 1342 | if (x == 0.) |
| 1343 | errno = EDOM; |
| 1344 | else |
| 1345 | errno = ERANGE; |
| 1346 | } |
| 1347 | } |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | if (errno && is_error(r)) |
| 1351 | return NULL; |
| 1352 | else |
| 1353 | return PyFloat_FromDouble(r); |
| 1354 | } |
| 1355 | |
| 1356 | PyDoc_STRVAR(math_pow_doc, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 1357 | "pow(x, y)\n\nReturn x**y (x to the power of y)."); |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1358 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 1359 | static const double degToRad = Py_MATH_PI / 180.0; |
| 1360 | static const double radToDeg = 180.0 / Py_MATH_PI; |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1361 | |
| 1362 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1363 | math_degrees(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1364 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1365 | double x = PyFloat_AsDouble(arg); |
| 1366 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1367 | return NULL; |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 1368 | return PyFloat_FromDouble(x * radToDeg); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1371 | PyDoc_STRVAR(math_degrees_doc, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 1372 | "degrees(x)\n\n\ |
| 1373 | Convert angle x from radians to degrees."); |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1374 | |
| 1375 | static PyObject * |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1376 | math_radians(PyObject *self, PyObject *arg) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1377 | { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1378 | double x = PyFloat_AsDouble(arg); |
| 1379 | if (x == -1.0 && PyErr_Occurred()) |
Raymond Hettinger | d6f2267 | 2002-05-13 03:56:10 +0000 | [diff] [blame] | 1380 | return NULL; |
| 1381 | return PyFloat_FromDouble(x * degToRad); |
| 1382 | } |
| 1383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1384 | PyDoc_STRVAR(math_radians_doc, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 1385 | "radians(x)\n\n\ |
| 1386 | Convert angle x from degrees to radians."); |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 1387 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 1388 | static PyObject * |
| 1389 | math_isnan(PyObject *self, PyObject *arg) |
| 1390 | { |
| 1391 | double x = PyFloat_AsDouble(arg); |
| 1392 | if (x == -1.0 && PyErr_Occurred()) |
| 1393 | return NULL; |
| 1394 | return PyBool_FromLong((long)Py_IS_NAN(x)); |
| 1395 | } |
| 1396 | |
| 1397 | PyDoc_STRVAR(math_isnan_doc, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 1398 | "isnan(x) -> bool\n\n\ |
| 1399 | Check if float x is not a number (NaN)."); |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 1400 | |
| 1401 | static PyObject * |
| 1402 | math_isinf(PyObject *self, PyObject *arg) |
| 1403 | { |
| 1404 | double x = PyFloat_AsDouble(arg); |
| 1405 | if (x == -1.0 && PyErr_Occurred()) |
| 1406 | return NULL; |
| 1407 | return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
| 1408 | } |
| 1409 | |
| 1410 | PyDoc_STRVAR(math_isinf_doc, |
Georg Brandl | a8f8bed2 | 2009-10-29 20:54:03 +0000 | [diff] [blame] | 1411 | "isinf(x) -> bool\n\n\ |
| 1412 | Check if float x is infinite (positive or negative)."); |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 1413 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 1414 | static PyMethodDef math_methods[] = { |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1415 | {"acos", math_acos, METH_O, math_acos_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1416 | {"acosh", math_acosh, METH_O, math_acosh_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1417 | {"asin", math_asin, METH_O, math_asin_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1418 | {"asinh", math_asinh, METH_O, math_asinh_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1419 | {"atan", math_atan, METH_O, math_atan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1420 | {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1421 | {"atanh", math_atanh, METH_O, math_atanh_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1422 | {"ceil", math_ceil, METH_O, math_ceil_doc}, |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 1423 | {"copysign", math_copysign, METH_VARARGS, math_copysign_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1424 | {"cos", math_cos, METH_O, math_cos_doc}, |
| 1425 | {"cosh", math_cosh, METH_O, math_cosh_doc}, |
| 1426 | {"degrees", math_degrees, METH_O, math_degrees_doc}, |
| 1427 | {"exp", math_exp, METH_O, math_exp_doc}, |
Mark Dickinson | 9cae178 | 2009-12-16 20:13:40 +0000 | [diff] [blame^] | 1428 | {"expm1", math_expm1, METH_O, math_expm1_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1429 | {"fabs", math_fabs, METH_O, math_fabs_doc}, |
Raymond Hettinger | ecbdd2e | 2008-06-09 06:54:45 +0000 | [diff] [blame] | 1430 | {"factorial", math_factorial, METH_O, math_factorial_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1431 | {"floor", math_floor, METH_O, math_floor_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1432 | {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1433 | {"frexp", math_frexp, METH_O, math_frexp_doc}, |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 1434 | {"fsum", math_fsum, METH_O, math_fsum_doc}, |
Mark Dickinson | b93fff0 | 2009-09-28 18:54:55 +0000 | [diff] [blame] | 1435 | {"gamma", math_gamma, METH_O, math_gamma_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1436 | {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 1437 | {"isinf", math_isinf, METH_O, math_isinf_doc}, |
| 1438 | {"isnan", math_isnan, METH_O, math_isnan_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1439 | {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
Mark Dickinson | 9be87bc | 2009-12-11 17:29:33 +0000 | [diff] [blame] | 1440 | {"lgamma", math_lgamma, METH_O, math_lgamma_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1441 | {"log", math_log, METH_VARARGS, math_log_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1442 | {"log1p", math_log1p, METH_O, math_log1p_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1443 | {"log10", math_log10, METH_O, math_log10_doc}, |
| 1444 | {"modf", math_modf, METH_O, math_modf_doc}, |
Fred Drake | 40c4868 | 2000-07-03 18:11:56 +0000 | [diff] [blame] | 1445 | {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
Neal Norwitz | 45e230a | 2006-11-19 21:26:53 +0000 | [diff] [blame] | 1446 | {"radians", math_radians, METH_O, math_radians_doc}, |
| 1447 | {"sin", math_sin, METH_O, math_sin_doc}, |
| 1448 | {"sinh", math_sinh, METH_O, math_sinh_doc}, |
| 1449 | {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, |
| 1450 | {"tan", math_tan, METH_O, math_tan_doc}, |
| 1451 | {"tanh", math_tanh, METH_O, math_tanh_doc}, |
Mark Dickinson | fef6b13 | 2008-07-30 16:20:10 +0000 | [diff] [blame] | 1452 | {"trunc", math_trunc, METH_O, math_trunc_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1453 | {NULL, NULL} /* sentinel */ |
| 1454 | }; |
| 1455 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1456 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1457 | PyDoc_STRVAR(module_doc, |
Tim Peters | 63c9453 | 2001-09-04 23:17:42 +0000 | [diff] [blame] | 1458 | "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] | 1459 | "mathematical functions defined by the C standard."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1460 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1461 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1462 | initmath(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1463 | { |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1464 | PyObject *m; |
Tim Peters | fe71f81 | 2001-08-07 22:10:00 +0000 | [diff] [blame] | 1465 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1466 | m = Py_InitModule3("math", math_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1467 | if (m == NULL) |
| 1468 | goto finally; |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 1469 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1470 | PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); |
| 1471 | PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); |
Barry Warsaw | fc93f75 | 1996-12-17 00:47:03 +0000 | [diff] [blame] | 1472 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1473 | finally: |
Barry Warsaw | 9bfd2bf | 2000-09-01 09:01:32 +0000 | [diff] [blame] | 1474 | return; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1475 | } |