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

Method quantize

Lib/_pydecimal.py:2498–2557  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

2496 return _dec_from_triple(dup._sign, dup._int[:end], exp)
2497
2498 def quantize(self, exp, rounding=None, context=None):
2499 """Quantize self so its exponent is the same as that of exp.
2500
2501 Similar to self._rescale(exp._exp) but with error checking.
2502 """
2503 exp = _convert_other(exp, raiseit=True)
2504
2505 if context is None:
2506 context = getcontext()
2507 if rounding is None:
2508 rounding = context.rounding
2509
2510 if self._is_special or exp._is_special:
2511 ans = self._check_nans(exp, context)
2512 if ans:
2513 return ans
2514
2515 if exp._isinfinity() or self._isinfinity():
2516 if exp._isinfinity() and self._isinfinity():
2517 return Decimal(self) # if both are inf, it is OK
2518 return context._raise_error(InvalidOperation,
2519 'quantize with one INF')
2520
2521 # exp._exp should be between Etiny and Emax
2522 if not (context.Etiny() <= exp._exp <= context.Emax):
2523 return context._raise_error(InvalidOperation,
2524 'target exponent out of bounds in quantize')
2525
2526 if not self:
2527 ans = _dec_from_triple(self._sign, '0', exp._exp)
2528 return ans._fix(context)
2529
2530 self_adjusted = self.adjusted()
2531 if self_adjusted > context.Emax:
2532 return context._raise_error(InvalidOperation,
2533 'exponent of quantize result too large for current context')
2534 if self_adjusted - exp._exp + 1 > context.prec:
2535 return context._raise_error(InvalidOperation,
2536 'quantize result has too many digits for current context')
2537
2538 ans = self._rescale(exp._exp, rounding)
2539 if ans.adjusted() > context.Emax:
2540 return context._raise_error(InvalidOperation,
2541 'exponent of quantize result too large for current context')
2542 if len(ans._int) > context.prec:
2543 return context._raise_error(InvalidOperation,
2544 'quantize result has too many digits for current context')
2545
2546 # raise appropriate flags
2547 if ans and ans.adjusted() < context.Emin:
2548 context._raise_error(Subnormal)
2549 if ans._exp > self._exp:
2550 if ans != self:
2551 context._raise_error(Inexact)
2552 context._raise_error(Rounded)
2553
2554 # call to fix takes care of any necessary folddown, and
2555 # signals Clamped if necessary

Callers 7

__round__Method · 0.95
test_quantizeMethod · 0.95
decimal_roundMethod · 0.95
quantizeMethod · 0.45
test_none_argsMethod · 0.45
test_quantizeMethod · 0.45

Calls 12

_check_nansMethod · 0.95
_isinfinityMethod · 0.95
adjustedMethod · 0.95
_rescaleMethod · 0.95
_convert_otherFunction · 0.85
getcontextFunction · 0.85
DecimalClass · 0.85
_dec_from_tripleFunction · 0.85
lenFunction · 0.85
_raise_errorMethod · 0.80
EtinyMethod · 0.80
_fixMethod · 0.80

Tested by 5

test_quantizeMethod · 0.76
decimal_roundMethod · 0.76
test_none_argsMethod · 0.36
test_quantizeMethod · 0.36