• Home
  • Raw
  • Download

Lines Matching full:decimal

17 This is an implementation of decimal floating point arithmetic based on
18 the General Decimal Arithmetic Specification:
20 http://speleotrove.com/decimal/decarith.html
26 Decimal floating point has finite precision with arbitrarily large bounds.
34 of 0.0; Decimal('1.00') % Decimal('0.1') returns the expected
35 Decimal('0.00')).
37 Here are some examples of using the decimal module:
39 >>> from decimal import *
41 >>> Decimal(0)
42 Decimal('0')
43 >>> Decimal('1')
44 Decimal('1')
45 >>> Decimal('-.0123')
46 Decimal('-0.0123')
47 >>> Decimal(123456)
48 Decimal('123456')
49 >>> Decimal('123.45e12345678')
50 Decimal('1.2345E+12345680')
51 >>> Decimal('1.33') + Decimal('1.27')
52 Decimal('2.60')
53 >>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41')
54 Decimal('-2.20')
55 >>> dig = Decimal(1)
56 >>> print(dig / Decimal(3))
59 >>> print(dig / Decimal(3))
63 >>> print(Decimal(3).sqrt())
65 >>> print(Decimal(3) ** 123)
67 >>> inf = Decimal(1) / Decimal(0)
70 >>> neginf = Decimal(-1) / Decimal(0)
85 decimal.DivisionByZero: x / 0
90 >>> c.divide(Decimal(0), Decimal(0))
91 Decimal('NaN')
98 >>> print(c.divide(Decimal(0), Decimal(0)))
103 decimal.InvalidOperation: 0 / 0
108 >>> print(c.divide(Decimal(0), Decimal(0)))
117 'Decimal', 'Context',
151 __name__ = 'decimal' # For pickling
153 # See http://speleotrove.com/decimal/
410 permitted in the Decimal() constructor, context.create_decimal() and
414 Decimal.from_float() or context.create_decimal_from_float() do not
471 The returned context manager creates a local decimal context
484 # General Decimal Arithmetic Specification
507 ##### Decimal class #######################################################
509 # Do not subclass Decimal from numbers.Real and do not register it as such
513 class Decimal(object): class
514 """Floating point class for decimal arithmetic."""
517 # Generally, the value of the Decimal instance is given by
523 """Create a decimal point instance.
525 >>> Decimal('3.14') # string input
526 Decimal('3.14')
527 >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
528 Decimal('3.14')
529 >>> Decimal(314) # int
530 Decimal('314')
531 >>> Decimal(Decimal(314)) # another decimal instance
532 Decimal('314')
533 >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
534 Decimal('3.14')
542 # and the Decimal constructor still deal with tuples of
555 "Invalid literal for Decimal: %r" % value)
596 # From another decimal
597 if isinstance(value, Decimal):
615 raise ValueError('Invalid tuple size in creation of Decimal '
663 value = Decimal.from_float(value)
670 raise TypeError("Cannot convert %r to Decimal" % value)
674 """Converts a float to a decimal number, exactly.
676 Note that Decimal.from_float(0.1) is not the same as Decimal('0.1').
679 0x1.999999999999ap-4. The exact equivalent of the value in decimal
682 >>> Decimal.from_float(0.1)
683 Decimal('0.1000000000000000055511151231257827021181583404541015625')
684 >>> Decimal.from_float(float('nan'))
685 Decimal('NaN')
686 >>> Decimal.from_float(float('inf'))
687 Decimal('Infinity')
688 >>> Decimal.from_float(-float('inf'))
689 Decimal('-Infinity')
690 >>> Decimal.from_float(-0.0)
691 Decimal('-0')
712 if cls is Decimal:
818 """Compare the two non-NaN decimal instances self and other.
833 # check for zeros; Decimal('0') == Decimal('-0')
864 # Note: The Decimal standard doesn't cover rich comparisons for
926 """Compare self to other. Return a decimal value:
928 a or b is a NaN ==> Decimal('NaN')
929 a < b ==> Decimal('-1')
930 a == b ==> Decimal('0')
931 a > b ==> Decimal('1')
941 return Decimal(self._cmp(other))
946 # In order to make sure that the hash of a Decimal instance
977 """Express a finite Decimal instance in the form n / d.
982 >>> Decimal('3.14').as_integer_ratio()
984 >>> Decimal('-123e5').as_integer_ratio()
986 >>> Decimal('0.00').as_integer_ratio()
1027 """Represents the number as an instance of Decimal."""
1029 return "Decimal('%s')" % str(self)
1046 # number of digits of self._int to left of decimal point
1050 # decimal point in the mantissa of the output string (that is,
1087 can leave up to 3 digits to the left of the decimal place and may
1106 # -Decimal('0') is Decimal('0'), not Decimal('-0'), except
1131 ans = Decimal(self)
1178 return Decimal(self)
1180 return Decimal(other) # Can't both be infinity here
1239 ans = Decimal(result)
1529 ans = Decimal(self)
1641 return Decimal(0)
1659 return Decimal(self)
1667 self - Decimal instance
1677 return Decimal(self)
1690 return Decimal(self)
1751 return Decimal(self)
1754 # self is a finite, nonzero Decimal
1833 If only one argument is supplied, round a finite Decimal
1839 >>> round(Decimal('123.456'))
1841 >>> round(Decimal('-456.789'))
1843 >>> round(Decimal('-3.0'))
1845 >>> round(Decimal('2.5'))
1847 >>> round(Decimal('3.5'))
1849 >>> round(Decimal('Inf'))
1853 >>> round(Decimal('NaN'))
1859 decimal places using the rounding mode for the current
1863 self.quantize(Decimal('1En')).
1865 >>> round(Decimal('123.456'), 0)
1866 Decimal('123')
1867 >>> round(Decimal('123.456'), 2)
1868 Decimal('123.46')
1869 >>> round(Decimal('123.456'), -2)
1870 Decimal('1E+2')
1871 >>> round(Decimal('-Infinity'), 37)
1872 Decimal('NaN')
1873 >>> round(Decimal('sNaN123'), 0)
1874 Decimal('NaN123')
1895 For a finite Decimal instance self, return the greatest
1910 For a finite Decimal instance self, return the least integer n
2015 # additional restriction for decimal: the modulus must be less
2038 # Decimal integers (i.e. force their exponents to be >= 0)
2555 return Decimal(self) # if both are inf, it is OK
2624 return Decimal(self)
2647 """Round a nonzero, nonspecial Decimal to a fixed number of
2659 return Decimal(self)
2683 return Decimal(self)
2685 return Decimal(self)
2708 return Decimal(self)
2710 return Decimal(self)
2728 return Decimal(self)
2743 # <= sqrt(c) < 10**p, so the closest representable Decimal at
2916 """Returns the same Decimal object.
3057 return Decimal(self)
3114 Currently, the encoding of a Decimal instance is always
3115 canonical, so this method returns True for any Decimal.
3122 A Decimal instance is considered finite if it is neither
3299 ans = Decimal(self._exp + len(self._int) - 1)
3348 # Decimal. Note that no attempt is made to fit the result
3350 ans = Decimal(self.adjusted())
3633 """Just returns 10, as this is Decimal, :)"""
3634 return Decimal(10)
3653 return Decimal(self)
3688 return Decimal(self)
3711 return Decimal(self)
3737 if type(self) is Decimal:
3742 if type(self) is Decimal:
3749 """Format a Decimal instance according to the given specifier.
3759 # there should be at least one digit after the decimal point.
3761 # Decimal---it's presumably there to make sure that
3799 # figure out placement of the decimal point
3814 # find digits before and after decimal point, and get exponent
3826 # done with the decimal-specific stuff; hand over the rest
3831 """Create a decimal instance directly, without any validation,
3838 self = object.__new__(Decimal)
3846 # Register Decimal as a kind of Number (an abstract base class).
3849 _numbers.Number.register(Decimal)
3858 the previous decimal context in __exit__()
3870 """Contains the context for a Decimal instance.
3880 Should be reset by user of Decimal instance.
3972 "'decimal.Context' object has no attribute '%s'" % name)
4095 """Creates a new Decimal instance but using self as context.
4098 IBM Decimal specification."""
4105 d = Decimal(num, context=self)
4112 """Creates a new Decimal instance from a float but rounding using self
4117 Decimal('3.1415')
4122 decimal.Inexact: None
4125 d = Decimal.from_float(f) # An exact conversion
4136 >>> ExtendedContext.abs(Decimal('2.1'))
4137 Decimal('2.1')
4138 >>> ExtendedContext.abs(Decimal('-100'))
4139 Decimal('100')
4140 >>> ExtendedContext.abs(Decimal('101.5'))
4141 Decimal('101.5')
4142 >>> ExtendedContext.abs(Decimal('-101.5'))
4143 Decimal('101.5')
4145 Decimal('1')
4153 >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
4154 Decimal('19.00')
4155 >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
4156 Decimal('1.02E+4')
4157 >>> ExtendedContext.add(1, Decimal(2))
4158 Decimal('3')
4159 >>> ExtendedContext.add(Decimal(8), 5)
4160 Decimal('13')
4162 Decimal('10')
4167 raise TypeError("Unable to convert %s to Decimal" % b)
4175 """Returns the same Decimal object.
4180 >>> ExtendedContext.canonical(Decimal('2.50'))
4181 Decimal('2.50')
4183 if not isinstance(a, Decimal):
4184 raise TypeError("canonical requires a Decimal as an argument.")
4201 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
4202 Decimal('-1')
4203 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
4204 Decimal('0')
4205 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
4206 Decimal('0')
4207 >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
4208 Decimal('1')
4209 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
4210 Decimal('1')
4211 >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
4212 Decimal('-1')
4214 Decimal('-1')
4215 >>> ExtendedContext.compare(Decimal(1), 2)
4216 Decimal('-1')
4217 >>> ExtendedContext.compare(1, Decimal(2))
4218 Decimal('-1')
4230 >>> c.compare_signal(Decimal('2.1'), Decimal('3'))
4231 Decimal('-1')
4232 >>> c.compare_signal(Decimal('2.1'), Decimal('2.1'))
4233 Decimal('0')
4237 >>> c.compare_signal(Decimal('NaN'), Decimal('2.1'))
4238 Decimal('NaN')
4244 >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1'))
4245 Decimal('NaN')
4249 Decimal('-1')
4250 >>> c.compare_signal(Decimal(-1), 2)
4251 Decimal('-1')
4252 >>> c.compare_signal(-1, Decimal(2))
4253 Decimal('-1')
4265 >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9'))
4266 Decimal('-1')
4267 >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12'))
4268 Decimal('-1')
4269 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3'))
4270 Decimal('-1')
4271 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30'))
4272 Decimal('0')
4273 >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300'))
4274 Decimal('1')
4275 >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN'))
4276 Decimal('-1')
4278 Decimal('-1')
4279 >>> ExtendedContext.compare_total(Decimal(1), 2)
4280 Decimal('-1')
4281 >>> ExtendedContext.compare_total(1, Decimal(2))
4282 Decimal('-1')
4298 >>> ExtendedContext.copy_abs(Decimal('2.1'))
4299 Decimal('2.1')
4300 >>> ExtendedContext.copy_abs(Decimal('-100'))
4301 Decimal('100')
4303 Decimal('1')
4309 """Returns a copy of the decimal object.
4311 >>> ExtendedContext.copy_decimal(Decimal('2.1'))
4312 Decimal('2.1')
4313 >>> ExtendedContext.copy_decimal(Decimal('-1.00'))
4314 Decimal('-1.00')
4316 Decimal('1')
4319 return Decimal(a)
4324 >>> ExtendedContext.copy_negate(Decimal('101.5'))
4325 Decimal('-101.5')
4326 >>> ExtendedContext.copy_negate(Decimal('-101.5'))
4327 Decimal('101.5')
4329 Decimal('-1')
4340 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33'))
4341 Decimal('1.50')
4342 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33'))
4343 Decimal('1.50')
4344 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33'))
4345 Decimal('-1.50')
4346 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33'))
4347 Decimal('-1.50')
4349 Decimal('-1')
4350 >>> ExtendedContext.copy_sign(Decimal(1), -2)
4351 Decimal('-1')
4352 >>> ExtendedContext.copy_sign(1, Decimal(-2))
4353 Decimal('-1')
4359 """Decimal division in a specified context.
4361 >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
4362 Decimal('0.333333333')
4363 >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
4364 Decimal('0.666666667')
4365 >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
4366 Decimal('2.5')
4367 >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
4368 Decimal('0.1')
4369 >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
4370 Decimal('1')
4371 >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
4372 Decimal('4.00')
4373 >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
4374 Decimal('1.20')
4375 >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
4376 Decimal('10')
4377 >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
4378 Decimal('1000')
4379 >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
4380 Decimal('1.20E+6')
4382 Decimal('1')
4383 >>> ExtendedContext.divide(Decimal(5), 5)
4384 Decimal('1')
4385 >>> ExtendedContext.divide(5, Decimal(5))
4386 Decimal('1')
4391 raise TypeError("Unable to convert %s to Decimal" % b)
4398 >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
4399 Decimal('0')
4400 >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
4401 Decimal('3')
4402 >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
4403 Decimal('3')
4405 Decimal('3')
4406 >>> ExtendedContext.divide_int(Decimal(10), 3)
4407 Decimal('3')
4408 >>> ExtendedContext.divide_int(10, Decimal(3))
4409 Decimal('3')
4414 raise TypeError("Unable to convert %s to Decimal" % b)
4421 >>> ExtendedContext.divmod(Decimal(8), Decimal(3))
4422 (Decimal('2'), Decimal('2'))
4423 >>> ExtendedContext.divmod(Decimal(8), Decimal(4))
4424 (Decimal('2'), Decimal('0'))
4426 (Decimal('2'), Decimal('0'))
4427 >>> ExtendedContext.divmod(Decimal(8), 4)
4428 (Decimal('2'), Decimal('0'))
4429 >>> ExtendedContext.divmod(8, Decimal(4))
4430 (Decimal('2'), Decimal('0'))
4435 raise TypeError("Unable to convert %s to Decimal" % b)
4445 >>> c.exp(Decimal('-Infinity'))
4446 Decimal('0')
4447 >>> c.exp(Decimal('-1'))
4448 Decimal('0.367879441')
4449 >>> c.exp(Decimal('0'))
4450 Decimal('1')
4451 >>> c.exp(Decimal('1'))
4452 Decimal('2.71828183')
4453 >>> c.exp(Decimal('0.693147181'))
4454 Decimal('2.00000000')
4455 >>> c.exp(Decimal('+Infinity'))
4456 Decimal('Infinity')
4458 Decimal('22026.4658')
4470 >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7'))
4471 Decimal('22')
4472 >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7'))
4473 Decimal('-8')
4474 >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578'))
4475 Decimal('1.38435736E+12')
4477 Decimal('7')
4478 >>> ExtendedContext.fma(1, Decimal(3), 4)
4479 Decimal('7')
4480 >>> ExtendedContext.fma(1, 3, Decimal(4))
4481 Decimal('7')
4489 Currently, the encoding of a Decimal instance is always
4490 canonical, so this method returns True for any Decimal.
4492 >>> ExtendedContext.is_canonical(Decimal('2.50'))
4495 if not isinstance(a, Decimal):
4496 raise TypeError("is_canonical requires a Decimal as an argument.")
4502 A Decimal instance is considered finite if it is neither
4505 >>> ExtendedContext.is_finite(Decimal('2.50'))
4507 >>> ExtendedContext.is_finite(Decimal('-0.3'))
4509 >>> ExtendedContext.is_finite(Decimal('0'))
4511 >>> ExtendedContext.is_finite(Decimal('Inf'))
4513 >>> ExtendedContext.is_finite(Decimal('NaN'))
4524 >>> ExtendedContext.is_infinite(Decimal('2.50'))
4526 >>> ExtendedContext.is_infinite(Decimal('-Inf'))
4528 >>> ExtendedContext.is_infinite(Decimal('NaN'))
4540 >>> ExtendedContext.is_nan(Decimal('2.50'))
4542 >>> ExtendedContext.is_nan(Decimal('NaN'))
4544 >>> ExtendedContext.is_nan(Decimal('-sNaN'))
4559 >>> c.is_normal(Decimal('2.50'))
4561 >>> c.is_normal(Decimal('0.1E-999'))
4563 >>> c.is_normal(Decimal('0.00'))
4565 >>> c.is_normal(Decimal('-Inf'))
4567 >>> c.is_normal(Decimal('NaN'))
4578 >>> ExtendedContext.is_qnan(Decimal('2.50'))
4580 >>> ExtendedContext.is_qnan(Decimal('NaN'))
4582 >>> ExtendedContext.is_qnan(Decimal('sNaN'))
4593 >>> ExtendedContext.is_signed(Decimal('2.50'))
4595 >>> ExtendedContext.is_signed(Decimal('-12'))
4597 >>> ExtendedContext.is_signed(Decimal('-0'))
4611 >>> ExtendedContext.is_snan(Decimal('2.50'))
4613 >>> ExtendedContext.is_snan(Decimal('NaN'))
4615 >>> ExtendedContext.is_snan(Decimal('sNaN'))
4629 >>> c.is_subnormal(Decimal('2.50'))
4631 >>> c.is_subnormal(Decimal('0.1E-999'))
4633 >>> c.is_subnormal(Decimal('0.00'))
4635 >>> c.is_subnormal(Decimal('-Inf'))
4637 >>> c.is_subnormal(Decimal('NaN'))
4648 >>> ExtendedContext.is_zero(Decimal('0'))
4650 >>> ExtendedContext.is_zero(Decimal('2.50'))
4652 >>> ExtendedContext.is_zero(Decimal('-0E+2'))
4668 >>> c.ln(Decimal('0'))
4669 Decimal('-Infinity')
4670 >>> c.ln(Decimal('1.000'))
4671 Decimal('0')
4672 >>> c.ln(Decimal('2.71828183'))
4673 Decimal('1.00000000')
4674 >>> c.ln(Decimal('10'))
4675 Decimal('2.30258509')
4676 >>> c.ln(Decimal('+Infinity'))
4677 Decimal('Infinity')
4679 Decimal('0')
4690 >>> c.log10(Decimal('0'))
4691 Decimal('-Infinity')
4692 >>> c.log10(Decimal('0.001'))
4693 Decimal('-3')
4694 >>> c.log10(Decimal('1.000'))
4695 Decimal('0')
4696 >>> c.log10(Decimal('2'))
4697 Decimal('0.301029996')
4698 >>> c.log10(Decimal('10'))
4699 Decimal('1')
4700 >>> c.log10(Decimal('70'))
4701 Decimal('1.84509804')
4702 >>> c.log10(Decimal('+Infinity'))
4703 Decimal('Infinity')
4705 Decimal('-Infinity')
4707 Decimal('0')
4720 >>> ExtendedContext.logb(Decimal('250'))
4721 Decimal('2')
4722 >>> ExtendedContext.logb(Decimal('2.50'))
4723 Decimal('0')
4724 >>> ExtendedContext.logb(Decimal('0.03'))
4725 Decimal('-2')
4726 >>> ExtendedContext.logb(Decimal('0'))
4727 Decimal('-Infinity')
4729 Decimal('0')
4731 Decimal('1')
4733 Decimal('2')
4743 >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
4744 Decimal('0')
4745 >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1'))
4746 Decimal('0')
4747 >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0'))
4748 Decimal('0')
4749 >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1'))
4750 Decimal('1')
4751 >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010'))
4752 Decimal('1000')
4753 >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10'))
4754 Decimal('10')
4756 Decimal('100')
4757 >>> ExtendedContext.logical_and(Decimal(110), 1101)
4758 Decimal('100')
4759 >>> ExtendedContext.logical_and(110, Decimal(1101))
4760 Decimal('100')
4770 >>> ExtendedContext.logical_invert(Decimal('0'))
4771 Decimal('111111111')
4772 >>> ExtendedContext.logical_invert(Decimal('1'))
4773 Decimal('111111110')
4774 >>> ExtendedContext.logical_invert(Decimal('111111111'))
4775 Decimal('0')
4776 >>> ExtendedContext.logical_invert(Decimal('101010101'))
4777 Decimal('10101010')
4779 Decimal('111110010')
4789 >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
4790 Decimal('0')
4791 >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1'))
4792 Decimal('1')
4793 >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0'))
4794 Decimal('1')
4795 >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1'))
4796 Decimal('1')
4797 >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010'))
4798 Decimal('1110')
4799 >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10'))
4800 Decimal('1110')
4802 Decimal('1111')
4803 >>> ExtendedContext.logical_or(Decimal(110), 1101)
4804 Decimal('1111')
4805 >>> ExtendedContext.logical_or(110, Decimal(1101))
4806 Decimal('1111')
4816 >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
4817 Decimal('0')
4818 >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1'))
4819 Decimal('1')
4820 >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0'))
4821 Decimal('1')
4822 >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1'))
4823 Decimal('0')
4824 >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010'))
4825 Decimal('110')
4826 >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10'))
4827 Decimal('1101')
4829 Decimal('1011')
4830 >>> ExtendedContext.logical_xor(Decimal(110), 1101)
4831 Decimal('1011')
4832 >>> ExtendedContext.logical_xor(110, Decimal(1101))
4833 Decimal('1011')
4847 >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
4848 Decimal('3')
4849 >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
4850 Decimal('3')
4851 >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
4852 Decimal('1')
4853 >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
4854 Decimal('7')
4856 Decimal('2')
4857 >>> ExtendedContext.max(Decimal(1), 2)
4858 Decimal('2')
4859 >>> ExtendedContext.max(1, Decimal(2))
4860 Decimal('2')
4868 >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN'))
4869 Decimal('7')
4870 >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10'))
4871 Decimal('-10')
4873 Decimal('-2')
4874 >>> ExtendedContext.max_mag(Decimal(1), -2)
4875 Decimal('-2')
4876 >>> ExtendedContext.max_mag(1, Decimal(-2))
4877 Decimal('-2')
4891 >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
4892 Decimal('2')
4893 >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
4894 Decimal('-10')
4895 >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
4896 Decimal('1.0')
4897 >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
4898 Decimal('7')
4900 Decimal('1')
4901 >>> ExtendedContext.min(Decimal(1), 2)
4902 Decimal('1')
4903 >>> ExtendedContext.min(1, Decimal(29))
4904 Decimal('1')
4912 >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2'))
4913 Decimal('-2')
4914 >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN'))
4915 Decimal('-3')
4917 Decimal('1')
4918 >>> ExtendedContext.min_mag(Decimal(1), -2)
4919 Decimal('1')
4920 >>> ExtendedContext.min_mag(1, Decimal(-2))
4921 Decimal('1')
4933 >>> ExtendedContext.minus(Decimal('1.3'))
4934 Decimal('-1.3')
4935 >>> ExtendedContext.minus(Decimal('-1.3'))
4936 Decimal('1.3')
4938 Decimal('-1')
4951 >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
4952 Decimal('3.60')
4953 >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
4954 Decimal('21')
4955 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
4956 Decimal('0.72')
4957 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
4958 Decimal('-0.0')
4959 >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
4960 Decimal('4.28135971E+11')
4962 Decimal('49')
4963 >>> ExtendedContext.multiply(Decimal(7), 7)
4964 Decimal('49')
4965 >>> ExtendedContext.multiply(7, Decimal(7))
4966 Decimal('49')
4971 raise TypeError("Unable to convert %s to Decimal" % b)
4981 >>> ExtendedContext.next_minus(Decimal('1'))
4982 Decimal('0.999999999')
4983 >>> c.next_minus(Decimal('1E-1007'))
4984 Decimal('0E-1007')
4985 >>> ExtendedContext.next_minus(Decimal('-1.00000003'))
4986 Decimal('-1.00000004')
4987 >>> c.next_minus(Decimal('Infinity'))
4988 Decimal('9.99999999E+999')
4990 Decimal('0.999999999')
5001 >>> ExtendedContext.next_plus(Decimal('1'))
5002 Decimal('1.00000001')
5003 >>> c.next_plus(Decimal('-1E-1007'))
5004 Decimal('-0E-1007')
5005 >>> ExtendedContext.next_plus(Decimal('-1.00000003'))
5006 Decimal('-1.00000002')
5007 >>> c.next_plus(Decimal('-Infinity'))
5008 Decimal('-9.99999999E+999')
5010 Decimal('1.00000001')
5026 >>> c.next_toward(Decimal('1'), Decimal('2'))
5027 Decimal('1.00000001')
5028 >>> c.next_toward(Decimal('-1E-1007'), Decimal('1'))
5029 Decimal('-0E-1007')
5030 >>> c.next_toward(Decimal('-1.00000003'), Decimal('0'))
5031 Decimal('-1.00000002')
5032 >>> c.next_toward(Decimal('1'), Decimal('0'))
5033 Decimal('0.999999999')
5034 >>> c.next_toward(Decimal('1E-1007'), Decimal('-100'))
5035 Decimal('0E-1007')
5036 >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10'))
5037 Decimal('-1.00000004')
5038 >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000'))
5039 Decimal('-0.00')
5041 Decimal('1E-1007')
5042 >>> c.next_toward(Decimal(0), 1)
5043 Decimal('1E-1007')
5044 >>> c.next_toward(0, Decimal(1))
5045 Decimal('1E-1007')
5056 >>> ExtendedContext.normalize(Decimal('2.1'))
5057 Decimal('2.1')
5058 >>> ExtendedContext.normalize(Decimal('-2.0'))
5059 Decimal('-2')
5060 >>> ExtendedContext.normalize(Decimal('1.200'))
5061 Decimal('1.2')
5062 >>> ExtendedContext.normalize(Decimal('-120'))
5063 Decimal('-1.2E+2')
5064 >>> ExtendedContext.normalize(Decimal('120.00'))
5065 Decimal('1.2E+2')
5066 >>> ExtendedContext.normalize(Decimal('0.00'))
5067 Decimal('0')
5069 Decimal('6')
5092 >>> c.number_class(Decimal('Infinity'))
5094 >>> c.number_class(Decimal('1E-10'))
5096 >>> c.number_class(Decimal('2.50'))
5098 >>> c.number_class(Decimal('0.1E-999'))
5100 >>> c.number_class(Decimal('0'))
5102 >>> c.number_class(Decimal('-0'))
5104 >>> c.number_class(Decimal('-0.1E-999'))
5106 >>> c.number_class(Decimal('-1E-10'))
5108 >>> c.number_class(Decimal('-2.50'))
5110 >>> c.number_class(Decimal('-Infinity'))
5112 >>> c.number_class(Decimal('NaN'))
5114 >>> c.number_class(Decimal('-NaN'))
5116 >>> c.number_class(Decimal('sNaN'))
5131 >>> ExtendedContext.plus(Decimal('1.3'))
5132 Decimal('1.3')
5133 >>> ExtendedContext.plus(Decimal('-1.3'))
5134 Decimal('-1.3')
5136 Decimal('-1')
5166 >>> c.power(Decimal('2'), Decimal('3'))
5167 Decimal('8')
5168 >>> c.power(Decimal('-2'), Decimal('3'))
5169 Decimal('-8')
5170 >>> c.power(Decimal('2'), Decimal('-3'))
5171 Decimal('0.125')
5172 >>> c.power(Decimal('1.7'), Decimal('8'))
5173 Decimal('69.7575744')
5174 >>> c.power(Decimal('10'), Decimal('0.301029996'))
5175 Decimal('2.00000000')
5176 >>> c.power(Decimal('Infinity'), Decimal('-1'))
5177 Decimal('0')
5178 >>> c.power(Decimal('Infinity'), Decimal('0'))
5179 Decimal('1')
5180 >>> c.power(Decimal('Infinity'), Decimal('1'))
5181 Decimal('Infinity')
5182 >>> c.power(Decimal('-Infinity'), Decimal('-1'))
5183 Decimal('-0')
5184 >>> c.power(Decimal('-Infinity'), Decimal('0'))
5185 Decimal('1')
5186 >>> c.power(Decimal('-Infinity'), Decimal('1'))
5187 Decimal('-Infinity')
5188 >>> c.power(Decimal('-Infinity'), Decimal('2'))
5189 Decimal('Infinity')
5190 >>> c.power(Decimal('0'), Decimal('0'))
5191 Decimal('NaN')
5193 >>> c.power(Decimal('3'), Decimal('7'), Decimal('16'))
5194 Decimal('11')
5195 >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16'))
5196 Decimal('-11')
5197 >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16'))
5198 Decimal('1')
5199 >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16'))
5200 Decimal('11')
5201 >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789'))
5202 Decimal('11729830')
5203 >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729'))
5204 Decimal('-0')
5205 >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537'))
5206 Decimal('1')
5208 Decimal('823543')
5209 >>> ExtendedContext.power(Decimal(7), 7)
5210 Decimal('823543')
5211 >>> ExtendedContext.power(7, Decimal(7), 2)
5212 Decimal('1')
5217 raise TypeError("Unable to convert %s to Decimal" % b)
5239 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
5240 Decimal('2.170')
5241 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
5242 Decimal('2.17')
5243 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
5244 Decimal('2.2')
5245 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
5246 Decimal('2')
5247 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
5248 Decimal('0E+1')
5249 >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
5250 Decimal('-Infinity')
5251 >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
5252 Decimal('NaN')
5253 >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
5254 Decimal('-0')
5255 >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
5256 Decimal('-0E+5')
5257 >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
5258 Decimal('NaN')
5259 >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
5260 Decimal('NaN')
5261 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
5262 Decimal('217.0')
5263 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
5264 Decimal('217')
5265 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
5266 Decimal('2.2E+2')
5267 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
5268 Decimal('2E+2')
5270 Decimal('1')
5271 >>> ExtendedContext.quantize(Decimal(1), 2)
5272 Decimal('1')
5273 >>> ExtendedContext.quantize(1, Decimal(2))
5274 Decimal('1')
5280 """Just returns 10, as this is Decimal, :)
5283 Decimal('10')
5285 return Decimal(10)
5299 >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
5300 Decimal('2.1')
5301 >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
5302 Decimal('1')
5303 >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
5304 Decimal('-1')
5305 >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
5306 Decimal('0.2')
5307 >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
5308 Decimal('0.1')
5309 >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
5310 Decimal('1.0')
5312 Decimal('4')
5313 >>> ExtendedContext.remainder(Decimal(22), 6)
5314 Decimal('4')
5315 >>> ExtendedContext.remainder(22, Decimal(6))
5316 Decimal('4')
5321 raise TypeError("Unable to convert %s to Decimal" % b)
5335 >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
5336 Decimal('-0.9')
5337 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
5338 Decimal('-2')
5339 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
5340 Decimal('1')
5341 >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
5342 Decimal('-1')
5343 >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
5344 Decimal('0.2')
5345 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
5346 Decimal('0.1')
5347 >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
5348 Decimal('-0.3')
5350 Decimal('3')
5351 >>> ExtendedContext.remainder_near(Decimal(3), 11)
5352 Decimal('3')
5353 >>> ExtendedContext.remainder_near(3, Decimal(11))
5354 Decimal('3')
5368 >>> ExtendedContext.rotate(Decimal('34'), Decimal('8'))
5369 Decimal('400000003')
5370 >>> ExtendedContext.rotate(Decimal('12'), Decimal('9'))
5371 Decimal('12')
5372 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2'))
5373 Decimal('891234567')
5374 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0'))
5375 Decimal('123456789')
5376 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2'))
5377 Decimal('345678912')
5379 Decimal('13333330')
5380 >>> ExtendedContext.rotate(Decimal(1333333), 1)
5381 Decimal('13333330')
5382 >>> ExtendedContext.rotate(1333333, Decimal(1))
5383 Decimal('13333330')
5394 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
5396 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
5398 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
5400 >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
5404 >>> ExtendedContext.same_quantum(Decimal(10000), -1)
5406 >>> ExtendedContext.same_quantum(10000, Decimal(-1))
5415 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2'))
5416 Decimal('0.0750')
5417 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0'))
5418 Decimal('7.50')
5419 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3'))
5420 Decimal('7.50E+3')
5422 Decimal('1E+4')
5423 >>> ExtendedContext.scaleb(Decimal(1), 4)
5424 Decimal('1E+4')
5425 >>> ExtendedContext.scaleb(1, Decimal(4))
5426 Decimal('1E+4')
5441 >>> ExtendedContext.shift(Decimal('34'), Decimal('8'))
5442 Decimal('400000000')
5443 >>> ExtendedContext.shift(Decimal('12'), Decimal('9'))
5444 Decimal('0')
5445 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2'))
5446 Decimal('1234567')
5447 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0'))
5448 Decimal('123456789')
5449 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2'))
5450 Decimal('345678900')
5452 Decimal('888888800')
5453 >>> ExtendedContext.shift(Decimal(88888888), 2)
5454 Decimal('888888800')
5455 >>> ExtendedContext.shift(88888888, Decimal(2))
5456 Decimal('888888800')
5467 >>> ExtendedContext.sqrt(Decimal('0'))
5468 Decimal('0')
5469 >>> ExtendedContext.sqrt(Decimal('-0'))
5470 Decimal('-0')
5471 >>> ExtendedContext.sqrt(Decimal('0.39'))
5472 Decimal('0.624499800')
5473 >>> ExtendedContext.sqrt(Decimal('100'))
5474 Decimal('10')
5475 >>> ExtendedContext.sqrt(Decimal('1'))
5476 Decimal('1')
5477 >>> ExtendedContext.sqrt(Decimal('1.0'))
5478 Decimal('1.0')
5479 >>> ExtendedContext.sqrt(Decimal('1.00'))
5480 Decimal('1.0')
5481 >>> ExtendedContext.sqrt(Decimal('7'))
5482 Decimal('2.64575131')
5483 >>> ExtendedContext.sqrt(Decimal('10'))
5484 Decimal('3.16227766')
5486 Decimal('1.41421356')
5496 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
5497 Decimal('0.23')
5498 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
5499 Decimal('0.00')
5500 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
5501 Decimal('-0.77')
5503 Decimal('3')
5504 >>> ExtendedContext.subtract(Decimal(8), 5)
5505 Decimal('3')
5506 >>> ExtendedContext.subtract(8, Decimal(5))
5507 Decimal('3')
5512 raise TypeError("Unable to convert %s to Decimal" % b)
5520 can leave up to 3 digits to the left of the decimal place and may
5525 >>> ExtendedContext.to_eng_string(Decimal('123E+1'))
5527 >>> ExtendedContext.to_eng_string(Decimal('123E+3'))
5529 >>> ExtendedContext.to_eng_string(Decimal('123E-10'))
5531 >>> ExtendedContext.to_eng_string(Decimal('-123E-12'))
5533 >>> ExtendedContext.to_eng_string(Decimal('7E-7'))
5535 >>> ExtendedContext.to_eng_string(Decimal('7E+1'))
5537 >>> ExtendedContext.to_eng_string(Decimal('0E+1'))
5562 >>> ExtendedContext.to_integral_exact(Decimal('2.1'))
5563 Decimal('2')
5564 >>> ExtendedContext.to_integral_exact(Decimal('100'))
5565 Decimal('100')
5566 >>> ExtendedContext.to_integral_exact(Decimal('100.0'))
5567 Decimal('100')
5568 >>> ExtendedContext.to_integral_exact(Decimal('101.5'))
5569 Decimal('102')
5570 >>> ExtendedContext.to_integral_exact(Decimal('-101.5'))
5571 Decimal('-102')
5572 >>> ExtendedContext.to_integral_exact(Decimal('10E+5'))
5573 Decimal('1.0E+6')
5574 >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77'))
5575 Decimal('7.89E+77')
5576 >>> ExtendedContext.to_integral_exact(Decimal('-Inf'))
5577 Decimal('-Infinity')
5591 >>> ExtendedContext.to_integral_value(Decimal('2.1'))
5592 Decimal('2')
5593 >>> ExtendedContext.to_integral_value(Decimal('100'))
5594 Decimal('100')
5595 >>> ExtendedContext.to_integral_value(Decimal('100.0'))
5596 Decimal('100')
5597 >>> ExtendedContext.to_integral_value(Decimal('101.5'))
5598 Decimal('102')
5599 >>> ExtendedContext.to_integral_value(Decimal('-101.5'))
5600 Decimal('-102')
5601 >>> ExtendedContext.to_integral_value(Decimal('10E+5'))
5602 Decimal('1.0E+6')
5603 >>> ExtendedContext.to_integral_value(Decimal('7.89E+77'))
5604 Decimal('7.89E+77')
5605 >>> ExtendedContext.to_integral_value(Decimal('-Inf'))
5606 Decimal('-Infinity')
5625 elif isinstance(value, Decimal):
5853 Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__."""
5925 """Compute an approximation to exp(c*10**e), with p decimal places of
5978 # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point
6014 """Convert other to Decimal.
6021 if isinstance(other, Decimal):
6024 return Decimal(other)
6026 return Decimal.from_float(other)
6029 raise TypeError("Unable to convert %s to Decimal" % other)
6033 """Given a Decimal instance self and a Python object other, return
6034 a pair (s, o) of Decimal instances such that "s op o" is
6039 if isinstance(other, Decimal):
6051 return self, Decimal(other.numerator)
6065 return self, Decimal.from_float(other)
6113 # at least one decimal digit, possibly after the decimal point. The
6140 # The functions in this section have little to do with the Decimal
6144 # A format specifier for Decimal looks like:
6187 decimal_point: string to use for decimal point
6232 # determine thousands separator, grouping, and decimal separator, and
6354 intpart: string of digits that must appear before the decimal point
6360 insert separators (decimal separator and thousands separators)
6391 _Infinity = Decimal('Inf')
6392 _NegativeInfinity = Decimal('-Inf')
6393 _NaN = Decimal('NaN')
6394 _Zero = Decimal(0)
6395 _One = Decimal(1)
6396 _NegativeOne = Decimal(-1)