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

Method quantize

Lib/_pydecimal.py:5199–5255  ·  view source on GitHub ↗

Returns a value equal to 'a' (rounded), having the exponent of 'b'. The coefficient of the result is derived from that of the left-hand operand. It may be rounded using the current rounding setting (if the exponent is being increased), multiplied by a positive power of ten

(self, a, b)

Source from the content-addressed store, hash-verified

5197 return r
5198
5199 def quantize(self, a, b):
5200 """Returns a value equal to 'a' (rounded), having the exponent of 'b'.
5201
5202 The coefficient of the result is derived from that of the left-hand
5203 operand. It may be rounded using the current rounding setting (if the
5204 exponent is being increased), multiplied by a positive power of ten (if
5205 the exponent is being decreased), or is unchanged (if the exponent is
5206 already equal to that of the right-hand operand).
5207
5208 Unlike other operations, if the length of the coefficient after the
5209 quantize operation would be greater than precision then an Invalid
5210 operation condition is raised. This guarantees that, unless there is
5211 an error condition, the exponent of the result of a quantize is always
5212 equal to that of the right-hand operand.
5213
5214 Also unlike other operations, quantize will never raise Underflow, even
5215 if the result is subnormal and inexact.
5216
5217 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
5218 Decimal('2.170')
5219 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
5220 Decimal('2.17')
5221 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
5222 Decimal('2.2')
5223 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
5224 Decimal('2')
5225 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
5226 Decimal('0E+1')
5227 >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
5228 Decimal('-Infinity')
5229 >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
5230 Decimal('NaN')
5231 >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
5232 Decimal('-0')
5233 >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
5234 Decimal('-0E+5')
5235 >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
5236 Decimal('NaN')
5237 >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
5238 Decimal('NaN')
5239 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
5240 Decimal('217.0')
5241 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
5242 Decimal('217')
5243 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
5244 Decimal('2.2E+2')
5245 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
5246 Decimal('2E+2')
5247 >>> ExtendedContext.quantize(1, 2)
5248 Decimal('1')
5249 >>> ExtendedContext.quantize(Decimal(1), 2)
5250 Decimal('1')
5251 >>> ExtendedContext.quantize(1, Decimal(2))
5252 Decimal('1')
5253 """
5254 a = _convert_other(a, raiseit=True)
5255 return a.quantize(b, context=self)
5256

Callers 1

test_quantizeMethod · 0.95

Calls 2

_convert_otherFunction · 0.85
quantizeMethod · 0.45

Tested by 1

test_quantizeMethod · 0.76