| 1 |
import math |
| 2 |
import sys |
| 3 |
|
| 4 |
from rpython.rlib import rfloat |
| 5 |
from rpython.rlib.objectmodel import specialize |
| 6 |
from pypy.interpreter.error import OperationError, oefmt |
| 7 |
from pypy.interpreter.gateway import unwrap_spec, WrappedDefault |
| 8 |
|
| 9 |
class State: |
| 10 |
def __init__(self, space): |
| 11 |
self.w_e = space.newfloat(math.e) |
| 12 |
self.w_pi = space.newfloat(math.pi) |
| 13 |
self.w_tau = space.newfloat(math.pi * 2.0) |
| 14 |
self.w_inf = space.newfloat(rfloat.INFINITY) |
| 15 |
self.w_nan = space.newfloat(rfloat.NAN) |
| 16 |
def get(space): |
| 17 |
return space.fromcache(State) |
| 18 |
|
| 19 |
def _get_double(space, w_x): |
| 20 |
if space.is_w(space.type(w_x), space.w_float): |
| 21 |
return space.float_w(w_x) |
| 22 |
else: |
| 23 |
return space.float_w(space.float(w_x)) |
| 24 |
|
| 25 |
@specialize.arg(1) |
| 26 |
def math1(space, f, w_x): |
| 27 |
x = _get_double(space, w_x) |
| 28 |
try: |
| 29 |
y = f(x) |
| 30 |
except OverflowError: |
| 31 |
raise oefmt(space.w_OverflowError, "math range error") |
| 32 |
except ValueError: |
| 33 |
raise oefmt(space.w_ValueError, "math domain error") |
| 34 |
return space.newfloat(y) |
| 35 |
|
| 36 |
@specialize.arg(1) |
| 37 |
def math1_w(space, f, w_x): |
| 38 |
x = _get_double(space, w_x) |
| 39 |
try: |
| 40 |
r = f(x) |
| 41 |
except OverflowError: |
| 42 |
raise oefmt(space.w_OverflowError, "math range error") |
| 43 |
except ValueError: |
| 44 |
raise oefmt(space.w_ValueError, "math domain error") |
| 45 |
return r |
| 46 |
|
| 47 |
@specialize.arg(1) |
| 48 |
def math2(space, f, w_x, w_snd): |
| 49 |
x = _get_double(space, w_x) |
| 50 |
snd = _get_double(space, w_snd) |
| 51 |
try: |
| 52 |
r = f(x, snd) |
| 53 |
except OverflowError: |
| 54 |
raise oefmt(space.w_OverflowError, "math range error") |
| 55 |
except ValueError: |
| 56 |
raise oefmt(space.w_ValueError, "math domain error") |
| 57 |
return space.newfloat(r) |
| 58 |
|
| 59 |
def trunc(space, w_x): |
| 60 |
"""Truncate x.""" |
| 61 |
w_descr = space.lookup(w_x, '__trunc__') |
| 62 |
if w_descr is not None: |
| 63 |
return space.get_and_call_function(w_descr, w_x) |
| 64 |
return space.trunc(w_x) |
| 65 |
|
| 66 |
def copysign(space, w_x, w_y): |
| 67 |
"""Return x with the sign of y.""" |
| 68 |
|
| 69 |
x = _get_double(space, w_x) |
| 70 |
y = _get_double(space, w_y) |
| 71 |
return space.newfloat(math.copysign(x, y)) |
| 72 |
|
| 73 |
def isinf(space, w_x): |
| 74 |
"""Return True if x is infinity.""" |
| 75 |
return space.newbool(math.isinf(_get_double(space, w_x))) |
| 76 |
|
| 77 |
def isnan(space, w_x): |
| 78 |
"""Return True if x is not a number.""" |
| 79 |
return space.newbool(math.isnan(_get_double(space, w_x))) |
| 80 |
|
| 81 |
def isfinite(space, w_x): |
| 82 |
"""isfinite(x) -> bool |
| 83 |
|
| 84 |
Return True if x is neither an infinity nor a NaN, and False otherwise.""" |
| 85 |
return space.newbool(rfloat.isfinite(_get_double(space, w_x))) |
| 86 |
|
| 87 |
def pow(space, w_x, w_y): |
| 88 |
"""pow(x,y) |
| 89 |
|
| 90 |
Return x**y (x to the power of y). |
| 91 |
""" |
| 92 |
x = _get_double(space, w_x) |
| 93 |
y = _get_double(space, w_y) |
| 94 |
try: |
| 95 |
r = math.pow(x, y) |
| 96 |
except OverflowError: |
| 97 |
raise oefmt(space.w_OverflowError, "math range error") |
| 98 |
except ValueError: |
| 99 |
if x == 0.0 and math.isinf(y) and y < 0: |
| 100 |
return space.newfloat(rfloat.INFINITY) |
| 101 |
raise oefmt(space.w_ValueError, "math domain error") |
| 102 |
return space.newfloat(r) |
| 103 |
|
| 104 |
def cosh(space, w_x): |
| 105 |
"""cosh(x) |
| 106 |
|
| 107 |
Return the hyperbolic cosine of x. |
| 108 |
""" |
| 109 |
return math1(space, math.cosh, w_x) |
| 110 |
|
| 111 |
def ldexp(space, w_x, w_i): |
| 112 |
"""ldexp(x, i) -> x * (2**i) |
| 113 |
""" |
| 114 |
x = _get_double(space, w_x) |
| 115 |
if space.isinstance_w(w_i, space.w_int): |
| 116 |
try: |
| 117 |
exp = space.int_w(w_i) |
| 118 |
except OperationError as e: |
| 119 |
if not e.match(space, space.w_OverflowError): |
| 120 |
raise |
| 121 |
if space.is_true(space.lt(w_i, space.newint(0))): |
| 122 |
exp = -sys.maxint |
| 123 |
else: |
| 124 |
exp = sys.maxint |
| 125 |
else: |
| 126 |
raise oefmt(space.w_TypeError, "integer required for second argument") |
| 127 |
try: |
| 128 |
r = math.ldexp(x, exp) |
| 129 |
except OverflowError: |
| 130 |
raise oefmt(space.w_OverflowError, "math range error") |
| 131 |
except ValueError: |
| 132 |
raise oefmt(space.w_ValueError, "math domain error") |
| 133 |
return space.newfloat(r) |
| 134 |
|
| 135 |
def hypot(space, args_w): |
| 136 |
""" |
| 137 |
Multidimensional Euclidean distance from the origin to a point. |
| 138 |
|
| 139 |
Roughly equivalent to: |
| 140 |
sqrt(sum(x**2 for x in args)) |
| 141 |
|
| 142 |
For a two dimensional point (x, y), gives the hypotenuse |
| 143 |
using the Pythagorean theorem: sqrt(x*x + y*y). |
| 144 |
|
| 145 |
For example, the hypotenuse of a 3/4/5 right triangle is: |
| 146 |
|
| 147 |
>>> hypot(3.0, 4.0) |
| 148 |
5.0 |
| 149 |
""" |
| 150 |
vec = [0.0] * len(args_w) |
| 151 |
found_nan = False |
| 152 |
max = 0.0 |
| 153 |
for i in range(len(args_w)): |
| 154 |
w_x = args_w[i] |
| 155 |
x = math.fabs(_get_double(space, w_x)) |
| 156 |
found_nan = math.isnan(x) or found_nan |
| 157 |
if x > max: |
| 158 |
max = x |
| 159 |
vec[i] = x |
| 160 |
result = _vector_norm(vec, max, found_nan) |
| 161 |
return space.newfloat(result) |
| 162 |
|
| 163 |
def dist(space, w_p, w_q, __posonly__=None): |
| 164 |
""" |
| 165 |
Return the Euclidean distance between two points p and q. |
| 166 |
|
| 167 |
The points should be specified as sequences (or iterables) of |
| 168 |
coordinates. Both inputs must have the same dimension. |
| 169 |
|
| 170 |
Roughly equivalent to: |
| 171 |
sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q))) |
| 172 |
""" |
| 173 |
p_w = space.unpackiterable(w_p) |
| 174 |
q_w = space.unpackiterable(w_q) |
| 175 |
if len(p_w) != len(q_w): |
| 176 |
raise oefmt(space.w_ValueError, "both points must have the same number of dimensions") |
| 177 |
|
| 178 |
vec = [0.0] * len(p_w) |
| 179 |
found_nan = False |
| 180 |
max = 0.0 |
| 181 |
for i in range(len(p_w)): |
| 182 |
px = _get_double(space, p_w[i]) |
| 183 |
qx = _get_double(space, q_w[i]) |
| 184 |
x = math.fabs(px - qx) |
| 185 |
found_nan = math.isnan(x) or found_nan |
| 186 |
if x > max: |
| 187 |
max = x |
| 188 |
vec[i] = x |
| 189 |
result = _vector_norm(vec, max, found_nan) |
| 190 |
return space.newfloat(result) |
| 191 |
|
| 192 |
|
| 193 |
def _vector_norm(vec, max, found_nan): |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
T27 = 134217729.0 |
| 290 |
x = scale = oldcsum = csum = 1.0 |
| 291 |
frac1 = frac2 = frac3 = 0.0 |
| 292 |
if math.isinf(max): |
| 293 |
return max |
| 294 |
if found_nan: |
| 295 |
return rfloat.NAN |
| 296 |
if max == 0.0 or len(vec) <= 1: |
| 297 |
return max |
| 298 |
_, max_e = math.frexp(max) |
| 299 |
if max_e >= -1023: |
| 300 |
|
| 301 |
scale = math.ldexp(1.0, -max_e) |
| 302 |
assert(max * scale >= 0.5) |
| 303 |
assert(max * scale < 1.0) |
| 304 |
for x in vec: |
| 305 |
assert(rfloat.isfinite(x) and math.fabs(x) <= max) |
| 306 |
x *= scale |
| 307 |
assert(math.fabs(x) < 1.0) |
| 308 |
t = x * T27 |
| 309 |
hi = t - (t - x) |
| 310 |
lo = x - hi |
| 311 |
assert(hi + lo == x) |
| 312 |
|
| 313 |
x = hi * hi |
| 314 |
assert(x <= 1.0) |
| 315 |
assert(math.fabs(csum) >= math.fabs(x)) |
| 316 |
oldcsum = csum |
| 317 |
csum += x |
| 318 |
frac1 += (oldcsum - csum) + x |
| 319 |
|
| 320 |
x = 2.0 * hi * lo |
| 321 |
assert(math.fabs(csum) >= math.fabs(x)) |
| 322 |
oldcsum = csum |
| 323 |
csum += x |
| 324 |
frac2 += (oldcsum - csum) + x |
| 325 |
|
| 326 |
assert(csum + lo * lo == csum) |
| 327 |
frac3 += lo * lo |
| 328 |
h = math.sqrt(csum - 1.0 + (frac1 + frac2 + frac3)) |
| 329 |
|
| 330 |
x = h |
| 331 |
t = x * T27 |
| 332 |
hi = t - (t - x) |
| 333 |
lo = x - hi |
| 334 |
assert(hi + lo == x) |
| 335 |
|
| 336 |
x = -hi * hi |
| 337 |
assert(math.fabs(csum) >= math.fabs(x)); |
| 338 |
oldcsum = csum; |
| 339 |
csum += x; |
| 340 |
frac1 += (oldcsum - csum) + x; |
| 341 |
|
| 342 |
x = -2.0 * hi * lo; |
| 343 |
assert(math.fabs(csum) >= math.fabs(x)); |
| 344 |
oldcsum = csum; |
| 345 |
csum += x; |
| 346 |
frac2 += (oldcsum - csum) + x; |
| 347 |
|
| 348 |
x = -lo * lo; |
| 349 |
assert(math.fabs(csum) >= math.fabs(x)); |
| 350 |
oldcsum = csum; |
| 351 |
csum += x; |
| 352 |
frac3 += (oldcsum - csum) + x; |
| 353 |
|
| 354 |
x = csum - 1.0 + (frac1 + frac2 + frac3); |
| 355 |
return (h + x / (2.0 * h)) / scale; |
| 356 |
|
| 357 |
else: |
| 358 |
|
| 359 |
|
| 360 |
for x in vec: |
| 361 |
assert(not math.isinf(x) and not math.isnan(x) and math.fabs(x) <= max); |
| 362 |
x /= max; |
| 363 |
x = x * x; |
| 364 |
assert(x <= 1.0); |
| 365 |
assert(math.fabs(csum) >= math.fabs(x)); |
| 366 |
oldcsum = csum; |
| 367 |
csum += x; |
| 368 |
frac1 += (oldcsum - csum) + x; |
| 369 |
return max * math.sqrt(csum - 1.0 + frac1); |
| 370 |
|
| 371 |
|
| 372 |
def tan(space, w_x): |
| 373 |
"""tan(x) |
| 374 |
|
| 375 |
Return the tangent of x (measured in radians). |
| 376 |
""" |
| 377 |
return math1(space, math.tan, w_x) |
| 378 |
|
| 379 |
def asin(space, w_x): |
| 380 |
"""asin(x) |
| 381 |
|
| 382 |
Return the arc sine (measured in radians) of x. |
| 383 |
""" |
| 384 |
return math1(space, math.asin, w_x) |
| 385 |
|
| 386 |
def fabs(space, w_x): |
| 387 |
"""fabs(x) |
| 388 |
|
| 389 |
Return the absolute value of the float x. |
| 390 |
""" |
| 391 |
return math1(space, math.fabs, w_x) |
| 392 |
|
| 393 |
def floor(space, w_x): |
| 394 |
"""floor(x) |
| 395 |
|
| 396 |
Return the floor of x as an int. |
| 397 |
This is the largest integral value <= x. |
| 398 |
""" |
| 399 |
from pypy.objspace.std.floatobject import newint_from_float |
| 400 |
w_descr = space.lookup(w_x, '__floor__') |
| 401 |
if w_descr is not None: |
| 402 |
return space.get_and_call_function(w_descr, w_x) |
| 403 |
x = _get_double(space, w_x) |
| 404 |
return newint_from_float(space, math.floor(x)) |
| 405 |
|
| 406 |
def sqrt(space, w_x): |
| 407 |
"""sqrt(x) |
| 408 |
|
| 409 |
Return the square root of x. |
| 410 |
""" |
| 411 |
return math1(space, math.sqrt, w_x) |
| 412 |
|
| 413 |
def frexp(space, w_x): |
| 414 |
"""frexp(x) |
| 415 |
|
| 416 |
Return the mantissa and exponent of x, as pair (m, e). |
| 417 |
m is a float and e is an int, such that x = m * 2.**e. |
| 418 |
If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0. |
| 419 |
""" |
| 420 |
mant, expo = math1_w(space, math.frexp, w_x) |
| 421 |
return space.newtuple2(space.newfloat(mant), space.newint(expo)) |
| 422 |
|
| 423 |
degToRad = math.pi / 180.0 |
| 424 |
|
| 425 |
def degrees(space, w_x): |
| 426 |
"""degrees(x) -> converts angle x from radians to degrees |
| 427 |
""" |
| 428 |
return space.newfloat(_get_double(space, w_x) / degToRad) |
| 429 |
|
| 430 |
def _log_any(space, w_x, base): |
| 431 |
|
| 432 |
try: |
| 433 |
try: |
| 434 |
x = _get_double(space, w_x) |
| 435 |
except OperationError as e: |
| 436 |
if not e.match(space, space.w_OverflowError): |
| 437 |
raise |
| 438 |
if not space.isinstance_w(w_x, space.w_int): |
| 439 |
raise |
| 440 |
|
| 441 |
num = space.bigint_w(w_x) |
| 442 |
result = num.log(base) |
| 443 |
else: |
| 444 |
if base == 10.0: |
| 445 |
result = math.log10(x) |
| 446 |
elif base == 2.0: |
| 447 |
result = rfloat.log2(x) |
| 448 |
else: |
| 449 |
result = math.log(x) |
| 450 |
if base != 0.0: |
| 451 |
den = math.log(base) |
| 452 |
result /= den |
| 453 |
except OverflowError: |
| 454 |
raise oefmt(space.w_OverflowError, "math range error") |
| 455 |
except ValueError: |
| 456 |
raise oefmt(space.w_ValueError, "math domain error") |
| 457 |
return space.newfloat(result) |
| 458 |
|
| 459 |
def log(space, w_x, w_base=None): |
| 460 |
"""log(x[, base]) -> the logarithm of x to the given base. |
| 461 |
If the base not specified, returns the natural logarithm (base e) of x. |
| 462 |
""" |
| 463 |
if w_base is None: |
| 464 |
base = 0.0 |
| 465 |
else: |
| 466 |
base = _get_double(space, w_base) |
| 467 |
if base <= 0.0: |
| 468 |
|
| 469 |
return math1(space, math.log, w_base) |
| 470 |
return _log_any(space, w_x, base) |
| 471 |
|
| 472 |
def log2(space, w_x): |
| 473 |
"""log2(x) -> the base 2 logarithm of x. |
| 474 |
""" |
| 475 |
return _log_any(space, w_x, 2.0) |
| 476 |
|
| 477 |
def log10(space, w_x): |
| 478 |
"""log10(x) -> the base 10 logarithm of x. |
| 479 |
""" |
| 480 |
return _log_any(space, w_x, 10.0) |
| 481 |
|
| 482 |
def fmod(space, w_x, w_y): |
| 483 |
"""fmod(x,y) |
| 484 |
|
| 485 |
Return fmod(x, y), according to platform C. x % y may differ. |
| 486 |
""" |
| 487 |
return math2(space, math.fmod, w_x, w_y) |
| 488 |
|
| 489 |
def atan(space, w_x): |
| 490 |
"""atan(x) |
| 491 |
|
| 492 |
Return the arc tangent (measured in radians) of x. |
| 493 |
""" |
| 494 |
return math1(space, math.atan, w_x) |
| 495 |
|
| 496 |
def ceil(space, w_x): |
| 497 |
"""ceil(x) |
| 498 |
|
| 499 |
Return the ceiling of x as an int. |
| 500 |
This is the smallest integral value >= x. |
| 501 |
""" |
| 502 |
from pypy.objspace.std.floatobject import newint_from_float |
| 503 |
w_descr = space.lookup(w_x, '__ceil__') |
| 504 |
if w_descr is not None: |
| 505 |
return space.get_and_call_function(w_descr, w_x) |
| 506 |
return newint_from_float(space, math1_w(space, math.ceil, w_x)) |
| 507 |
|
| 508 |
def sinh(space, w_x): |
| 509 |
"""sinh(x) |
| 510 |
|
| 511 |
Return the hyperbolic sine of x. |
| 512 |
""" |
| 513 |
return math1(space, math.sinh, w_x) |
| 514 |
|
| 515 |
def cos(space, w_x): |
| 516 |
"""cos(x) |
| 517 |
|
| 518 |
Return the cosine of x (measured in radians). |
| 519 |
""" |
| 520 |
return math1(space, math.cos, w_x) |
| 521 |
|
| 522 |
def tanh(space, w_x): |
| 523 |
"""tanh(x) |
| 524 |
|
| 525 |
Return the hyperbolic tangent of x. |
| 526 |
""" |
| 527 |
return math1(space, math.tanh, w_x) |
| 528 |
|
| 529 |
def radians(space, w_x): |
| 530 |
"""radians(x) -> converts angle x from degrees to radians |
| 531 |
""" |
| 532 |
return space.newfloat(_get_double(space, w_x) * degToRad) |
| 533 |
|
| 534 |
def sin(space, w_x): |
| 535 |
"""sin(x) |
| 536 |
|
| 537 |
Return the sine of x (measured in radians). |
| 538 |
""" |
| 539 |
return math1(space, math.sin, w_x) |
| 540 |
|
| 541 |
def atan2(space, w_y, w_x): |
| 542 |
"""atan2(y, x) |
| 543 |
|
| 544 |
Return the arc tangent (measured in radians) of y/x. |
| 545 |
Unlike atan(y/x), the signs of both x and y are considered. |
| 546 |
""" |
| 547 |
return math2(space, math.atan2, w_y, w_x) |
| 548 |
|
| 549 |
def modf(space, w_x): |
| 550 |
"""modf(x) |
| 551 |
|
| 552 |
Return the fractional and integer parts of x. Both results carry the sign |
| 553 |
of x. The integer part is returned as a real. |
| 554 |
""" |
| 555 |
frac, intpart = math1_w(space, math.modf, w_x) |
| 556 |
return space.newtuple2(space.newfloat(frac), space.newfloat(intpart)) |
| 557 |
|
| 558 |
def exp(space, w_x): |
| 559 |
"""exp(x) |
| 560 |
|
| 561 |
Return e raised to the power of x. |
| 562 |
""" |
| 563 |
return math1(space, math.exp, w_x) |
| 564 |
|
| 565 |
def acos(space, w_x): |
| 566 |
"""acos(x) |
| 567 |
|
| 568 |
Return the arc cosine (measured in radians) of x. |
| 569 |
""" |
| 570 |
return math1(space, math.acos, w_x) |
| 571 |
|
| 572 |
def fsum(space, w_iterable): |
| 573 |
"""Sum an iterable of floats, trying to keep precision.""" |
| 574 |
w_iter = space.iter(w_iterable) |
| 575 |
inf_sum = special_sum = 0.0 |
| 576 |
partials = [] |
| 577 |
while True: |
| 578 |
try: |
| 579 |
w_value = space.next(w_iter) |
| 580 |
except OperationError as e: |
| 581 |
if not e.match(space, space.w_StopIteration): |
| 582 |
raise |
| 583 |
break |
| 584 |
v = _get_double(space, w_value) |
| 585 |
original = v |
| 586 |
added = 0 |
| 587 |
for y in partials: |
| 588 |
if abs(v) < abs(y): |
| 589 |
v, y = y, v |
| 590 |
hi = v + y |
| 591 |
yr = hi - v |
| 592 |
lo = y - yr |
| 593 |
if lo != 0.0: |
| 594 |
partials[added] = lo |
| 595 |
added += 1 |
| 596 |
v = hi |
| 597 |
del partials[added:] |
| 598 |
if v != 0.0: |
| 599 |
if not rfloat.isfinite(v): |
| 600 |
if rfloat.isfinite(original): |
| 601 |
raise oefmt(space.w_OverflowError, "intermediate overflow") |
| 602 |
if math.isinf(original): |
| 603 |
inf_sum += original |
| 604 |
special_sum += original |
| 605 |
del partials[:] |
| 606 |
else: |
| 607 |
partials.append(v) |
| 608 |
if special_sum != 0.0: |
| 609 |
if math.isnan(inf_sum): |
| 610 |
raise oefmt(space.w_ValueError, "-inf + inf") |
| 611 |
return space.newfloat(special_sum) |
| 612 |
hi = 0.0 |
| 613 |
if partials: |
| 614 |
hi = partials[-1] |
| 615 |
j = 0 |
| 616 |
lo = 0 |
| 617 |
for j in range(len(partials) - 2, -1, -1): |
| 618 |
v = hi |
| 619 |
y = partials[j] |
| 620 |
assert abs(y) < abs(v) |
| 621 |
hi = v + y |
| 622 |
yr = hi - v |
| 623 |
lo = y - yr |
| 624 |
if lo != 0.0: |
| 625 |
break |
| 626 |
if j > 0 and (lo < 0.0 and partials[j - 1] < 0.0 or |
| 627 |
lo > 0.0 and partials[j - 1] > 0.0): |
| 628 |
y = lo * 2.0 |
| 629 |
v = hi + y |
| 630 |
yr = v - hi |
| 631 |
if y == yr: |
| 632 |
hi = v |
| 633 |
return space.newfloat(hi) |
| 634 |
|
| 635 |
def log1p(space, w_x): |
| 636 |
"""Find log(x + 1).""" |
| 637 |
try: |
| 638 |
return math1(space, rfloat.log1p, w_x) |
| 639 |
except OperationError as e: |
| 640 |
|
| 641 |
if not e.match(space, space.w_OverflowError): |
| 642 |
raise |
| 643 |
raise oefmt(space.w_ValueError, "math domain error") |
| 644 |
|
| 645 |
def acosh(space, w_x): |
| 646 |
"""Inverse hyperbolic cosine""" |
| 647 |
return math1(space, rfloat.acosh, w_x) |
| 648 |
|
| 649 |
def asinh(space, w_x): |
| 650 |
"""Inverse hyperbolic sine""" |
| 651 |
return math1(space, rfloat.asinh, w_x) |
| 652 |
|
| 653 |
def atanh(space, w_x): |
| 654 |
"""Inverse hyperbolic tangent""" |
| 655 |
return math1(space, rfloat.atanh, w_x) |
| 656 |
|
| 657 |
def expm1(space, w_x): |
| 658 |
"""exp(x) - 1""" |
| 659 |
return math1(space, rfloat.expm1, w_x) |
| 660 |
|
| 661 |
def erf(space, w_x): |
| 662 |
"""The error function""" |
| 663 |
return math1(space, rfloat.erf, w_x) |
| 664 |
|
| 665 |
def erfc(space, w_x): |
| 666 |
"""The complementary error function""" |
| 667 |
return math1(space, rfloat.erfc, w_x) |
| 668 |
|
| 669 |
def gamma(space, w_x): |
| 670 |
"""Compute the gamma function for x.""" |
| 671 |
return math1(space, rfloat.gamma, w_x) |
| 672 |
|
| 673 |
def lgamma(space, w_x): |
| 674 |
"""Compute the natural logarithm of the gamma function for x.""" |
| 675 |
return math1(space, rfloat.lgamma, w_x) |
| 676 |
|
| 677 |
@unwrap_spec(w_rel_tol=WrappedDefault(1e-09), w_abs_tol=WrappedDefault(0.0)) |
| 678 |
def isclose(space, w_a, w_b, __kwonly__, w_rel_tol, w_abs_tol): |
| 679 |
"""isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) -> bool |
| 680 |
|
| 681 |
Determine whether two floating point numbers are close in value. |
| 682 |
|
| 683 |
rel_tol |
| 684 |
maximum difference for being considered "close", relative to the |
| 685 |
magnitude of the input values |
| 686 |
abs_tol |
| 687 |
maximum difference for being considered "close", regardless of the |
| 688 |
magnitude of the input values |
| 689 |
|
| 690 |
Return True if a is close in value to b, and False otherwise. |
| 691 |
|
| 692 |
For the values to be considered close, the difference between them |
| 693 |
must be smaller than at least one of the tolerances. |
| 694 |
|
| 695 |
-inf, inf and NaN behave similarly to the IEEE 754 Standard. That |
| 696 |
is, NaN is not close to anything, even itself. inf and -inf are |
| 697 |
only close to themselves.""" |
| 698 |
a = _get_double(space, w_a) |
| 699 |
b = _get_double(space, w_b) |
| 700 |
rel_tol = _get_double(space, w_rel_tol) |
| 701 |
abs_tol = _get_double(space, w_abs_tol) |
| 702 |
|
| 703 |
|
| 704 |
if rel_tol < 0.0 or abs_tol < 0.0: |
| 705 |
raise oefmt(space.w_ValueError, "tolerances must be non-negative") |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
if a == b: |
| 710 |
return space.w_True |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
if math.isinf(a) or math.isinf(b): |
| 718 |
return space.w_False |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
diff = math.fabs(b - a) |
| 723 |
result = ((diff <= math.fabs(rel_tol * b) or |
| 724 |
diff <= math.fabs(rel_tol * a)) or |
| 725 |
diff <= abs_tol) |
| 726 |
return space.newbool(result) |
| 727 |
|
| 728 |
def gcd(space, integers_w): |
| 729 |
"""greatest common divisor""" |
| 730 |
if len(integers_w) == 0: |
| 731 |
return space.newint(0) |
| 732 |
if len(integers_w) == 1: |
| 733 |
space.index(integers_w[0]) |
| 734 |
return space.abs(integers_w[0]) |
| 735 |
if len(integers_w) == 2: |
| 736 |
return gcd_two(space, integers_w[0], integers_w[1]) |
| 737 |
return _gcd_many(space, integers_w) |
| 738 |
|
| 739 |
def _gcd_many(space, integers_w): |
| 740 |
w_res = integers_w[0] |
| 741 |
|
| 742 |
for i in range(1, len(integers_w)): |
| 743 |
w_res = gcd_two(space, w_res, integers_w[i]) |
| 744 |
return w_res |
| 745 |
|
| 746 |
|
| 747 |
def gcd_two(space, w_a, w_b): |
| 748 |
from rpython.rlib import rbigint |
| 749 |
w_a = space.abs(space.index(w_a)) |
| 750 |
w_b = space.abs(space.index(w_b)) |
| 751 |
try: |
| 752 |
a = space.int_w(w_a) |
| 753 |
b = space.int_w(w_b) |
| 754 |
except OperationError as e: |
| 755 |
if not e.match(space, space.w_OverflowError): |
| 756 |
raise |
| 757 |
|
| 758 |
a = space.bigint_w(w_a) |
| 759 |
b = space.bigint_w(w_b) |
| 760 |
g = a.gcd(b) |
| 761 |
return space.newlong_from_rbigint(g) |
| 762 |
else: |
| 763 |
g = rbigint.gcd_binary(a, b) |
| 764 |
return space.newint(g) |
| 765 |
|
| 766 |
def nextafter(space, w_a, w_b): |
| 767 |
""" Return the next floating-point value after x towards y. """ |
| 768 |
a = _get_double(space, w_a) |
| 769 |
b = _get_double(space, w_b) |
| 770 |
return space.newfloat(rfloat.nextafter(a, b)) |
| 771 |
|
| 772 |
def ulp(space, w_x): |
| 773 |
"""Return the value of the least significant bit of the |
| 774 |
float x. |
| 775 |
""" |
| 776 |
x = _get_double(space, w_x) |
| 777 |
if math.isnan(x): |
| 778 |
return w_x |
| 779 |
x = math.fabs(float(x)) |
| 780 |
if math.isinf(x): |
| 781 |
return space.newfloat(x) |
| 782 |
|
| 783 |
x2 = rfloat.nextafter(x, rfloat.INFINITY) |
| 784 |
if math.isinf(x2): |
| 785 |
|
| 786 |
x2 = rfloat.nextafter(x, -rfloat.INFINITY) |
| 787 |
return space.newfloat(x - x2) |
| 788 |
return space.newfloat(x2 - x) |
| 789 |
|
| 790 |
def exp2(space, w_x): |
| 791 |
'Return 2 raised to the power of x.' |
| 792 |
return math1(space, rfloat.exp2, w_x) |
| 793 |
|
| 794 |
def cbrt(space, w_x): |
| 795 |
'Return the cube root of x.' |
| 796 |
return math1(space, rfloat.cbrt, w_x) |