MCPcopy Index your code
hub / github.com/python/cpython / quantize

Method quantize

Lib/_pydecimal.py:5202–5258  ·  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

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

Callers 1

test_quantizeMethod · 0.95

Calls 2

_convert_otherFunction · 0.85
quantizeMethod · 0.45

Tested by 1

test_quantizeMethod · 0.76