Quantize self so its exponent is the same as that of exp. Similar to self._rescale(exp._exp) but with error checking.
(self, exp, rounding=None, context=None)
| 2544 | return _dec_from_triple(dup._sign, dup._int[:end], exp) |
| 2545 | |
| 2546 | def quantize(self, exp, rounding=None, context=None): |
| 2547 | """Quantize self so its exponent is the same as that of exp. |
| 2548 | |
| 2549 | Similar to self._rescale(exp._exp) but with error checking. |
| 2550 | """ |
| 2551 | exp = _convert_other(exp, raiseit=True) |
| 2552 | |
| 2553 | if context is None: |
| 2554 | context = getcontext() |
| 2555 | if rounding is None: |
| 2556 | rounding = context.rounding |
| 2557 | |
| 2558 | if self._is_special or exp._is_special: |
| 2559 | ans = self._check_nans(exp, context) |
| 2560 | if ans: |
| 2561 | return ans |
| 2562 | |
| 2563 | if exp._isinfinity() or self._isinfinity(): |
| 2564 | if exp._isinfinity() and self._isinfinity(): |
| 2565 | return Decimal(self) # if both are inf, it is OK |
| 2566 | return context._raise_error(InvalidOperation, |
| 2567 | 'quantize with one INF') |
| 2568 | |
| 2569 | # exp._exp should be between Etiny and Emax |
| 2570 | if not (context.Etiny() <= exp._exp <= context.Emax): |
| 2571 | return context._raise_error(InvalidOperation, |
| 2572 | 'target exponent out of bounds in quantize') |
| 2573 | |
| 2574 | if not self: |
| 2575 | ans = _dec_from_triple(self._sign, '0', exp._exp) |
| 2576 | return ans._fix(context) |
| 2577 | |
| 2578 | self_adjusted = self.adjusted() |
| 2579 | if self_adjusted > context.Emax: |
| 2580 | return context._raise_error(InvalidOperation, |
| 2581 | 'exponent of quantize result too large for current context') |
| 2582 | if self_adjusted - exp._exp + 1 > context.prec: |
| 2583 | return context._raise_error(InvalidOperation, |
| 2584 | 'quantize result has too many digits for current context') |
| 2585 | |
| 2586 | ans = self._rescale(exp._exp, rounding) |
| 2587 | if ans.adjusted() > context.Emax: |
| 2588 | return context._raise_error(InvalidOperation, |
| 2589 | 'exponent of quantize result too large for current context') |
| 2590 | if len(ans._int) > context.prec: |
| 2591 | return context._raise_error(InvalidOperation, |
| 2592 | 'quantize result has too many digits for current context') |
| 2593 | |
| 2594 | # raise appropriate flags |
| 2595 | if ans and ans.adjusted() < context.Emin: |
| 2596 | context._raise_error(Subnormal) |
| 2597 | if ans._exp > self._exp: |
| 2598 | if ans != self: |
| 2599 | context._raise_error(Inexact) |
| 2600 | context._raise_error(Rounded) |
| 2601 | |
| 2602 | # call to fix takes care of any necessary folddown, and |
| 2603 | # signals Clamped if necessary |
no test coverage detected