Remainder nearest to 0- abs(remainder-near) <= other/2
(self, other, context=None)
| 1449 | return other.__mod__(self, context=context) |
| 1450 | |
| 1451 | def remainder_near(self, other, context=None): |
| 1452 | """ |
| 1453 | Remainder nearest to 0- abs(remainder-near) <= other/2 |
| 1454 | """ |
| 1455 | if context is None: |
| 1456 | context = getcontext() |
| 1457 | |
| 1458 | other = _convert_other(other, raiseit=True) |
| 1459 | |
| 1460 | ans = self._check_nans(other, context) |
| 1461 | if ans: |
| 1462 | return ans |
| 1463 | |
| 1464 | # self == +/-infinity -> InvalidOperation |
| 1465 | if self._isinfinity(): |
| 1466 | return context._raise_error(InvalidOperation, |
| 1467 | 'remainder_near(infinity, x)') |
| 1468 | |
| 1469 | # other == 0 -> either InvalidOperation or DivisionUndefined |
| 1470 | if not other: |
| 1471 | if self: |
| 1472 | return context._raise_error(InvalidOperation, |
| 1473 | 'remainder_near(x, 0)') |
| 1474 | else: |
| 1475 | return context._raise_error(DivisionUndefined, |
| 1476 | 'remainder_near(0, 0)') |
| 1477 | |
| 1478 | # other = +/-infinity -> remainder = self |
| 1479 | if other._isinfinity(): |
| 1480 | ans = Decimal(self) |
| 1481 | return ans._fix(context) |
| 1482 | |
| 1483 | # self = 0 -> remainder = self, with ideal exponent |
| 1484 | ideal_exponent = min(self._exp, other._exp) |
| 1485 | if not self: |
| 1486 | ans = _dec_from_triple(self._sign, '0', ideal_exponent) |
| 1487 | return ans._fix(context) |
| 1488 | |
| 1489 | # catch most cases of large or small quotient |
| 1490 | expdiff = self.adjusted() - other.adjusted() |
| 1491 | if expdiff >= context.prec + 1: |
| 1492 | # expdiff >= prec+1 => abs(self/other) > 10**prec |
| 1493 | return context._raise_error(DivisionImpossible) |
| 1494 | if expdiff <= -2: |
| 1495 | # expdiff <= -2 => abs(self/other) < 0.1 |
| 1496 | ans = self._rescale(ideal_exponent, context.rounding) |
| 1497 | return ans._fix(context) |
| 1498 | |
| 1499 | # adjust both arguments to have the same exponent, then divide |
| 1500 | op1 = _WorkRep(self) |
| 1501 | op2 = _WorkRep(other) |
| 1502 | if op1.exp >= op2.exp: |
| 1503 | op1.int *= 10**(op1.exp - op2.exp) |
| 1504 | else: |
| 1505 | op2.int *= 10**(op2.exp - op1.exp) |
| 1506 | q, r = divmod(op1.int, op2.int) |
| 1507 | # remainder is r*10**ideal_exponent; other is +/-op2.int * |
| 1508 | # 10**ideal_exponent. Apply correction to ensure that |