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