| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#include "Python.h" |
| 56 |
#include "_math.h" |
| 57 |
|
| 58 |
#ifdef _OSF_SOURCE |
| 59 |
|
| 60 |
extern double copysign(double, double); |
| 61 |
#endif |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static const double pi = 3.141592653589793238462643383279502884197; |
| 71 |
static const double sqrtpi = 1.772453850905516027298167483341145182798; |
| 72 |
|
| 73 |
static double |
| 74 |
m_sinpi(double x) |
| 75 |
{ |
| 76 |
double y, r; |
| 77 |
int n; |
| 78 |
|
| 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 |
|
| 92 |
|
| 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); |
| 103 |
r = -1.23e200; |
| 104 |
} |
| 105 |
return copysign(1.0, x)*r; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 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 |
|
| 236 |
if (!Py_IS_FINITE(x)) { |
| 237 |
if (Py_IS_NAN(x) || x > 0.0) |
| 238 |
return x; |
| 239 |
else { |
| 240 |
errno = EDOM; |
| 241 |
return Py_NAN; |
| 242 |
} |
| 243 |
} |
| 244 |
if (x == 0.0) { |
| 245 |
errno = EDOM; |
| 246 |
return 1.0/x; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
if (x == floor(x)) { |
| 251 |
if (x < 0.0) { |
| 252 |
errno = EDOM; |
| 253 |
return Py_NAN; |
| 254 |
} |
| 255 |
if (x <= NGAMMA_INTEGRAL) |
| 256 |
return gamma_integral[(int)x - 1]; |
| 257 |
} |
| 258 |
absx = fabs(x); |
| 259 |
|
| 260 |
|
| 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 |
|
| 269 |
|
| 270 |
|
| 271 |
if (absx > 200.0) { |
| 272 |
if (x < 0.0) { |
| 273 |
return 0.0/m_sinpi(x); |
| 274 |
} |
| 275 |
else { |
| 276 |
errno = ERANGE; |
| 277 |
return Py_HUGE_VAL; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
y = absx + lanczos_g_minus_half; |
| 282 |
|
| 283 |
if (absx > lanczos_g_minus_half) { |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 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 / m_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; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
static double |
| 331 |
m_lgamma(double x) |
| 332 |
{ |
| 333 |
double r, absx; |
| 334 |
|
| 335 |
|
| 336 |
if (!Py_IS_FINITE(x)) { |
| 337 |
if (Py_IS_NAN(x)) |
| 338 |
return x; |
| 339 |
else |
| 340 |
return Py_HUGE_VAL; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
if (x == floor(x) && x <= 2.0) { |
| 345 |
if (x <= 0.0) { |
| 346 |
errno = EDOM; |
| 347 |
return Py_HUGE_VAL; |
| 348 |
} |
| 349 |
else { |
| 350 |
return 0.0; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
absx = fabs(x); |
| 355 |
|
| 356 |
if (absx < 1e-20) |
| 357 |
return -log(absx); |
| 358 |
|
| 359 |
|
| 360 |
if (x > 0.0) { |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 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(m_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 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
#define ERF_SERIES_CUTOFF 1.5 |
| 416 |
#define ERF_SERIES_TERMS 25 |
| 417 |
#define ERFC_CONTFRAC_CUTOFF 30.0 |
| 418 |
#define ERFC_CONTFRAC_TERMS 50 |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
static double |
| 428 |
m_erf_series(double x) |
| 429 |
{ |
| 430 |
double x2, acc, fk, result; |
| 431 |
int i, saved_errno; |
| 432 |
|
| 433 |
x2 = x * x; |
| 434 |
acc = 0.0; |
| 435 |
fk = (double)ERF_SERIES_TERMS + 0.5; |
| 436 |
for (i = 0; i < ERF_SERIES_TERMS; i++) { |
| 437 |
acc = 2.0 + x2 * acc / fk; |
| 438 |
fk -= 1.0; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
saved_errno = errno; |
| 443 |
result = acc * x * exp(-x2) / sqrtpi; |
| 444 |
errno = saved_errno; |
| 445 |
return result; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
static double |
| 458 |
m_erfc_contfrac(double x) |
| 459 |
{ |
| 460 |
double x2, a, da, p, p_last, q, q_last, b, result; |
| 461 |
int i, saved_errno; |
| 462 |
|
| 463 |
if (x >= ERFC_CONTFRAC_CUTOFF) |
| 464 |
return 0.0; |
| 465 |
|
| 466 |
x2 = x*x; |
| 467 |
a = 0.0; |
| 468 |
da = 0.5; |
| 469 |
p = 1.0; p_last = 0.0; |
| 470 |
q = da + x2; q_last = 1.0; |
| 471 |
for (i = 0; i < ERFC_CONTFRAC_TERMS; i++) { |
| 472 |
double temp; |
| 473 |
a += da; |
| 474 |
da += 2.0; |
| 475 |
b = da + x2; |
| 476 |
temp = p; p = b*p - a*p_last; p_last = temp; |
| 477 |
temp = q; q = b*q - a*q_last; q_last = temp; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
saved_errno = errno; |
| 482 |
result = p / q * x * exp(-x2) / sqrtpi; |
| 483 |
errno = saved_errno; |
| 484 |
return result; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
static double |
| 490 |
m_erf(double x) |
| 491 |
{ |
| 492 |
double absx, cf; |
| 493 |
|
| 494 |
if (Py_IS_NAN(x)) |
| 495 |
return x; |
| 496 |
absx = fabs(x); |
| 497 |
if (absx < ERF_SERIES_CUTOFF) |
| 498 |
return m_erf_series(x); |
| 499 |
else { |
| 500 |
cf = m_erfc_contfrac(absx); |
| 501 |
return x > 0.0 ? 1.0 - cf : cf - 1.0; |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
static double |
| 508 |
m_erfc(double x) |
| 509 |
{ |
| 510 |
double absx, cf; |
| 511 |
|
| 512 |
if (Py_IS_NAN(x)) |
| 513 |
return x; |
| 514 |
absx = fabs(x); |
| 515 |
if (absx < ERF_SERIES_CUTOFF) |
| 516 |
return 1.0 - m_erf_series(x); |
| 517 |
else { |
| 518 |
cf = m_erfc_contfrac(absx); |
| 519 |
return x > 0.0 ? cf : 2.0 - cf; |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
static double |
| 532 |
m_atan2(double y, double x) |
| 533 |
{ |
| 534 |
if (Py_IS_NAN(x) || Py_IS_NAN(y)) |
| 535 |
return Py_NAN; |
| 536 |
if (Py_IS_INFINITY(y)) { |
| 537 |
if (Py_IS_INFINITY(x)) { |
| 538 |
if (copysign(1., x) == 1.) |
| 539 |
|
| 540 |
return copysign(0.25*Py_MATH_PI, y); |
| 541 |
else |
| 542 |
|
| 543 |
return copysign(0.75*Py_MATH_PI, y); |
| 544 |
} |
| 545 |
|
| 546 |
return copysign(0.5*Py_MATH_PI, y); |
| 547 |
} |
| 548 |
if (Py_IS_INFINITY(x) || y == 0.) { |
| 549 |
if (copysign(1., x) == 1.) |
| 550 |
|
| 551 |
return copysign(0., y); |
| 552 |
else |
| 553 |
|
| 554 |
return copysign(Py_MATH_PI, y); |
| 555 |
} |
| 556 |
return atan2(y, x); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
static double |
| 567 |
m_log(double x) |
| 568 |
{ |
| 569 |
if (Py_IS_FINITE(x)) { |
| 570 |
if (x > 0.0) |
| 571 |
return log(x); |
| 572 |
errno = EDOM; |
| 573 |
if (x == 0.0) |
| 574 |
return -Py_HUGE_VAL; |
| 575 |
else |
| 576 |
return Py_NAN; |
| 577 |
} |
| 578 |
else if (Py_IS_NAN(x)) |
| 579 |
return x; |
| 580 |
else if (x > 0.0) |
| 581 |
return x; |
| 582 |
else { |
| 583 |
errno = EDOM; |
| 584 |
return Py_NAN; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
static double |
| 589 |
m_log10(double x) |
| 590 |
{ |
| 591 |
if (Py_IS_FINITE(x)) { |
| 592 |
if (x > 0.0) |
| 593 |
return log10(x); |
| 594 |
errno = EDOM; |
| 595 |
if (x == 0.0) |
| 596 |
return -Py_HUGE_VAL; |
| 597 |
else |
| 598 |
return Py_NAN; |
| 599 |
} |
| 600 |
else if (Py_IS_NAN(x)) |
| 601 |
return x; |
| 602 |
else if (x > 0.0) |
| 603 |
return x; |
| 604 |
else { |
| 605 |
errno = EDOM; |
| 606 |
return Py_NAN; |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
static int |
| 616 |
is_error(double x) |
| 617 |
{ |
| 618 |
int result = 1; |
| 619 |
assert(errno); |
| 620 |
if (errno == EDOM) |
| 621 |
PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 622 |
|
| 623 |
else if (errno == ERANGE) { |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
if (fabs(x) < 1.0) |
| 640 |
result = 0; |
| 641 |
else |
| 642 |
PyErr_SetString(PyExc_OverflowError, |
| 643 |
"math range error"); |
| 644 |
} |
| 645 |
else |
| 646 |
|
| 647 |
PyErr_SetFromErrno(PyExc_ValueError); |
| 648 |
return result; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
static PyObject * |
| 682 |
math_1(PyObject *arg, double (*func) (double), int can_overflow) |
| 683 |
{ |
| 684 |
double x, r; |
| 685 |
x = PyFloat_AsDouble(arg); |
| 686 |
if (x == -1.0 && PyErr_Occurred()) |
| 687 |
return NULL; |
| 688 |
errno = 0; |
| 689 |
PyFPE_START_PROTECT("in math_1", return 0); |
| 690 |
r = (*func)(x); |
| 691 |
PyFPE_END_PROTECT(r); |
| 692 |
if (Py_IS_NAN(r)) { |
| 693 |
if (!Py_IS_NAN(x)) |
| 694 |
errno = EDOM; |
| 695 |
else |
| 696 |
errno = 0; |
| 697 |
} |
| 698 |
else if (Py_IS_INFINITY(r)) { |
| 699 |
if (Py_IS_FINITE(x)) |
| 700 |
errno = can_overflow ? ERANGE : EDOM; |
| 701 |
else |
| 702 |
errno = 0; |
| 703 |
} |
| 704 |
if (errno && is_error(r)) |
| 705 |
return NULL; |
| 706 |
else |
| 707 |
return PyFloat_FromDouble(r); |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
static PyObject * |
| 715 |
math_1a(PyObject *arg, double (*func) (double)) |
| 716 |
{ |
| 717 |
double x, r; |
| 718 |
x = PyFloat_AsDouble(arg); |
| 719 |
if (x == -1.0 && PyErr_Occurred()) |
| 720 |
return NULL; |
| 721 |
errno = 0; |
| 722 |
PyFPE_START_PROTECT("in math_1a", return 0); |
| 723 |
r = (*func)(x); |
| 724 |
PyFPE_END_PROTECT(r); |
| 725 |
if (errno && is_error(r)) |
| 726 |
return NULL; |
| 727 |
return PyFloat_FromDouble(r); |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
static PyObject * |
| 758 |
math_2(PyObject *args, double (*func) (double, double), char *funcname) |
| 759 |
{ |
| 760 |
PyObject *ox, *oy; |
| 761 |
double x, y, r; |
| 762 |
if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy)) |
| 763 |
return NULL; |
| 764 |
x = PyFloat_AsDouble(ox); |
| 765 |
y = PyFloat_AsDouble(oy); |
| 766 |
if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 767 |
return NULL; |
| 768 |
errno = 0; |
| 769 |
PyFPE_START_PROTECT("in math_2", return 0); |
| 770 |
r = (*func)(x, y); |
| 771 |
PyFPE_END_PROTECT(r); |
| 772 |
if (Py_IS_NAN(r)) { |
| 773 |
if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 774 |
errno = EDOM; |
| 775 |
else |
| 776 |
errno = 0; |
| 777 |
} |
| 778 |
else if (Py_IS_INFINITY(r)) { |
| 779 |
if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 780 |
errno = ERANGE; |
| 781 |
else |
| 782 |
errno = 0; |
| 783 |
} |
| 784 |
if (errno && is_error(r)) |
| 785 |
return NULL; |
| 786 |
else |
| 787 |
return PyFloat_FromDouble(r); |
| 788 |
} |
| 789 |
|
| 790 |
#define FUNC1(funcname, func, can_overflow, docstring) \ |
| 791 |
static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
| 792 |
return math_1(args, func, can_overflow); \ |
| 793 |
}\ |
| 794 |
PyDoc_STRVAR(math_##funcname##_doc, docstring); |
| 795 |
|
| 796 |
#define FUNC1A(funcname, func, docstring) \ |
| 797 |
static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
| 798 |
return math_1a(args, func); \ |
| 799 |
}\ |
| 800 |
PyDoc_STRVAR(math_##funcname##_doc, docstring); |
| 801 |
|
| 802 |
#define FUNC2(funcname, func, docstring) \ |
| 803 |
static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
| 804 |
return math_2(args, func, #funcname); \ |
| 805 |
}\ |
| 806 |
PyDoc_STRVAR(math_##funcname##_doc, docstring); |
| 807 |
|
| 808 |
FUNC1(acos, acos, 0, |
| 809 |
"acos(x)\n\nReturn the arc cosine (measured in radians) of x.") |
| 810 |
FUNC1(acosh, m_acosh, 0, |
| 811 |
"acosh(x)\n\nReturn the inverse hyperbolic cosine of x.") |
| 812 |
FUNC1(asin, asin, 0, |
| 813 |
"asin(x)\n\nReturn the arc sine (measured in radians) of x.") |
| 814 |
FUNC1(asinh, m_asinh, 0, |
| 815 |
"asinh(x)\n\nReturn the inverse hyperbolic sine of x.") |
| 816 |
FUNC1(atan, atan, 0, |
| 817 |
"atan(x)\n\nReturn the arc tangent (measured in radians) of x.") |
| 818 |
FUNC2(atan2, m_atan2, |
| 819 |
"atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n" |
| 820 |
"Unlike atan(y/x), the signs of both x and y are considered.") |
| 821 |
FUNC1(atanh, m_atanh, 0, |
| 822 |
"atanh(x)\n\nReturn the inverse hyperbolic tangent of x.") |
| 823 |
FUNC1(ceil, ceil, 0, |
| 824 |
"ceil(x)\n\nReturn the ceiling of x as a float.\n" |
| 825 |
"This is the smallest integral value >= x.") |
| 826 |
FUNC2(copysign, copysign, |
| 827 |
"copysign(x, y)\n\nReturn x with the sign of y.") |
| 828 |
FUNC1(cos, cos, 0, |
| 829 |
"cos(x)\n\nReturn the cosine of x (measured in radians).") |
| 830 |
FUNC1(cosh, cosh, 1, |
| 831 |
"cosh(x)\n\nReturn the hyperbolic cosine of x.") |
| 832 |
FUNC1A(erf, m_erf, |
| 833 |
"erf(x)\n\nError function at x.") |
| 834 |
FUNC1A(erfc, m_erfc, |
| 835 |
"erfc(x)\n\nComplementary error function at x.") |
| 836 |
FUNC1(exp, exp, 1, |
| 837 |
"exp(x)\n\nReturn e raised to the power of x.") |
| 838 |
FUNC1(expm1, m_expm1, 1, |
| 839 |
"expm1(x)\n\nReturn exp(x)-1.\n" |
| 840 |
"This function avoids the loss of precision involved in the direct " |
| 841 |
"evaluation of exp(x)-1 for small x.") |
| 842 |
FUNC1(fabs, fabs, 0, |
| 843 |
"fabs(x)\n\nReturn the absolute value of the float x.") |
| 844 |
FUNC1(floor, floor, 0, |
| 845 |
"floor(x)\n\nReturn the floor of x as a float.\n" |
| 846 |
"This is the largest integral value <= x.") |
| 847 |
FUNC1A(gamma, m_tgamma, |
| 848 |
"gamma(x)\n\nGamma function at x.") |
| 849 |
FUNC1A(lgamma, m_lgamma, |
| 850 |
"lgamma(x)\n\nNatural logarithm of absolute value of Gamma function at x.") |
| 851 |
FUNC1(log1p, m_log1p, 1, |
| 852 |
"log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n" |
| 853 |
"The result is computed in a way which is accurate for x near zero.") |
| 854 |
FUNC1(sin, sin, 0, |
| 855 |
"sin(x)\n\nReturn the sine of x (measured in radians).") |
| 856 |
FUNC1(sinh, sinh, 1, |
| 857 |
"sinh(x)\n\nReturn the hyperbolic sine of x.") |
| 858 |
FUNC1(sqrt, sqrt, 0, |
| 859 |
"sqrt(x)\n\nReturn the square root of x.") |
| 860 |
FUNC1(tan, tan, 0, |
| 861 |
"tan(x)\n\nReturn the tangent of x (measured in radians).") |
| 862 |
FUNC1(tanh, tanh, 0, |
| 863 |
"tanh(x)\n\nReturn the hyperbolic tangent of x.") |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
#define NUM_PARTIALS 32 |
| 902 |
|
| 903 |
|
| 904 |
static int |
| 905 |
_fsum_realloc(double **p_ptr, Py_ssize_t n, |
| 906 |
double *ps, Py_ssize_t *m_ptr) |
| 907 |
{ |
| 908 |
void *v = NULL; |
| 909 |
Py_ssize_t m = *m_ptr; |
| 910 |
|
| 911 |
m += m; |
| 912 |
if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) { |
| 913 |
double *p = *p_ptr; |
| 914 |
if (p == ps) { |
| 915 |
v = PyMem_Malloc(sizeof(double) * m); |
| 916 |
if (v != NULL) |
| 917 |
memcpy(v, ps, sizeof(double) * n); |
| 918 |
} |
| 919 |
else |
| 920 |
v = PyMem_Realloc(p, sizeof(double) * m); |
| 921 |
} |
| 922 |
if (v == NULL) { |
| 923 |
PyErr_SetString(PyExc_MemoryError, "math.fsum partials"); |
| 924 |
return 1; |
| 925 |
} |
| 926 |
*p_ptr = (double*) v; |
| 927 |
*m_ptr = m; |
| 928 |
return 0; |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
static PyObject* |
| 962 |
math_fsum(PyObject *self, PyObject *seq) |
| 963 |
{ |
| 964 |
PyObject *item, *iter, *sum = NULL; |
| 965 |
Py_ssize_t i, j, n = 0, m = NUM_PARTIALS; |
| 966 |
double x, y, t, ps[NUM_PARTIALS], *p = ps; |
| 967 |
double xsave, special_sum = 0.0, inf_sum = 0.0; |
| 968 |
volatile double hi, yr, lo; |
| 969 |
|
| 970 |
iter = PyObject_GetIter(seq); |
| 971 |
if (iter == NULL) |
| 972 |
return NULL; |
| 973 |
|
| 974 |
PyFPE_START_PROTECT("fsum", Py_DECREF(iter); return NULL) |
| 975 |
|
| 976 |
for(;;) { |
| 977 |
assert(0 <= n && n <= m); |
| 978 |
assert((m == NUM_PARTIALS && p == ps) || |
| 979 |
(m > NUM_PARTIALS && p != NULL)); |
| 980 |
|
| 981 |
item = PyIter_Next(iter); |
| 982 |
if (item == NULL) { |
| 983 |
if (PyErr_Occurred()) |
| 984 |
goto _fsum_error; |
| 985 |
break; |
| 986 |
} |
| 987 |
x = PyFloat_AsDouble(item); |
| 988 |
Py_DECREF(item); |
| 989 |
if (PyErr_Occurred()) |
| 990 |
goto _fsum_error; |
| 991 |
|
| 992 |
xsave = x; |
| 993 |
for (i = j = 0; j < n; j++) { |
| 994 |
y = p[j]; |
| 995 |
if (fabs(x) < fabs(y)) { |
| 996 |
t = x; x = y; y = t; |
| 997 |
} |
| 998 |
hi = x + y; |
| 999 |
yr = hi - x; |
| 1000 |
lo = y - yr; |
| 1001 |
if (lo != 0.0) |
| 1002 |
p[i++] = lo; |
| 1003 |
x = hi; |
| 1004 |
} |
| 1005 |
|
| 1006 |
n = i; |
| 1007 |
if (x != 0.0) { |
| 1008 |
if (! Py_IS_FINITE(x)) { |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
if (Py_IS_FINITE(xsave)) { |
| 1014 |
PyErr_SetString(PyExc_OverflowError, |
| 1015 |
"intermediate overflow in fsum"); |
| 1016 |
goto _fsum_error; |
| 1017 |
} |
| 1018 |
if (Py_IS_INFINITY(xsave)) |
| 1019 |
inf_sum += xsave; |
| 1020 |
special_sum += xsave; |
| 1021 |
|
| 1022 |
n = 0; |
| 1023 |
} |
| 1024 |
else if (n >= m && _fsum_realloc(&p, n, ps, &m)) |
| 1025 |
goto _fsum_error; |
| 1026 |
else |
| 1027 |
p[n++] = x; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
if (special_sum != 0.0) { |
| 1032 |
if (Py_IS_NAN(inf_sum)) |
| 1033 |
PyErr_SetString(PyExc_ValueError, |
| 1034 |
"-inf + inf in fsum"); |
| 1035 |
else |
| 1036 |
sum = PyFloat_FromDouble(special_sum); |
| 1037 |
goto _fsum_error; |
| 1038 |
} |
| 1039 |
|
| 1040 |
hi = 0.0; |
| 1041 |
if (n > 0) { |
| 1042 |
hi = p[--n]; |
| 1043 |
|
| 1044 |
|
| 1045 |
while (n > 0) { |
| 1046 |
x = hi; |
| 1047 |
y = p[--n]; |
| 1048 |
assert(fabs(y) < fabs(x)); |
| 1049 |
hi = x + y; |
| 1050 |
yr = hi - x; |
| 1051 |
lo = y - yr; |
| 1052 |
if (lo != 0.0) |
| 1053 |
break; |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) || |
| 1061 |
(lo > 0.0 && p[n-1] > 0.0))) { |
| 1062 |
y = lo * 2.0; |
| 1063 |
x = hi + y; |
| 1064 |
yr = x - hi; |
| 1065 |
if (y == yr) |
| 1066 |
hi = x; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
sum = PyFloat_FromDouble(hi); |
| 1070 |
|
| 1071 |
_fsum_error: |
| 1072 |
PyFPE_END_PROTECT(hi) |
| 1073 |
Py_DECREF(iter); |
| 1074 |
if (p != ps) |
| 1075 |
PyMem_Free(p); |
| 1076 |
return sum; |
| 1077 |
} |
| 1078 |
|
| 1079 |
#undef NUM_PARTIALS |
| 1080 |
|
| 1081 |
PyDoc_STRVAR(math_fsum_doc, |
| 1082 |
"fsum(iterable)\n\n\ |
| 1083 |
Return an accurate floating point sum of values in the iterable.\n\ |
| 1084 |
Assumes IEEE-754 floating point arithmetic."); |
| 1085 |
|
| 1086 |
static PyObject * |
| 1087 |
math_factorial(PyObject *self, PyObject *arg) |
| 1088 |
{ |
| 1089 |
long i, x; |
| 1090 |
PyObject *result, *iobj, *newresult; |
| 1091 |
|
| 1092 |
if (PyFloat_Check(arg)) { |
| 1093 |
PyObject *lx; |
| 1094 |
double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg); |
| 1095 |
if (!(Py_IS_FINITE(dx) && dx == floor(dx))) { |
| 1096 |
PyErr_SetString(PyExc_ValueError, |
| 1097 |
"factorial() only accepts integral values"); |
| 1098 |
return NULL; |
| 1099 |
} |
| 1100 |
lx = PyLong_FromDouble(dx); |
| 1101 |
if (lx == NULL) |
| 1102 |
return NULL; |
| 1103 |
x = PyLong_AsLong(lx); |
| 1104 |
Py_DECREF(lx); |
| 1105 |
} |
| 1106 |
else |
| 1107 |
x = PyInt_AsLong(arg); |
| 1108 |
|
| 1109 |
if (x == -1 && PyErr_Occurred()) |
| 1110 |
return NULL; |
| 1111 |
if (x < 0) { |
| 1112 |
PyErr_SetString(PyExc_ValueError, |
| 1113 |
"factorial() not defined for negative values"); |
| 1114 |
return NULL; |
| 1115 |
} |
| 1116 |
|
| 1117 |
result = (PyObject *)PyInt_FromLong(1); |
| 1118 |
if (result == NULL) |
| 1119 |
return NULL; |
| 1120 |
for (i=1 ; i<=x ; i++) { |
| 1121 |
iobj = (PyObject *)PyInt_FromLong(i); |
| 1122 |
if (iobj == NULL) |
| 1123 |
goto error; |
| 1124 |
newresult = PyNumber_Multiply(result, iobj); |
| 1125 |
Py_DECREF(iobj); |
| 1126 |
if (newresult == NULL) |
| 1127 |
goto error; |
| 1128 |
Py_DECREF(result); |
| 1129 |
result = newresult; |
| 1130 |
} |
| 1131 |
return result; |
| 1132 |
|
| 1133 |
error: |
| 1134 |
Py_DECREF(result); |
| 1135 |
return NULL; |
| 1136 |
} |
| 1137 |
|
| 1138 |
PyDoc_STRVAR(math_factorial_doc, |
| 1139 |
"factorial(x) -> Integral\n" |
| 1140 |
"\n" |
| 1141 |
"Find x!. Raise a ValueError if x is negative or non-integral."); |
| 1142 |
|
| 1143 |
static PyObject * |
| 1144 |
math_trunc(PyObject *self, PyObject *number) |
| 1145 |
{ |
| 1146 |
return PyObject_CallMethod(number, "__trunc__", NULL); |
| 1147 |
} |
| 1148 |
|
| 1149 |
PyDoc_STRVAR(math_trunc_doc, |
| 1150 |
"trunc(x:Real) -> Integral\n" |
| 1151 |
"\n" |
| 1152 |
"Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method."); |
| 1153 |
|
| 1154 |
static PyObject * |
| 1155 |
math_frexp(PyObject *self, PyObject *arg) |
| 1156 |
{ |
| 1157 |
int i; |
| 1158 |
double x = PyFloat_AsDouble(arg); |
| 1159 |
if (x == -1.0 && PyErr_Occurred()) |
| 1160 |
return NULL; |
| 1161 |
|
| 1162 |
|
| 1163 |
if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) { |
| 1164 |
i = 0; |
| 1165 |
} |
| 1166 |
else { |
| 1167 |
PyFPE_START_PROTECT("in math_frexp", return 0); |
| 1168 |
x = frexp(x, &i); |
| 1169 |
PyFPE_END_PROTECT(x); |
| 1170 |
} |
| 1171 |
return Py_BuildValue("(di)", x, i); |
| 1172 |
} |
| 1173 |
|
| 1174 |
PyDoc_STRVAR(math_frexp_doc, |
| 1175 |
"frexp(x)\n" |
| 1176 |
"\n" |
| 1177 |
"Return the mantissa and exponent of x, as pair (m, e).\n" |
| 1178 |
"m is a float and e is an int, such that x = m * 2.**e.\n" |
| 1179 |
"If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0."); |
| 1180 |
|
| 1181 |
static PyObject * |
| 1182 |
math_ldexp(PyObject *self, PyObject *args) |
| 1183 |
{ |
| 1184 |
double x, r; |
| 1185 |
PyObject *oexp; |
| 1186 |
long exp; |
| 1187 |
int overflow; |
| 1188 |
if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp)) |
| 1189 |
return NULL; |
| 1190 |
|
| 1191 |
if (_PyAnyInt_Check(oexp)) { |
| 1192 |
|
| 1193 |
|
| 1194 |
exp = PyLong_AsLongAndOverflow(oexp, &overflow); |
| 1195 |
if (exp == -1 && PyErr_Occurred()) |
| 1196 |
return NULL; |
| 1197 |
if (overflow) |
| 1198 |
exp = overflow < 0 ? LONG_MIN : LONG_MAX; |
| 1199 |
} |
| 1200 |
else { |
| 1201 |
PyErr_SetString(PyExc_TypeError, |
| 1202 |
"Expected an int or long as second argument " |
| 1203 |
"to ldexp."); |
| 1204 |
return NULL; |
| 1205 |
} |
| 1206 |
|
| 1207 |
if (x == 0. || !Py_IS_FINITE(x)) { |
| 1208 |
|
| 1209 |
r = x; |
| 1210 |
errno = 0; |
| 1211 |
} else if (exp > INT_MAX) { |
| 1212 |
|
| 1213 |
r = copysign(Py_HUGE_VAL, x); |
| 1214 |
errno = ERANGE; |
| 1215 |
} else if (exp < INT_MIN) { |
| 1216 |
|
| 1217 |
r = copysign(0., x); |
| 1218 |
errno = 0; |
| 1219 |
} else { |
| 1220 |
errno = 0; |
| 1221 |
PyFPE_START_PROTECT("in math_ldexp", return 0); |
| 1222 |
r = ldexp(x, (int)exp); |
| 1223 |
PyFPE_END_PROTECT(r); |
| 1224 |
if (Py_IS_INFINITY(r)) |
| 1225 |
errno = ERANGE; |
| 1226 |
} |
| 1227 |
|
| 1228 |
if (errno && is_error(r)) |
| 1229 |
return NULL; |
| 1230 |
return PyFloat_FromDouble(r); |
| 1231 |
} |
| 1232 |
|
| 1233 |
PyDoc_STRVAR(math_ldexp_doc, |
| 1234 |
"ldexp(x, i)\n\n\ |
| 1235 |
Return x * (2**i)."); |
| 1236 |
|
| 1237 |
static PyObject * |
| 1238 |
math_modf(PyObject *self, PyObject *arg) |
| 1239 |
{ |
| 1240 |
double y, x = PyFloat_AsDouble(arg); |
| 1241 |
if (x == -1.0 && PyErr_Occurred()) |
| 1242 |
return NULL; |
| 1243 |
|
| 1244 |
|
| 1245 |
if (!Py_IS_FINITE(x)) { |
| 1246 |
if (Py_IS_INFINITY(x)) |
| 1247 |
return Py_BuildValue("(dd)", copysign(0., x), x); |
| 1248 |
else if (Py_IS_NAN(x)) |
| 1249 |
return Py_BuildValue("(dd)", x, x); |
| 1250 |
} |
| 1251 |
|
| 1252 |
errno = 0; |
| 1253 |
PyFPE_START_PROTECT("in math_modf", return 0); |
| 1254 |
x = modf(x, &y); |
| 1255 |
PyFPE_END_PROTECT(x); |
| 1256 |
return Py_BuildValue("(dd)", x, y); |
| 1257 |
} |
| 1258 |
|
| 1259 |
PyDoc_STRVAR(math_modf_doc, |
| 1260 |
"modf(x)\n" |
| 1261 |
"\n" |
| 1262 |
"Return the fractional and integer parts of x. Both results carry the sign\n" |
| 1263 |
"of x and are floats."); |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
static PyObject* |
| 1275 |
loghelper(PyObject* arg, double (*func)(double), char *funcname) |
| 1276 |
{ |
| 1277 |
|
| 1278 |
if (PyLong_Check(arg)) { |
| 1279 |
double x, result; |
| 1280 |
Py_ssize_t e; |
| 1281 |
|
| 1282 |
|
| 1283 |
if (Py_SIZE(arg) <= 0) { |
| 1284 |
PyErr_SetString(PyExc_ValueError, |
| 1285 |
"math domain error"); |
| 1286 |
return NULL; |
| 1287 |
} |
| 1288 |
|
| 1289 |
x = PyLong_AsDouble(arg); |
| 1290 |
if (x == -1.0 && PyErr_Occurred()) { |
| 1291 |
if (!PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 1292 |
return NULL; |
| 1293 |
|
| 1294 |
|
| 1295 |
PyErr_Clear(); |
| 1296 |
x = _PyLong_Frexp((PyLongObject *)arg, &e); |
| 1297 |
if (x == -1.0 && PyErr_Occurred()) |
| 1298 |
return NULL; |
| 1299 |
|
| 1300 |
result = func(x) + func(2.0) * e; |
| 1301 |
} |
| 1302 |
else |
| 1303 |
|
| 1304 |
result = func(x); |
| 1305 |
return PyFloat_FromDouble(result); |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
return math_1(arg, func, 0); |
| 1310 |
} |
| 1311 |
|
| 1312 |
static PyObject * |
| 1313 |
math_log(PyObject *self, PyObject *args) |
| 1314 |
{ |
| 1315 |
PyObject *arg; |
| 1316 |
PyObject *base = NULL; |
| 1317 |
PyObject *num, *den; |
| 1318 |
PyObject *ans; |
| 1319 |
|
| 1320 |
if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base)) |
| 1321 |
return NULL; |
| 1322 |
|
| 1323 |
num = loghelper(arg, m_log, "log"); |
| 1324 |
if (num == NULL || base == NULL) |
| 1325 |
return num; |
| 1326 |
|
| 1327 |
den = loghelper(base, m_log, "log"); |
| 1328 |
if (den == NULL) { |
| 1329 |
Py_DECREF(num); |
| 1330 |
return NULL; |
| 1331 |
} |
| 1332 |
|
| 1333 |
ans = PyNumber_Divide(num, den); |
| 1334 |
Py_DECREF(num); |
| 1335 |
Py_DECREF(den); |
| 1336 |
return ans; |
| 1337 |
} |
| 1338 |
|
| 1339 |
PyDoc_STRVAR(math_log_doc, |
| 1340 |
"log(x[, base])\n\n\ |
| 1341 |
Return the logarithm of x to the given base.\n\ |
| 1342 |
If the base not specified, returns the natural logarithm (base e) of x."); |
| 1343 |
|
| 1344 |
static PyObject * |
| 1345 |
math_log10(PyObject *self, PyObject *arg) |
| 1346 |
{ |
| 1347 |
return loghelper(arg, m_log10, "log10"); |
| 1348 |
} |
| 1349 |
|
| 1350 |
PyDoc_STRVAR(math_log10_doc, |
| 1351 |
"log10(x)\n\nReturn the base 10 logarithm of x."); |
| 1352 |
|
| 1353 |
static PyObject * |
| 1354 |
math_fmod(PyObject *self, PyObject *args) |
| 1355 |
{ |
| 1356 |
PyObject *ox, *oy; |
| 1357 |
double r, x, y; |
| 1358 |
if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy)) |
| 1359 |
return NULL; |
| 1360 |
x = PyFloat_AsDouble(ox); |
| 1361 |
y = PyFloat_AsDouble(oy); |
| 1362 |
if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 1363 |
return NULL; |
| 1364 |
|
| 1365 |
if (Py_IS_INFINITY(y) && Py_IS_FINITE(x)) |
| 1366 |
return PyFloat_FromDouble(x); |
| 1367 |
errno = 0; |
| 1368 |
PyFPE_START_PROTECT("in math_fmod", return 0); |
| 1369 |
r = fmod(x, y); |
| 1370 |
PyFPE_END_PROTECT(r); |
| 1371 |
if (Py_IS_NAN(r)) { |
| 1372 |
if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 1373 |
errno = EDOM; |
| 1374 |
else |
| 1375 |
errno = 0; |
| 1376 |
} |
| 1377 |
if (errno && is_error(r)) |
| 1378 |
return NULL; |
| 1379 |
else |
| 1380 |
return PyFloat_FromDouble(r); |
| 1381 |
} |
| 1382 |
|
| 1383 |
PyDoc_STRVAR(math_fmod_doc, |
| 1384 |
"fmod(x, y)\n\nReturn fmod(x, y), according to platform C." |
| 1385 |
" x % y may differ."); |
| 1386 |
|
| 1387 |
static PyObject * |
| 1388 |
math_hypot(PyObject *self, PyObject *args) |
| 1389 |
{ |
| 1390 |
PyObject *ox, *oy; |
| 1391 |
double r, x, y; |
| 1392 |
if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy)) |
| 1393 |
return NULL; |
| 1394 |
x = PyFloat_AsDouble(ox); |
| 1395 |
y = PyFloat_AsDouble(oy); |
| 1396 |
if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 1397 |
return NULL; |
| 1398 |
|
| 1399 |
if (Py_IS_INFINITY(x)) |
| 1400 |
return PyFloat_FromDouble(fabs(x)); |
| 1401 |
if (Py_IS_INFINITY(y)) |
| 1402 |
return PyFloat_FromDouble(fabs(y)); |
| 1403 |
errno = 0; |
| 1404 |
PyFPE_START_PROTECT("in math_hypot", return 0); |
| 1405 |
r = hypot(x, y); |
| 1406 |
PyFPE_END_PROTECT(r); |
| 1407 |
if (Py_IS_NAN(r)) { |
| 1408 |
if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
| 1409 |
errno = EDOM; |
| 1410 |
else |
| 1411 |
errno = 0; |
| 1412 |
} |
| 1413 |
else if (Py_IS_INFINITY(r)) { |
| 1414 |
if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
| 1415 |
errno = ERANGE; |
| 1416 |
else |
| 1417 |
errno = 0; |
| 1418 |
} |
| 1419 |
if (errno && is_error(r)) |
| 1420 |
return NULL; |
| 1421 |
else |
| 1422 |
return PyFloat_FromDouble(r); |
| 1423 |
} |
| 1424 |
|
| 1425 |
PyDoc_STRVAR(math_hypot_doc, |
| 1426 |
"hypot(x, y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y)."); |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
static PyObject * |
| 1435 |
math_pow(PyObject *self, PyObject *args) |
| 1436 |
{ |
| 1437 |
PyObject *ox, *oy; |
| 1438 |
double r, x, y; |
| 1439 |
int odd_y; |
| 1440 |
|
| 1441 |
if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy)) |
| 1442 |
return NULL; |
| 1443 |
x = PyFloat_AsDouble(ox); |
| 1444 |
y = PyFloat_AsDouble(oy); |
| 1445 |
if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 1446 |
return NULL; |
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
r = 0.; |
| 1451 |
if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) { |
| 1452 |
errno = 0; |
| 1453 |
if (Py_IS_NAN(x)) |
| 1454 |
r = y == 0. ? 1. : x; |
| 1455 |
else if (Py_IS_NAN(y)) |
| 1456 |
r = x == 1. ? 1. : y; |
| 1457 |
else if (Py_IS_INFINITY(x)) { |
| 1458 |
odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0; |
| 1459 |
if (y > 0.) |
| 1460 |
r = odd_y ? x : fabs(x); |
| 1461 |
else if (y == 0.) |
| 1462 |
r = 1.; |
| 1463 |
else |
| 1464 |
r = odd_y ? copysign(0., x) : 0.; |
| 1465 |
} |
| 1466 |
else if (Py_IS_INFINITY(y)) { |
| 1467 |
if (fabs(x) == 1.0) |
| 1468 |
r = 1.; |
| 1469 |
else if (y > 0. && fabs(x) > 1.0) |
| 1470 |
r = y; |
| 1471 |
else if (y < 0. && fabs(x) < 1.0) { |
| 1472 |
r = -y; |
| 1473 |
if (x == 0.) |
| 1474 |
errno = EDOM; |
| 1475 |
} |
| 1476 |
else |
| 1477 |
r = 0.; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
else { |
| 1481 |
|
| 1482 |
errno = 0; |
| 1483 |
PyFPE_START_PROTECT("in math_pow", return 0); |
| 1484 |
r = pow(x, y); |
| 1485 |
PyFPE_END_PROTECT(r); |
| 1486 |
|
| 1487 |
|
| 1488 |
if (!Py_IS_FINITE(r)) { |
| 1489 |
if (Py_IS_NAN(r)) { |
| 1490 |
errno = EDOM; |
| 1491 |
} |
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
else if (Py_IS_INFINITY(r)) { |
| 1498 |
if (x == 0.) |
| 1499 |
errno = EDOM; |
| 1500 |
else |
| 1501 |
errno = ERANGE; |
| 1502 |
} |
| 1503 |
} |
| 1504 |
} |
| 1505 |
|
| 1506 |
if (errno && is_error(r)) |
| 1507 |
return NULL; |
| 1508 |
else |
| 1509 |
return PyFloat_FromDouble(r); |
| 1510 |
} |
| 1511 |
|
| 1512 |
PyDoc_STRVAR(math_pow_doc, |
| 1513 |
"pow(x, y)\n\nReturn x**y (x to the power of y)."); |
| 1514 |
|
| 1515 |
static const double degToRad = Py_MATH_PI / 180.0; |
| 1516 |
static const double radToDeg = 180.0 / Py_MATH_PI; |
| 1517 |
|
| 1518 |
static PyObject * |
| 1519 |
math_degrees(PyObject *self, PyObject *arg) |
| 1520 |
{ |
| 1521 |
double x = PyFloat_AsDouble(arg); |
| 1522 |
if (x == -1.0 && PyErr_Occurred()) |
| 1523 |
return NULL; |
| 1524 |
return PyFloat_FromDouble(x * radToDeg); |
| 1525 |
} |
| 1526 |
|
| 1527 |
PyDoc_STRVAR(math_degrees_doc, |
| 1528 |
"degrees(x)\n\n\ |
| 1529 |
Convert angle x from radians to degrees."); |
| 1530 |
|
| 1531 |
static PyObject * |
| 1532 |
math_radians(PyObject *self, PyObject *arg) |
| 1533 |
{ |
| 1534 |
double x = PyFloat_AsDouble(arg); |
| 1535 |
if (x == -1.0 && PyErr_Occurred()) |
| 1536 |
return NULL; |
| 1537 |
return PyFloat_FromDouble(x * degToRad); |
| 1538 |
} |
| 1539 |
|
| 1540 |
PyDoc_STRVAR(math_radians_doc, |
| 1541 |
"radians(x)\n\n\ |
| 1542 |
Convert angle x from degrees to radians."); |
| 1543 |
|
| 1544 |
static PyObject * |
| 1545 |
math_isnan(PyObject *self, PyObject *arg) |
| 1546 |
{ |
| 1547 |
double x = PyFloat_AsDouble(arg); |
| 1548 |
if (x == -1.0 && PyErr_Occurred()) |
| 1549 |
return NULL; |
| 1550 |
return PyBool_FromLong((long)Py_IS_NAN(x)); |
| 1551 |
} |
| 1552 |
|
| 1553 |
PyDoc_STRVAR(math_isnan_doc, |
| 1554 |
"isnan(x) -> bool\n\n\ |
| 1555 |
Check if float x is not a number (NaN)."); |
| 1556 |
|
| 1557 |
static PyObject * |
| 1558 |
math_isinf(PyObject *self, PyObject *arg) |
| 1559 |
{ |
| 1560 |
double x = PyFloat_AsDouble(arg); |
| 1561 |
if (x == -1.0 && PyErr_Occurred()) |
| 1562 |
return NULL; |
| 1563 |
return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
| 1564 |
} |
| 1565 |
|
| 1566 |
PyDoc_STRVAR(math_isinf_doc, |
| 1567 |
"isinf(x) -> bool\n\n\ |
| 1568 |
Check if float x is infinite (positive or negative)."); |
| 1569 |
|
| 1570 |
static PyMethodDef math_methods[] = { |
| 1571 |
{"acos", math_acos, METH_O, math_acos_doc}, |
| 1572 |
{"acosh", math_acosh, METH_O, math_acosh_doc}, |
| 1573 |
{"asin", math_asin, METH_O, math_asin_doc}, |
| 1574 |
{"asinh", math_asinh, METH_O, math_asinh_doc}, |
| 1575 |
{"atan", math_atan, METH_O, math_atan_doc}, |
| 1576 |
{"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
| 1577 |
{"atanh", math_atanh, METH_O, math_atanh_doc}, |
| 1578 |
{"ceil", math_ceil, METH_O, math_ceil_doc}, |
| 1579 |
{"copysign", math_copysign, METH_VARARGS, math_copysign_doc}, |
| 1580 |
{"cos", math_cos, METH_O, math_cos_doc}, |
| 1581 |
{"cosh", math_cosh, METH_O, math_cosh_doc}, |
| 1582 |
{"degrees", math_degrees, METH_O, math_degrees_doc}, |
| 1583 |
{"erf", math_erf, METH_O, math_erf_doc}, |
| 1584 |
{"erfc", math_erfc, METH_O, math_erfc_doc}, |
| 1585 |
{"exp", math_exp, METH_O, math_exp_doc}, |
| 1586 |
{"expm1", math_expm1, METH_O, math_expm1_doc}, |
| 1587 |
{"fabs", math_fabs, METH_O, math_fabs_doc}, |
| 1588 |
{"factorial", math_factorial, METH_O, math_factorial_doc}, |
| 1589 |
{"floor", math_floor, METH_O, math_floor_doc}, |
| 1590 |
{"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
| 1591 |
{"frexp", math_frexp, METH_O, math_frexp_doc}, |
| 1592 |
{"fsum", math_fsum, METH_O, math_fsum_doc}, |
| 1593 |
{"gamma", math_gamma, METH_O, math_gamma_doc}, |
| 1594 |
{"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
| 1595 |
{"isinf", math_isinf, METH_O, math_isinf_doc}, |
| 1596 |
{"isnan", math_isnan, METH_O, math_isnan_doc}, |
| 1597 |
{"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
| 1598 |
{"lgamma", math_lgamma, METH_O, math_lgamma_doc}, |
| 1599 |
{"log", math_log, METH_VARARGS, math_log_doc}, |
| 1600 |
{"log1p", math_log1p, METH_O, math_log1p_doc}, |
| 1601 |
{"log10", math_log10, METH_O, math_log10_doc}, |
| 1602 |
{"modf", math_modf, METH_O, math_modf_doc}, |
| 1603 |
{"pow", math_pow, METH_VARARGS, math_pow_doc}, |
| 1604 |
{"radians", math_radians, METH_O, math_radians_doc}, |
| 1605 |
{"sin", math_sin, METH_O, math_sin_doc}, |
| 1606 |
{"sinh", math_sinh, METH_O, math_sinh_doc}, |
| 1607 |
{"sqrt", math_sqrt, METH_O, math_sqrt_doc}, |
| 1608 |
{"tan", math_tan, METH_O, math_tan_doc}, |
| 1609 |
{"tanh", math_tanh, METH_O, math_tanh_doc}, |
| 1610 |
{"trunc", math_trunc, METH_O, math_trunc_doc}, |
| 1611 |
{NULL, NULL} |
| 1612 |
}; |
| 1613 |
|
| 1614 |
|
| 1615 |
PyDoc_STRVAR(module_doc, |
| 1616 |
"This module is always available. It provides access to the\n" |
| 1617 |
"mathematical functions defined by the C standard."); |
| 1618 |
|
| 1619 |
PyMODINIT_FUNC |
| 1620 |
initmath(void) |
| 1621 |
{ |
| 1622 |
PyObject *m; |
| 1623 |
|
| 1624 |
m = Py_InitModule3("math", math_methods, module_doc); |
| 1625 |
if (m == NULL) |
| 1626 |
goto finally; |
| 1627 |
|
| 1628 |
PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); |
| 1629 |
PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); |
| 1630 |
|
| 1631 |
finally: |
| 1632 |
return; |
| 1633 |
} |