Lines Matching full:decimal
16 """Python decimal arithmetic module"""
20 'Decimal', 'Context',
54 __name__ = 'decimal' # For pickling
56 # See http://speleotrove.com/decimal/
65 DecimalTuple = _namedtuple('DecimalTuple', 'sign digits exponent', module='decimal')
313 permitted in the Decimal() constructor, context.create_decimal() and
317 Decimal.from_float() or context.create_decimal_from_float() do not
378 The returned context manager creates a local decimal context
391 # General Decimal Arithmetic Specification
420 ##### Decimal class #######################################################
422 # Do not subclass Decimal from numbers.Real and do not register it as such
426 class Decimal(object): class
427 """Floating-point class for decimal arithmetic."""
430 # Generally, the value of the Decimal instance is given by
436 """Create a decimal point instance.
438 >>> Decimal('3.14') # string input
439 Decimal('3.14')
440 >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
441 Decimal('3.14')
442 >>> Decimal(314) # int
443 Decimal('314')
444 >>> Decimal(Decimal(314)) # another decimal instance
445 Decimal('314')
446 >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
447 Decimal('3.14')
455 # and the Decimal constructor still deal with tuples of
468 "Invalid literal for Decimal: %r" % value)
509 # From another decimal
510 if isinstance(value, Decimal):
528 raise ValueError('Invalid tuple size in creation of Decimal '
576 value = Decimal.from_float(value)
583 raise TypeError("Cannot convert %r to Decimal" % value)
587 """Converts a float to a decimal number, exactly.
589 Note that Decimal.from_float(0.1) is not the same as Decimal('0.1').
592 0x1.999999999999ap-4. The exact equivalent of the value in decimal
595 >>> Decimal.from_float(0.1)
596 Decimal('0.1000000000000000055511151231257827021181583404541015625')
597 >>> Decimal.from_float(float('nan'))
598 Decimal('NaN')
599 >>> Decimal.from_float(float('inf'))
600 Decimal('Infinity')
601 >>> Decimal.from_float(-float('inf'))
602 Decimal('-Infinity')
603 >>> Decimal.from_float(-0.0)
604 Decimal('-0')
625 if cls is Decimal:
731 """Compare the two non-NaN decimal instances self and other.
746 # check for zeros; Decimal('0') == Decimal('-0')
777 # Note: The Decimal standard doesn't cover rich comparisons for
839 """Compare self to other. Return a decimal value:
841 a or b is a NaN ==> Decimal('NaN')
842 a < b ==> Decimal('-1')
843 a == b ==> Decimal('0')
844 a > b ==> Decimal('1')
854 return Decimal(self._cmp(other))
859 # In order to make sure that the hash of a Decimal instance
890 """Express a finite Decimal instance in the form n / d.
895 >>> Decimal('3.14').as_integer_ratio()
897 >>> Decimal('-123e5').as_integer_ratio()
899 >>> Decimal('0.00').as_integer_ratio()
940 """Represents the number as an instance of Decimal."""
942 return "Decimal('%s')" % str(self)
959 # number of digits of self._int to left of decimal point
963 # decimal point in the mantissa of the output string (that is,
1000 can leave up to 3 digits to the left of the decimal place and may
1019 # -Decimal('0') is Decimal('0'), not Decimal('-0'), except
1044 ans = Decimal(self)
1091 return Decimal(self)
1093 return Decimal(other) # Can't both be infinity here
1152 ans = Decimal(result)
1442 ans = Decimal(self)
1554 return Decimal(0)
1572 return Decimal(self)
1580 self - Decimal instance
1590 return Decimal(self)
1603 return Decimal(self)
1664 return Decimal(self)
1667 # self is a finite, nonzero Decimal
1746 If only one argument is supplied, round a finite Decimal
1752 >>> round(Decimal('123.456'))
1754 >>> round(Decimal('-456.789'))
1756 >>> round(Decimal('-3.0'))
1758 >>> round(Decimal('2.5'))
1760 >>> round(Decimal('3.5'))
1762 >>> round(Decimal('Inf'))
1766 >>> round(Decimal('NaN'))
1772 decimal places using the rounding mode for the current
1776 self.quantize(Decimal('1En')).
1778 >>> round(Decimal('123.456'), 0)
1779 Decimal('123')
1780 >>> round(Decimal('123.456'), 2)
1781 Decimal('123.46')
1782 >>> round(Decimal('123.456'), -2)
1783 Decimal('1E+2')
1784 >>> round(Decimal('-Infinity'), 37)
1785 Decimal('NaN')
1786 >>> round(Decimal('sNaN123'), 0)
1787 Decimal('NaN123')
1808 For a finite Decimal instance self, return the greatest
1823 For a finite Decimal instance self, return the least integer n
1928 # additional restriction for decimal: the modulus must be less
1951 # Decimal integers (i.e. force their exponents to be >= 0)
2479 return Decimal(self) # if both are inf, it is OK
2548 return Decimal(self)
2571 """Round a nonzero, nonspecial Decimal to a fixed number of
2583 return Decimal(self)
2607 return Decimal(self)
2609 return Decimal(self)
2632 return Decimal(self)
2634 return Decimal(self)
2652 return Decimal(self)
2667 # <= sqrt(c) < 10**p, so the closest representable Decimal at
2840 """Returns the same Decimal object.
2981 return Decimal(self)
3038 Currently, the encoding of a Decimal instance is always
3039 canonical, so this method returns True for any Decimal.
3046 A Decimal instance is considered finite if it is neither
3223 ans = Decimal(self._exp + len(self._int) - 1)
3272 # Decimal. Note that no attempt is made to fit the result
3274 ans = Decimal(self.adjusted())
3557 """Just returns 10, as this is Decimal, :)"""
3558 return Decimal(10)
3577 return Decimal(self)
3612 return Decimal(self)
3635 return Decimal(self)
3661 if type(self) is Decimal:
3666 if type(self) is Decimal:
3673 """Format a Decimal instance according to the given specifier.
3683 # there should be at least one digit after the decimal point.
3685 # Decimal---it's presumably there to make sure that
3727 # figure out placement of the decimal point
3742 # find digits before and after decimal point, and get exponent
3754 # done with the decimal-specific stuff; hand over the rest
3759 """Create a decimal instance directly, without any validation,
3766 self = object.__new__(Decimal)
3774 # Register Decimal as a kind of Number (an abstract base class).
3777 _numbers.Number.register(Decimal)
3786 the previous decimal context in __exit__()
3798 """Contains the context for a Decimal instance.
3808 Should be reset by user of Decimal instance.
3900 "'decimal.Context' object has no attribute '%s'" % name)
4023 """Creates a new Decimal instance but using self as context.
4026 IBM Decimal specification."""
4033 d = Decimal(num, context=self)
4040 """Creates a new Decimal instance from a float but rounding using self
4045 Decimal('3.1415')
4050 decimal.Inexact: None
4053 d = Decimal.from_float(f) # An exact conversion
4064 >>> ExtendedContext.abs(Decimal('2.1'))
4065 Decimal('2.1')
4066 >>> ExtendedContext.abs(Decimal('-100'))
4067 Decimal('100')
4068 >>> ExtendedContext.abs(Decimal('101.5'))
4069 Decimal('101.5')
4070 >>> ExtendedContext.abs(Decimal('-101.5'))
4071 Decimal('101.5')
4073 Decimal('1')
4081 >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
4082 Decimal('19.00')
4083 >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
4084 Decimal('1.02E+4')
4085 >>> ExtendedContext.add(1, Decimal(2))
4086 Decimal('3')
4087 >>> ExtendedContext.add(Decimal(8), 5)
4088 Decimal('13')
4090 Decimal('10')
4095 raise TypeError("Unable to convert %s to Decimal" % b)
4103 """Returns the same Decimal object.
4108 >>> ExtendedContext.canonical(Decimal('2.50'))
4109 Decimal('2.50')
4111 if not isinstance(a, Decimal):
4112 raise TypeError("canonical requires a Decimal as an argument.")
4129 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
4130 Decimal('-1')
4131 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
4132 Decimal('0')
4133 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
4134 Decimal('0')
4135 >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
4136 Decimal('1')
4137 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
4138 Decimal('1')
4139 >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
4140 Decimal('-1')
4142 Decimal('-1')
4143 >>> ExtendedContext.compare(Decimal(1), 2)
4144 Decimal('-1')
4145 >>> ExtendedContext.compare(1, Decimal(2))
4146 Decimal('-1')
4158 >>> c.compare_signal(Decimal('2.1'), Decimal('3'))
4159 Decimal('-1')
4160 >>> c.compare_signal(Decimal('2.1'), Decimal('2.1'))
4161 Decimal('0')
4165 >>> c.compare_signal(Decimal('NaN'), Decimal('2.1'))
4166 Decimal('NaN')
4172 >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1'))
4173 Decimal('NaN')
4177 Decimal('-1')
4178 >>> c.compare_signal(Decimal(-1), 2)
4179 Decimal('-1')
4180 >>> c.compare_signal(-1, Decimal(2))
4181 Decimal('-1')
4193 >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9'))
4194 Decimal('-1')
4195 >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12'))
4196 Decimal('-1')
4197 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3'))
4198 Decimal('-1')
4199 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30'))
4200 Decimal('0')
4201 >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300'))
4202 Decimal('1')
4203 >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN'))
4204 Decimal('-1')
4206 Decimal('-1')
4207 >>> ExtendedContext.compare_total(Decimal(1), 2)
4208 Decimal('-1')
4209 >>> ExtendedContext.compare_total(1, Decimal(2))
4210 Decimal('-1')
4226 >>> ExtendedContext.copy_abs(Decimal('2.1'))
4227 Decimal('2.1')
4228 >>> ExtendedContext.copy_abs(Decimal('-100'))
4229 Decimal('100')
4231 Decimal('1')
4237 """Returns a copy of the decimal object.
4239 >>> ExtendedContext.copy_decimal(Decimal('2.1'))
4240 Decimal('2.1')
4241 >>> ExtendedContext.copy_decimal(Decimal('-1.00'))
4242 Decimal('-1.00')
4244 Decimal('1')
4247 return Decimal(a)
4252 >>> ExtendedContext.copy_negate(Decimal('101.5'))
4253 Decimal('-101.5')
4254 >>> ExtendedContext.copy_negate(Decimal('-101.5'))
4255 Decimal('101.5')
4257 Decimal('-1')
4268 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33'))
4269 Decimal('1.50')
4270 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33'))
4271 Decimal('1.50')
4272 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33'))
4273 Decimal('-1.50')
4274 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33'))
4275 Decimal('-1.50')
4277 Decimal('-1')
4278 >>> ExtendedContext.copy_sign(Decimal(1), -2)
4279 Decimal('-1')
4280 >>> ExtendedContext.copy_sign(1, Decimal(-2))
4281 Decimal('-1')
4287 """Decimal division in a specified context.
4289 >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
4290 Decimal('0.333333333')
4291 >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
4292 Decimal('0.666666667')
4293 >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
4294 Decimal('2.5')
4295 >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
4296 Decimal('0.1')
4297 >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
4298 Decimal('1')
4299 >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
4300 Decimal('4.00')
4301 >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
4302 Decimal('1.20')
4303 >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
4304 Decimal('10')
4305 >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
4306 Decimal('1000')
4307 >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
4308 Decimal('1.20E+6')
4310 Decimal('1')
4311 >>> ExtendedContext.divide(Decimal(5), 5)
4312 Decimal('1')
4313 >>> ExtendedContext.divide(5, Decimal(5))
4314 Decimal('1')
4319 raise TypeError("Unable to convert %s to Decimal" % b)
4326 >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
4327 Decimal('0')
4328 >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
4329 Decimal('3')
4330 >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
4331 Decimal('3')
4333 Decimal('3')
4334 >>> ExtendedContext.divide_int(Decimal(10), 3)
4335 Decimal('3')
4336 >>> ExtendedContext.divide_int(10, Decimal(3))
4337 Decimal('3')
4342 raise TypeError("Unable to convert %s to Decimal" % b)
4349 >>> ExtendedContext.divmod(Decimal(8), Decimal(3))
4350 (Decimal('2'), Decimal('2'))
4351 >>> ExtendedContext.divmod(Decimal(8), Decimal(4))
4352 (Decimal('2'), Decimal('0'))
4354 (Decimal('2'), Decimal('0'))
4355 >>> ExtendedContext.divmod(Decimal(8), 4)
4356 (Decimal('2'), Decimal('0'))
4357 >>> ExtendedContext.divmod(8, Decimal(4))
4358 (Decimal('2'), Decimal('0'))
4363 raise TypeError("Unable to convert %s to Decimal" % b)
4373 >>> c.exp(Decimal('-Infinity'))
4374 Decimal('0')
4375 >>> c.exp(Decimal('-1'))
4376 Decimal('0.367879441')
4377 >>> c.exp(Decimal('0'))
4378 Decimal('1')
4379 >>> c.exp(Decimal('1'))
4380 Decimal('2.71828183')
4381 >>> c.exp(Decimal('0.693147181'))
4382 Decimal('2.00000000')
4383 >>> c.exp(Decimal('+Infinity'))
4384 Decimal('Infinity')
4386 Decimal('22026.4658')
4398 >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7'))
4399 Decimal('22')
4400 >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7'))
4401 Decimal('-8')
4402 >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578'))
4403 Decimal('1.38435736E+12')
4405 Decimal('7')
4406 >>> ExtendedContext.fma(1, Decimal(3), 4)
4407 Decimal('7')
4408 >>> ExtendedContext.fma(1, 3, Decimal(4))
4409 Decimal('7')
4417 Currently, the encoding of a Decimal instance is always
4418 canonical, so this method returns True for any Decimal.
4420 >>> ExtendedContext.is_canonical(Decimal('2.50'))
4423 if not isinstance(a, Decimal):
4424 raise TypeError("is_canonical requires a Decimal as an argument.")
4430 A Decimal instance is considered finite if it is neither
4433 >>> ExtendedContext.is_finite(Decimal('2.50'))
4435 >>> ExtendedContext.is_finite(Decimal('-0.3'))
4437 >>> ExtendedContext.is_finite(Decimal('0'))
4439 >>> ExtendedContext.is_finite(Decimal('Inf'))
4441 >>> ExtendedContext.is_finite(Decimal('NaN'))
4452 >>> ExtendedContext.is_infinite(Decimal('2.50'))
4454 >>> ExtendedContext.is_infinite(Decimal('-Inf'))
4456 >>> ExtendedContext.is_infinite(Decimal('NaN'))
4468 >>> ExtendedContext.is_nan(Decimal('2.50'))
4470 >>> ExtendedContext.is_nan(Decimal('NaN'))
4472 >>> ExtendedContext.is_nan(Decimal('-sNaN'))
4487 >>> c.is_normal(Decimal('2.50'))
4489 >>> c.is_normal(Decimal('0.1E-999'))
4491 >>> c.is_normal(Decimal('0.00'))
4493 >>> c.is_normal(Decimal('-Inf'))
4495 >>> c.is_normal(Decimal('NaN'))
4506 >>> ExtendedContext.is_qnan(Decimal('2.50'))
4508 >>> ExtendedContext.is_qnan(Decimal('NaN'))
4510 >>> ExtendedContext.is_qnan(Decimal('sNaN'))
4521 >>> ExtendedContext.is_signed(Decimal('2.50'))
4523 >>> ExtendedContext.is_signed(Decimal('-12'))
4525 >>> ExtendedContext.is_signed(Decimal('-0'))
4539 >>> ExtendedContext.is_snan(Decimal('2.50'))
4541 >>> ExtendedContext.is_snan(Decimal('NaN'))
4543 >>> ExtendedContext.is_snan(Decimal('sNaN'))
4557 >>> c.is_subnormal(Decimal('2.50'))
4559 >>> c.is_subnormal(Decimal('0.1E-999'))
4561 >>> c.is_subnormal(Decimal('0.00'))
4563 >>> c.is_subnormal(Decimal('-Inf'))
4565 >>> c.is_subnormal(Decimal('NaN'))
4576 >>> ExtendedContext.is_zero(Decimal('0'))
4578 >>> ExtendedContext.is_zero(Decimal('2.50'))
4580 >>> ExtendedContext.is_zero(Decimal('-0E+2'))
4596 >>> c.ln(Decimal('0'))
4597 Decimal('-Infinity')
4598 >>> c.ln(Decimal('1.000'))
4599 Decimal('0')
4600 >>> c.ln(Decimal('2.71828183'))
4601 Decimal('1.00000000')
4602 >>> c.ln(Decimal('10'))
4603 Decimal('2.30258509')
4604 >>> c.ln(Decimal('+Infinity'))
4605 Decimal('Infinity')
4607 Decimal('0')
4618 >>> c.log10(Decimal('0'))
4619 Decimal('-Infinity')
4620 >>> c.log10(Decimal('0.001'))
4621 Decimal('-3')
4622 >>> c.log10(Decimal('1.000'))
4623 Decimal('0')
4624 >>> c.log10(Decimal('2'))
4625 Decimal('0.301029996')
4626 >>> c.log10(Decimal('10'))
4627 Decimal('1')
4628 >>> c.log10(Decimal('70'))
4629 Decimal('1.84509804')
4630 >>> c.log10(Decimal('+Infinity'))
4631 Decimal('Infinity')
4633 Decimal('-Infinity')
4635 Decimal('0')
4648 >>> ExtendedContext.logb(Decimal('250'))
4649 Decimal('2')
4650 >>> ExtendedContext.logb(Decimal('2.50'))
4651 Decimal('0')
4652 >>> ExtendedContext.logb(Decimal('0.03'))
4653 Decimal('-2')
4654 >>> ExtendedContext.logb(Decimal('0'))
4655 Decimal('-Infinity')
4657 Decimal('0')
4659 Decimal('1')
4661 Decimal('2')
4671 >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
4672 Decimal('0')
4673 >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1'))
4674 Decimal('0')
4675 >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0'))
4676 Decimal('0')
4677 >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1'))
4678 Decimal('1')
4679 >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010'))
4680 Decimal('1000')
4681 >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10'))
4682 Decimal('10')
4684 Decimal('100')
4685 >>> ExtendedContext.logical_and(Decimal(110), 1101)
4686 Decimal('100')
4687 >>> ExtendedContext.logical_and(110, Decimal(1101))
4688 Decimal('100')
4698 >>> ExtendedContext.logical_invert(Decimal('0'))
4699 Decimal('111111111')
4700 >>> ExtendedContext.logical_invert(Decimal('1'))
4701 Decimal('111111110')
4702 >>> ExtendedContext.logical_invert(Decimal('111111111'))
4703 Decimal('0')
4704 >>> ExtendedContext.logical_invert(Decimal('101010101'))
4705 Decimal('10101010')
4707 Decimal('111110010')
4717 >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
4718 Decimal('0')
4719 >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1'))
4720 Decimal('1')
4721 >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0'))
4722 Decimal('1')
4723 >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1'))
4724 Decimal('1')
4725 >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010'))
4726 Decimal('1110')
4727 >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10'))
4728 Decimal('1110')
4730 Decimal('1111')
4731 >>> ExtendedContext.logical_or(Decimal(110), 1101)
4732 Decimal('1111')
4733 >>> ExtendedContext.logical_or(110, Decimal(1101))
4734 Decimal('1111')
4744 >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
4745 Decimal('0')
4746 >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1'))
4747 Decimal('1')
4748 >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0'))
4749 Decimal('1')
4750 >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1'))
4751 Decimal('0')
4752 >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010'))
4753 Decimal('110')
4754 >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10'))
4755 Decimal('1101')
4757 Decimal('1011')
4758 >>> ExtendedContext.logical_xor(Decimal(110), 1101)
4759 Decimal('1011')
4760 >>> ExtendedContext.logical_xor(110, Decimal(1101))
4761 Decimal('1011')
4775 >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
4776 Decimal('3')
4777 >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
4778 Decimal('3')
4779 >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
4780 Decimal('1')
4781 >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
4782 Decimal('7')
4784 Decimal('2')
4785 >>> ExtendedContext.max(Decimal(1), 2)
4786 Decimal('2')
4787 >>> ExtendedContext.max(1, Decimal(2))
4788 Decimal('2')
4796 >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN'))
4797 Decimal('7')
4798 >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10'))
4799 Decimal('-10')
4801 Decimal('-2')
4802 >>> ExtendedContext.max_mag(Decimal(1), -2)
4803 Decimal('-2')
4804 >>> ExtendedContext.max_mag(1, Decimal(-2))
4805 Decimal('-2')
4819 >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
4820 Decimal('2')
4821 >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
4822 Decimal('-10')
4823 >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
4824 Decimal('1.0')
4825 >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
4826 Decimal('7')
4828 Decimal('1')
4829 >>> ExtendedContext.min(Decimal(1), 2)
4830 Decimal('1')
4831 >>> ExtendedContext.min(1, Decimal(29))
4832 Decimal('1')
4840 >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2'))
4841 Decimal('-2')
4842 >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN'))
4843 Decimal('-3')
4845 Decimal('1')
4846 >>> ExtendedContext.min_mag(Decimal(1), -2)
4847 Decimal('1')
4848 >>> ExtendedContext.min_mag(1, Decimal(-2))
4849 Decimal('1')
4861 >>> ExtendedContext.minus(Decimal('1.3'))
4862 Decimal('-1.3')
4863 >>> ExtendedContext.minus(Decimal('-1.3'))
4864 Decimal('1.3')
4866 Decimal('-1')
4879 >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
4880 Decimal('3.60')
4881 >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
4882 Decimal('21')
4883 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
4884 Decimal('0.72')
4885 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
4886 Decimal('-0.0')
4887 >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
4888 Decimal('4.28135971E+11')
4890 Decimal('49')
4891 >>> ExtendedContext.multiply(Decimal(7), 7)
4892 Decimal('49')
4893 >>> ExtendedContext.multiply(7, Decimal(7))
4894 Decimal('49')
4899 raise TypeError("Unable to convert %s to Decimal" % b)
4909 >>> ExtendedContext.next_minus(Decimal('1'))
4910 Decimal('0.999999999')
4911 >>> c.next_minus(Decimal('1E-1007'))
4912 Decimal('0E-1007')
4913 >>> ExtendedContext.next_minus(Decimal('-1.00000003'))
4914 Decimal('-1.00000004')
4915 >>> c.next_minus(Decimal('Infinity'))
4916 Decimal('9.99999999E+999')
4918 Decimal('0.999999999')
4929 >>> ExtendedContext.next_plus(Decimal('1'))
4930 Decimal('1.00000001')
4931 >>> c.next_plus(Decimal('-1E-1007'))
4932 Decimal('-0E-1007')
4933 >>> ExtendedContext.next_plus(Decimal('-1.00000003'))
4934 Decimal('-1.00000002')
4935 >>> c.next_plus(Decimal('-Infinity'))
4936 Decimal('-9.99999999E+999')
4938 Decimal('1.00000001')
4954 >>> c.next_toward(Decimal('1'), Decimal('2'))
4955 Decimal('1.00000001')
4956 >>> c.next_toward(Decimal('-1E-1007'), Decimal('1'))
4957 Decimal('-0E-1007')
4958 >>> c.next_toward(Decimal('-1.00000003'), Decimal('0'))
4959 Decimal('-1.00000002')
4960 >>> c.next_toward(Decimal('1'), Decimal('0'))
4961 Decimal('0.999999999')
4962 >>> c.next_toward(Decimal('1E-1007'), Decimal('-100'))
4963 Decimal('0E-1007')
4964 >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10'))
4965 Decimal('-1.00000004')
4966 >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000'))
4967 Decimal('-0.00')
4969 Decimal('1E-1007')
4970 >>> c.next_toward(Decimal(0), 1)
4971 Decimal('1E-1007')
4972 >>> c.next_toward(0, Decimal(1))
4973 Decimal('1E-1007')
4984 >>> ExtendedContext.normalize(Decimal('2.1'))
4985 Decimal('2.1')
4986 >>> ExtendedContext.normalize(Decimal('-2.0'))
4987 Decimal('-2')
4988 >>> ExtendedContext.normalize(Decimal('1.200'))
4989 Decimal('1.2')
4990 >>> ExtendedContext.normalize(Decimal('-120'))
4991 Decimal('-1.2E+2')
4992 >>> ExtendedContext.normalize(Decimal('120.00'))
4993 Decimal('1.2E+2')
4994 >>> ExtendedContext.normalize(Decimal('0.00'))
4995 Decimal('0')
4997 Decimal('6')
5020 >>> c.number_class(Decimal('Infinity'))
5022 >>> c.number_class(Decimal('1E-10'))
5024 >>> c.number_class(Decimal('2.50'))
5026 >>> c.number_class(Decimal('0.1E-999'))
5028 >>> c.number_class(Decimal('0'))
5030 >>> c.number_class(Decimal('-0'))
5032 >>> c.number_class(Decimal('-0.1E-999'))
5034 >>> c.number_class(Decimal('-1E-10'))
5036 >>> c.number_class(Decimal('-2.50'))
5038 >>> c.number_class(Decimal('-Infinity'))
5040 >>> c.number_class(Decimal('NaN'))
5042 >>> c.number_class(Decimal('-NaN'))
5044 >>> c.number_class(Decimal('sNaN'))
5059 >>> ExtendedContext.plus(Decimal('1.3'))
5060 Decimal('1.3')
5061 >>> ExtendedContext.plus(Decimal('-1.3'))
5062 Decimal('-1.3')
5064 Decimal('-1')
5094 >>> c.power(Decimal('2'), Decimal('3'))
5095 Decimal('8')
5096 >>> c.power(Decimal('-2'), Decimal('3'))
5097 Decimal('-8')
5098 >>> c.power(Decimal('2'), Decimal('-3'))
5099 Decimal('0.125')
5100 >>> c.power(Decimal('1.7'), Decimal('8'))
5101 Decimal('69.7575744')
5102 >>> c.power(Decimal('10'), Decimal('0.301029996'))
5103 Decimal('2.00000000')
5104 >>> c.power(Decimal('Infinity'), Decimal('-1'))
5105 Decimal('0')
5106 >>> c.power(Decimal('Infinity'), Decimal('0'))
5107 Decimal('1')
5108 >>> c.power(Decimal('Infinity'), Decimal('1'))
5109 Decimal('Infinity')
5110 >>> c.power(Decimal('-Infinity'), Decimal('-1'))
5111 Decimal('-0')
5112 >>> c.power(Decimal('-Infinity'), Decimal('0'))
5113 Decimal('1')
5114 >>> c.power(Decimal('-Infinity'), Decimal('1'))
5115 Decimal('-Infinity')
5116 >>> c.power(Decimal('-Infinity'), Decimal('2'))
5117 Decimal('Infinity')
5118 >>> c.power(Decimal('0'), Decimal('0'))
5119 Decimal('NaN')
5121 >>> c.power(Decimal('3'), Decimal('7'), Decimal('16'))
5122 Decimal('11')
5123 >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16'))
5124 Decimal('-11')
5125 >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16'))
5126 Decimal('1')
5127 >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16'))
5128 Decimal('11')
5129 >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789'))
5130 Decimal('11729830')
5131 >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729'))
5132 Decimal('-0')
5133 >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537'))
5134 Decimal('1')
5136 Decimal('823543')
5137 >>> ExtendedContext.power(Decimal(7), 7)
5138 Decimal('823543')
5139 >>> ExtendedContext.power(7, Decimal(7), 2)
5140 Decimal('1')
5145 raise TypeError("Unable to convert %s to Decimal" % b)
5167 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
5168 Decimal('2.170')
5169 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
5170 Decimal('2.17')
5171 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
5172 Decimal('2.2')
5173 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
5174 Decimal('2')
5175 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
5176 Decimal('0E+1')
5177 >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
5178 Decimal('-Infinity')
5179 >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
5180 Decimal('NaN')
5181 >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
5182 Decimal('-0')
5183 >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
5184 Decimal('-0E+5')
5185 >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
5186 Decimal('NaN')
5187 >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
5188 Decimal('NaN')
5189 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
5190 Decimal('217.0')
5191 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
5192 Decimal('217')
5193 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
5194 Decimal('2.2E+2')
5195 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
5196 Decimal('2E+2')
5198 Decimal('1')
5199 >>> ExtendedContext.quantize(Decimal(1), 2)
5200 Decimal('1')
5201 >>> ExtendedContext.quantize(1, Decimal(2))
5202 Decimal('1')
5208 """Just returns 10, as this is Decimal, :)
5211 Decimal('10')
5213 return Decimal(10)
5227 >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
5228 Decimal('2.1')
5229 >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
5230 Decimal('1')
5231 >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
5232 Decimal('-1')
5233 >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
5234 Decimal('0.2')
5235 >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
5236 Decimal('0.1')
5237 >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
5238 Decimal('1.0')
5240 Decimal('4')
5241 >>> ExtendedContext.remainder(Decimal(22), 6)
5242 Decimal('4')
5243 >>> ExtendedContext.remainder(22, Decimal(6))
5244 Decimal('4')
5249 raise TypeError("Unable to convert %s to Decimal" % b)
5263 >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
5264 Decimal('-0.9')
5265 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
5266 Decimal('-2')
5267 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
5268 Decimal('1')
5269 >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
5270 Decimal('-1')
5271 >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
5272 Decimal('0.2')
5273 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
5274 Decimal('0.1')
5275 >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
5276 Decimal('-0.3')
5278 Decimal('3')
5279 >>> ExtendedContext.remainder_near(Decimal(3), 11)
5280 Decimal('3')
5281 >>> ExtendedContext.remainder_near(3, Decimal(11))
5282 Decimal('3')
5296 >>> ExtendedContext.rotate(Decimal('34'), Decimal('8'))
5297 Decimal('400000003')
5298 >>> ExtendedContext.rotate(Decimal('12'), Decimal('9'))
5299 Decimal('12')
5300 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2'))
5301 Decimal('891234567')
5302 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0'))
5303 Decimal('123456789')
5304 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2'))
5305 Decimal('345678912')
5307 Decimal('13333330')
5308 >>> ExtendedContext.rotate(Decimal(1333333), 1)
5309 Decimal('13333330')
5310 >>> ExtendedContext.rotate(1333333, Decimal(1))
5311 Decimal('13333330')
5322 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
5324 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
5326 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
5328 >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
5332 >>> ExtendedContext.same_quantum(Decimal(10000), -1)
5334 >>> ExtendedContext.same_quantum(10000, Decimal(-1))
5343 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2'))
5344 Decimal('0.0750')
5345 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0'))
5346 Decimal('7.50')
5347 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3'))
5348 Decimal('7.50E+3')
5350 Decimal('1E+4')
5351 >>> ExtendedContext.scaleb(Decimal(1), 4)
5352 Decimal('1E+4')
5353 >>> ExtendedContext.scaleb(1, Decimal(4))
5354 Decimal('1E+4')
5369 >>> ExtendedContext.shift(Decimal('34'), Decimal('8'))
5370 Decimal('400000000')
5371 >>> ExtendedContext.shift(Decimal('12'), Decimal('9'))
5372 Decimal('0')
5373 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2'))
5374 Decimal('1234567')
5375 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0'))
5376 Decimal('123456789')
5377 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2'))
5378 Decimal('345678900')
5380 Decimal('888888800')
5381 >>> ExtendedContext.shift(Decimal(88888888), 2)
5382 Decimal('888888800')
5383 >>> ExtendedContext.shift(88888888, Decimal(2))
5384 Decimal('888888800')
5395 >>> ExtendedContext.sqrt(Decimal('0'))
5396 Decimal('0')
5397 >>> ExtendedContext.sqrt(Decimal('-0'))
5398 Decimal('-0')
5399 >>> ExtendedContext.sqrt(Decimal('0.39'))
5400 Decimal('0.624499800')
5401 >>> ExtendedContext.sqrt(Decimal('100'))
5402 Decimal('10')
5403 >>> ExtendedContext.sqrt(Decimal('1'))
5404 Decimal('1')
5405 >>> ExtendedContext.sqrt(Decimal('1.0'))
5406 Decimal('1.0')
5407 >>> ExtendedContext.sqrt(Decimal('1.00'))
5408 Decimal('1.0')
5409 >>> ExtendedContext.sqrt(Decimal('7'))
5410 Decimal('2.64575131')
5411 >>> ExtendedContext.sqrt(Decimal('10'))
5412 Decimal('3.16227766')
5414 Decimal('1.41421356')
5424 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
5425 Decimal('0.23')
5426 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
5427 Decimal('0.00')
5428 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
5429 Decimal('-0.77')
5431 Decimal('3')
5432 >>> ExtendedContext.subtract(Decimal(8), 5)
5433 Decimal('3')
5434 >>> ExtendedContext.subtract(8, Decimal(5))
5435 Decimal('3')
5440 raise TypeError("Unable to convert %s to Decimal" % b)
5448 can leave up to 3 digits to the left of the decimal place and may
5453 >>> ExtendedContext.to_eng_string(Decimal('123E+1'))
5455 >>> ExtendedContext.to_eng_string(Decimal('123E+3'))
5457 >>> ExtendedContext.to_eng_string(Decimal('123E-10'))
5459 >>> ExtendedContext.to_eng_string(Decimal('-123E-12'))
5461 >>> ExtendedContext.to_eng_string(Decimal('7E-7'))
5463 >>> ExtendedContext.to_eng_string(Decimal('7E+1'))
5465 >>> ExtendedContext.to_eng_string(Decimal('0E+1'))
5490 >>> ExtendedContext.to_integral_exact(Decimal('2.1'))
5491 Decimal('2')
5492 >>> ExtendedContext.to_integral_exact(Decimal('100'))
5493 Decimal('100')
5494 >>> ExtendedContext.to_integral_exact(Decimal('100.0'))
5495 Decimal('100')
5496 >>> ExtendedContext.to_integral_exact(Decimal('101.5'))
5497 Decimal('102')
5498 >>> ExtendedContext.to_integral_exact(Decimal('-101.5'))
5499 Decimal('-102')
5500 >>> ExtendedContext.to_integral_exact(Decimal('10E+5'))
5501 Decimal('1.0E+6')
5502 >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77'))
5503 Decimal('7.89E+77')
5504 >>> ExtendedContext.to_integral_exact(Decimal('-Inf'))
5505 Decimal('-Infinity')
5519 >>> ExtendedContext.to_integral_value(Decimal('2.1'))
5520 Decimal('2')
5521 >>> ExtendedContext.to_integral_value(Decimal('100'))
5522 Decimal('100')
5523 >>> ExtendedContext.to_integral_value(Decimal('100.0'))
5524 Decimal('100')
5525 >>> ExtendedContext.to_integral_value(Decimal('101.5'))
5526 Decimal('102')
5527 >>> ExtendedContext.to_integral_value(Decimal('-101.5'))
5528 Decimal('-102')
5529 >>> ExtendedContext.to_integral_value(Decimal('10E+5'))
5530 Decimal('1.0E+6')
5531 >>> ExtendedContext.to_integral_value(Decimal('7.89E+77'))
5532 Decimal('7.89E+77')
5533 >>> ExtendedContext.to_integral_value(Decimal('-Inf'))
5534 Decimal('-Infinity')
5553 elif isinstance(value, Decimal):
5781 Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__."""
5853 """Compute an approximation to exp(c*10**e), with p decimal places of
5906 # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point
5942 """Convert other to Decimal.
5949 if isinstance(other, Decimal):
5952 return Decimal(other)
5954 return Decimal.from_float(other)
5957 raise TypeError("Unable to convert %s to Decimal" % other)
5961 """Given a Decimal instance self and a Python object other, return
5962 a pair (s, o) of Decimal instances such that "s op o" is
5967 if isinstance(other, Decimal):
5979 return self, Decimal(other.numerator)
5993 return self, Decimal.from_float(other)
6041 # at least one decimal digit, possibly after the decimal point. The
6068 # The functions in this section have little to do with the Decimal
6072 # A format specifier for Decimal looks like:
6116 decimal_point: string to use for decimal point
6161 # determine thousands separator, grouping, and decimal separator, and
6283 intpart: string of digits that must appear before the decimal point
6289 insert separators (decimal separator and thousands separators)
6320 _Infinity = Decimal('Inf')
6321 _NegativeInfinity = Decimal('-Inf')
6322 _NaN = Decimal('NaN')
6323 _Zero = Decimal(0)
6324 _One = Decimal(1)
6325 _NegativeOne = Decimal(-1)