MCPcopy Index your code
hub / github.com/RustPython/RustPython / next_toward

Method next_toward

Lib/_pydecimal.py:3518–3562  ·  view source on GitHub ↗

Returns the number closest to self, in the direction towards other. The result is the closest representable number to self (excluding self) that is in the direction towards other, unless both have the same value. If the two operands are numerically equal, then the r

(self, other, context=None)

Source from the content-addressed store, hash-verified

3516 context)
3517
3518 def next_toward(self, other, context=None):
3519 """Returns the number closest to self, in the direction towards other.
3520
3521 The result is the closest representable number to self
3522 (excluding self) that is in the direction towards other,
3523 unless both have the same value. If the two operands are
3524 numerically equal, then the result is a copy of self with the
3525 sign set to be the same as the sign of other.
3526 """
3527 other = _convert_other(other, raiseit=True)
3528
3529 if context is None:
3530 context = getcontext()
3531
3532 ans = self._check_nans(other, context)
3533 if ans:
3534 return ans
3535
3536 comparison = self._cmp(other)
3537 if comparison == 0:
3538 return self.copy_sign(other)
3539
3540 if comparison == -1:
3541 ans = self.next_plus(context)
3542 else: # comparison == 1
3543 ans = self.next_minus(context)
3544
3545 # decide which flags to raise using value of ans
3546 if ans._isinfinity():
3547 context._raise_error(Overflow,
3548 'Infinite result from next_toward',
3549 ans._sign)
3550 context._raise_error(Inexact)
3551 context._raise_error(Rounded)
3552 elif ans.adjusted() < context.Emin:
3553 context._raise_error(Underflow)
3554 context._raise_error(Subnormal)
3555 context._raise_error(Inexact)
3556 context._raise_error(Rounded)
3557 # if precision == 1 then we don't raise Clamped for a
3558 # result 0E-Etiny.
3559 if not ans:
3560 context._raise_error(Clamped)
3561
3562 return ans
3563
3564 def number_class(self, context=None):
3565 """Returns an indication of the class of self.

Callers 3

next_towardMethod · 0.45
test_named_parametersMethod · 0.45

Calls 10

_check_nansMethod · 0.95
_cmpMethod · 0.95
copy_signMethod · 0.95
next_plusMethod · 0.95
next_minusMethod · 0.95
_convert_otherFunction · 0.85
getcontextFunction · 0.85
_isinfinityMethod · 0.80
_raise_errorMethod · 0.80
adjustedMethod · 0.80

Tested by 2

test_named_parametersMethod · 0.36