Home | History | Annotate | Download | only in Lib

Lines Matching defs: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',
148 __name__ = 'decimal' # For pickling
150 # See http://speleotrove.com/decimal/
406 permitted in the Decimal() constructor, context.create_decimal() and
410 Decimal.from_float() or context.create_decimal_from_float() do not
467 The returned context manager creates a local decimal context
480 # General Decimal Arithmetic Specification
503 ##### Decimal class #######################################################
505 # Do not subclass Decimal from numbers.Real and do not register it as such
509 class Decimal(object):
510 """Floating point class for decimal arithmetic."""
513 # Generally, the value of the Decimal instance is given by
519 """Create a decimal point instance.
521 >>> Decimal('3.14') # string input
522 Decimal('3.14')
523 >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
524 Decimal('3.14')
525 >>> Decimal(314) # int
526 Decimal('314')
527 >>> Decimal(Decimal(314)) # another decimal instance
528 Decimal('314')
529 >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
530 Decimal('3.14')
538 # and the Decimal constructor still deal with tuples of
551 "Invalid literal for Decimal: %r" % value)
592 # From another decimal
593 if isinstance(value, Decimal):
611 raise ValueError('Invalid tuple size in creation of Decimal '
659 value = Decimal.from_float(value)
666 raise TypeError("Cannot convert %r to Decimal" % value)
670 """Converts a float to a decimal number, exactly.
672 Note that Decimal.from_float(0.1) is not the same as Decimal('0.1').
675 0x1.999999999999ap-4. The exact equivalent of the value in decimal
678 >>> Decimal.from_float(0.1)
679 Decimal('0.1000000000000000055511151231257827021181583404541015625')
680 >>> Decimal.from_float(float('nan'))
681 Decimal('NaN')
682 >>> Decimal.from_float(float('inf'))
683 Decimal('Infinity')
684 >>> Decimal.from_float(-float('inf'))
685 Decimal('-Infinity')
686 >>> Decimal.from_float(-0.0)
687 Decimal('-0')
708 if cls is Decimal:
814 """Compare the two non-NaN decimal instances self and other.
829 # check for zeros; Decimal('0') == Decimal('-0')
860 # Note: The Decimal standard doesn't cover rich comparisons for
922 """Compare self to other. Return a decimal value:
924 a or b is a NaN ==> Decimal('NaN')
925 Decimal('-1')
926 a == b ==> Decimal('0')
927 a > b ==> Decimal('1')
937 return Decimal(self._cmp(other))
942 # In order to make sure that the hash of a Decimal instance
973 """Express a finite Decimal instance in the form n / d.
978 >>> Decimal('3.14').as_integer_ratio()
980 >>> Decimal('-123e5').as_integer_ratio()
982 >>> Decimal('0.00').as_integer_ratio()
1023 """Represents the number as an instance of Decimal."""
1025 return "Decimal('%s')" % str(self)
1042 # number of digits of self._int to left of decimal point
1046 # decimal point in the mantissa of the output string (that is,
1083 can leave up to 3 digits to the left of the decimal place and may
1102 # -Decimal('0') is Decimal('0'), not Decimal('-0'), except
1127 ans = Decimal(self)
1174 return Decimal(self)
1176 return Decimal(other) # Can't both be infinity here
1235 ans = Decimal(result)
1525 ans = Decimal(self)
1637 return Decimal(0)
1655 return Decimal(self)
1663 self - Decimal instance
1673 return Decimal(self)
1686 return Decimal(self)
1747 return Decimal(self)
1750 # self is a finite, nonzero Decimal
1829 If only one argument is supplied, round a finite Decimal
1835 >>> round(Decimal('123.456'))
1837 >>> round(Decimal('-456.789'))
1839 >>> round(Decimal('-3.0'))
1841 >>> round(Decimal('2.5'))
1843 >>> round(Decimal('3.5'))
1845 >>> round(Decimal('Inf'))
1849 >>> round(Decimal('NaN'))
1855 decimal places using the rounding mode for the current
1859 self.quantize(Decimal('1En')).
1861 >>> round(Decimal('123.456'), 0)
1862 Decimal('123')
1863 >>> round(Decimal('123.456'), 2)
1864 Decimal('123.46')
1865 >>> round(Decimal('123.456'), -2)
1866 Decimal('1E+2')
1867 >>> round(Decimal('-Infinity'), 37)
1868 Decimal('NaN')
1869 >>> round(Decimal('sNaN123'), 0)
1870 Decimal('NaN123')
1891 For a finite Decimal instance self, return the greatest
1906 For a finite Decimal instance self, return the least integer n
2011 # additional restriction for decimal: the modulus must be less
2034 # Decimal integers (i.e. force their exponents to be >= 0)
2551 return Decimal(self) # if both are inf, it is OK
2620 return Decimal(self)
2643 """Round a nonzero, nonspecial Decimal to a fixed number of
2655 return Decimal(self)
2679 return Decimal(self)
2681 return Decimal(self)
2704 return Decimal(self)
2706 return Decimal(self)
2724 return Decimal(self)
2739 # <= sqrt(c) < 10**p, so the closest representable Decimal at
2912 """Returns the same Decimal object.
3053 return Decimal(self)
3110 Currently, the encoding of a Decimal instance is always
3111 canonical, so this method returns True for any Decimal.
3118 A Decimal instance is considered finite if it is neither
3295 ans = Decimal(self._exp + len(self._int) - 1)
3344 # Decimal. Note that no attempt is made to fit the result
3346 ans = Decimal(self.adjusted())
3629 """Just returns 10, as this is Decimal, :)"""
3630 return Decimal(10)
3649 return Decimal(self)
3684 return Decimal(self)
3707 return Decimal(self)
3733 if type(self) is Decimal:
3738 if type(self) is Decimal:
3745 """Format a Decimal instance according to the given specifier.
3755 # there should be at least one digit after the decimal point.
3757 # Decimal---it's presumably there to make sure that
3795 # figure out placement of the decimal point
3810 # find digits before and after decimal point, and get exponent
3822 # done with the decimal-specific stuff; hand over the rest
3827 """Create a decimal instance directly, without any validation,
3834 self = object.__new__(Decimal)
3842 # Register Decimal as a kind of Number (an abstract base class).
3845 _numbers.Number.register(Decimal)
3854 the previous decimal context in __exit__()
3866 """Contains the context for a Decimal instance.
3876 Should be reset by user of Decimal instance.
3968 "'decimal.Context' object has no attribute '%s'" % name)
4091 """Creates a new Decimal instance but using self as context.
4094 IBM Decimal specification."""
4101 d = Decimal(num, context=self)
4108 """Creates a new Decimal instance from a float but rounding using self
4113 Decimal('3.1415')
4118 decimal.Inexact: None
4121 d = Decimal.from_float(f) # An exact conversion
4132 >>> ExtendedContext.abs(Decimal('2.1'))
4133 Decimal('2.1')
4134 >>> ExtendedContext.abs(Decimal('-100'))
4135 Decimal('100')
4136 >>> ExtendedContext.abs(Decimal('101.5'))
4137 Decimal('101.5')
4138 >>> ExtendedContext.abs(Decimal('-101.5'))
4139 Decimal('101.5')
4141 Decimal('1')
4149 >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
4150 Decimal('19.00')
4151 >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
4152 Decimal('1.02E+4')
4153 >>> ExtendedContext.add(1, Decimal(2))
4154 Decimal('3')
4155 >>> ExtendedContext.add(Decimal(8), 5)
4156 Decimal('13')
4158 Decimal('10')
4163 raise TypeError("Unable to convert %s to Decimal" % b)
4171 """Returns the same Decimal object.
4176 >>> ExtendedContext.canonical(Decimal('2.50'))
4177 Decimal('2.50')
4179 if not isinstance(a, Decimal):
4180 raise TypeError("canonical requires a Decimal as an argument.")
4197 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
4198 Decimal('-1')
4199 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
4200 Decimal('0')
4201 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
4202 Decimal('0')
4203 >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
4204 Decimal('1')
4205 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
4206 Decimal('1')
4207 >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
4208 Decimal('-1')
4210 Decimal('-1')
4211 >>> ExtendedContext.compare(Decimal(1), 2)
4212 Decimal('-1')
4213 >>> ExtendedContext.compare(1, Decimal(2))
4214 Decimal('-1')
4226 >>> c.compare_signal(Decimal('2.1'), Decimal('3'))
4227 Decimal('-1')
4228 >>> c.compare_signal(Decimal('2.1'), Decimal('2.1'))
4229 Decimal('0')
4233 >>> c.compare_signal(Decimal('NaN'), Decimal('2.1'))
4234 Decimal('NaN')
4240 >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1'))
4241 Decimal('NaN')
4245 Decimal('-1')
4246 >>> c.compare_signal(Decimal(-1), 2)
4247 Decimal('-1')
4248 >>> c.compare_signal(-1, Decimal(2))
4249 Decimal('-1')
4261 >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9'))
4262 Decimal('-1')
4263 >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12'))
4264 Decimal('-1')
4265 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3'))
4266 Decimal('-1')
4267 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30'))
4268 Decimal('0')
4269 >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300'))
4270 Decimal('1')
4271 >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN'))
4272 Decimal('-1')
4274 Decimal('-1')
4275 >>> ExtendedContext.compare_total(Decimal(1), 2)
4276 Decimal('-1')
4277 >>> ExtendedContext.compare_total(1, Decimal(2))
4278 Decimal('-1')
4294 >>> ExtendedContext.copy_abs(Decimal('2.1'))
4295 Decimal('2.1')
4296 >>> ExtendedContext.copy_abs(Decimal('-100'))
4297 Decimal('100')
4299 Decimal('1')
4305 """Returns a copy of the decimal object.
4307 >>> ExtendedContext.copy_decimal(Decimal('2.1'))
4308 Decimal('2.1')
4309 >>> ExtendedContext.copy_decimal(Decimal('-1.00'))
4310 Decimal('-1.00')
4312 Decimal('1')
4315 return Decimal(a)
4320 >>> ExtendedContext.copy_negate(Decimal('101.5'))
4321 Decimal('-101.5')
4322 >>> ExtendedContext.copy_negate(Decimal('-101.5'))
4323 Decimal('101.5')
4325 Decimal('-1')
4336 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33'))
4337 Decimal('1.50')
4338 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33'))
4339 Decimal('1.50')
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')
4345 Decimal('-1')
4346 >>> ExtendedContext.copy_sign(Decimal(1), -2)
4347 Decimal('-1')
4348 >>> ExtendedContext.copy_sign(1, Decimal(-2))
4349 Decimal('-1')
4355 """Decimal division in a specified context.
4357 >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
4358 Decimal('0.333333333')
4359 >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
4360 Decimal('0.666666667')
4361 >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
4362 Decimal('2.5')
4363 >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
4364 Decimal('0.1')
4365 >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
4366 Decimal('1')
4367 >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
4368 Decimal('4.00')
4369 >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
4370 Decimal('1.20')
4371 >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
4372 Decimal('10')
4373 >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
4374 Decimal('1000')
4375 >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
4376 Decimal('1.20E+6')
4378 Decimal('1')
4379 >>> ExtendedContext.divide(Decimal(5), 5)
4380 Decimal('1')
4381 >>> ExtendedContext.divide(5, Decimal(5))
4382 Decimal('1')
4387 raise TypeError("Unable to convert %s to Decimal" % b)
4394 >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
4395 Decimal('0')
4396 >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
4397 Decimal('3')
4398 >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
4399 Decimal('3')
4401 Decimal('3')
4402 >>> ExtendedContext.divide_int(Decimal(10), 3)
4403 Decimal('3')
4404 >>> ExtendedContext.divide_int(10, Decimal(3))
4405 Decimal('3')
4410 raise TypeError("Unable to convert %s to Decimal" % b)
4417 >>> ExtendedContext.divmod(Decimal(8), Decimal(3))
4418 (Decimal('2'), Decimal('2'))
4419 >>> ExtendedContext.divmod(Decimal(8), Decimal(4))
4420 (Decimal('2'), Decimal('0'))
4422 (Decimal('2'), Decimal('0'))
4423 >>> ExtendedContext.divmod(Decimal(8), 4)
4424 (Decimal('2'), Decimal('0'))
4425 >>> ExtendedContext.divmod(8, Decimal(4))
4426 (Decimal('2'), Decimal('0'))
4431 raise TypeError("Unable to convert %s to Decimal" % b)
4441 >>> c.exp(Decimal('-Infinity'))
4442 Decimal('0')
4443 >>> c.exp(Decimal('-1'))
4444 Decimal('0.367879441')
4445 >>> c.exp(Decimal('0'))
4446 Decimal('1')
4447 >>> c.exp(Decimal('1'))
4448 Decimal('2.71828183')
4449 >>> c.exp(Decimal('0.693147181'))
4450 Decimal('2.00000000')
4451 >>> c.exp(Decimal('+Infinity'))
4452 Decimal('Infinity')
4454 Decimal('22026.4658')
4466 >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7'))
4467 Decimal('22')
4468 >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7'))
4469 Decimal('-8')
4470 >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578'))
4471 Decimal('1.38435736E+12')
4473 Decimal('7')
4474 >>> ExtendedContext.fma(1, Decimal(3), 4)
4475 Decimal('7')
4476 >>> ExtendedContext.fma(1, 3, Decimal(4))
4477 Decimal('7')
4485 Currently, the encoding of a Decimal instance is always
4486 canonical, so this method returns True for any Decimal.
4488 >>> ExtendedContext.is_canonical(Decimal('2.50'))
4491 if not isinstance(a, Decimal):
4492 raise TypeError("is_canonical requires a Decimal as an argument.")
4498 A Decimal instance is considered finite if it is neither
4501 >>> ExtendedContext.is_finite(Decimal('2.50'))
4503 >>> ExtendedContext.is_finite(Decimal('-0.3'))
4505 >>> ExtendedContext.is_finite(Decimal('0'))
4507 >>> ExtendedContext.is_finite(Decimal('Inf'))
4509 >>> ExtendedContext.is_finite(Decimal('NaN'))
4520 >>> ExtendedContext.is_infinite(Decimal('2.50'))
4522 >>> ExtendedContext.is_infinite(Decimal('-Inf'))
4524 >>> ExtendedContext.is_infinite(Decimal('NaN'))
4536 >>> ExtendedContext.is_nan(Decimal('2.50'))
4538 >>> ExtendedContext.is_nan(Decimal('NaN'))
4540 >>> ExtendedContext.is_nan(Decimal('-sNaN'))
4555 >>> c.is_normal(Decimal('2.50'))
4557 >>> c.is_normal(Decimal('0.1E-999'))
4559 >>> c.is_normal(Decimal('0.00'))
4561 >>> c.is_normal(Decimal('-Inf'))
4563 >>> c.is_normal(Decimal('NaN'))
4574 >>> ExtendedContext.is_qnan(Decimal('2.50'))
4576 >>> ExtendedContext.is_qnan(Decimal('NaN'))
4578 >>> ExtendedContext.is_qnan(Decimal('sNaN'))
4589 >>> ExtendedContext.is_signed(Decimal('2.50'))
4591 >>> ExtendedContext.is_signed(Decimal('-12'))
4593 >>> ExtendedContext.is_signed(Decimal('-0'))
4607 >>> ExtendedContext.is_snan(Decimal('2.50'))
4609 >>> ExtendedContext.is_snan(Decimal('NaN'))
4611 >>> ExtendedContext.is_snan(Decimal('sNaN'))
4625 >>> c.is_subnormal(Decimal('2.50'))
4627 >>> c.is_subnormal(Decimal('0.1E-999'))
4629 >>> c.is_subnormal(Decimal('0.00'))
4631 >>> c.is_subnormal(Decimal('-Inf'))
4633 >>> c.is_subnormal(Decimal('NaN'))
4644 >>> ExtendedContext.is_zero(Decimal('0'))
4646 >>> ExtendedContext.is_zero(Decimal('2.50'))
4648 >>> ExtendedContext.is_zero(Decimal('-0E+2'))
4664 >>> c.ln(Decimal('0'))
4665 Decimal('-Infinity')
4666 >>> c.ln(Decimal('1.000'))
4667 Decimal('0')
4668 >>> c.ln(Decimal('2.71828183'))
4669 Decimal('1.00000000')
4670 >>> c.ln(Decimal('10'))
4671 Decimal('2.30258509')
4672 >>> c.ln(Decimal('+Infinity'))
4673 Decimal('Infinity')
4675 Decimal('0')
4686 >>> c.log10(Decimal('0'))
4687 Decimal('-Infinity')
4688 >>> c.log10(Decimal('0.001'))
4689 Decimal('-3')
4690 >>> c.log10(Decimal('1.000'))
4691 Decimal('0')
4692 >>> c.log10(Decimal('2'))
4693 Decimal('0.301029996')
4694 >>> c.log10(Decimal('10'))
4695 Decimal('1')
4696 >>> c.log10(Decimal('70'))
4697 Decimal('1.84509804')
4698 >>> c.log10(Decimal('+Infinity'))
4699 Decimal('Infinity')
4701 Decimal('-Infinity')
4703 Decimal('0')
4716 >>> ExtendedContext.logb(Decimal('250'))
4717 Decimal('2')
4718 >>> ExtendedContext.logb(Decimal('2.50'))
4719 Decimal('0')
4720 >>> ExtendedContext.logb(Decimal('0.03'))
4721 Decimal('-2')
4722 >>> ExtendedContext.logb(Decimal('0'))
4723 Decimal('-Infinity')
4725 Decimal('0')
4727 Decimal('1')
4729 Decimal('2')
4739 >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
4740 Decimal('0')
4741 >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1'))
4742 Decimal('0')
4743 >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0'))
4744 Decimal('0')
4745 >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1'))
4746 Decimal('1')
4747 >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010'))
4748 Decimal('1000')
4749 >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10'))
4750 Decimal('10')
4752 Decimal('100')
4753 >>> ExtendedContext.logical_and(Decimal(110), 1101)
4754 Decimal('100')
4755 >>> ExtendedContext.logical_and(110, Decimal(1101))
4756 Decimal('100')
4766 >>> ExtendedContext.logical_invert(Decimal('0'))
4767 Decimal('111111111')
4768 >>> ExtendedContext.logical_invert(Decimal('1'))
4769 Decimal('111111110')
4770 >>> ExtendedContext.logical_invert(Decimal('111111111'))
4771 Decimal('0')
4772 >>> ExtendedContext.logical_invert(Decimal('101010101'))
4773 Decimal('10101010')
4775 Decimal('111110010')
4785 >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
4786 Decimal('0')
4787 >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1'))
4788 Decimal('1')
4789 >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0'))
4790 Decimal('1')
4791 >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1'))
4792 Decimal('1')
4793 >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010'))
4794 Decimal('1110')
4795 >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10'))
4796 Decimal('1110')
4798 Decimal('1111')
4799 >>> ExtendedContext.logical_or(Decimal(110), 1101)
4800 Decimal('1111')
4801 >>> ExtendedContext.logical_or(110, Decimal(1101))
4802 Decimal('1111')
4812 >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
4813 Decimal('0')
4814 >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1'))
4815 Decimal('1')
4816 >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0'))
4817 Decimal('1')
4818 >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1'))
4819 Decimal('0')
4820 >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010'))
4821 Decimal('110')
4822 >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10'))
4823 Decimal('1101')
4825 Decimal('1011')
4826 >>> ExtendedContext.logical_xor(Decimal(110), 1101)
4827 Decimal('1011')
4828 >>> ExtendedContext.logical_xor(110, Decimal(1101))
4829 Decimal('1011')
4843 >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
4844 Decimal('3')
4845 >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
4846 Decimal('3')
4847 >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
4848 Decimal('1')
4849 >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
4850 Decimal('7')
4852 Decimal('2')
4853 >>> ExtendedContext.max(Decimal(1), 2)
4854 Decimal('2')
4855 >>> ExtendedContext.max(1, Decimal(2))
4856 Decimal('2')
4864 >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN'))
4865 Decimal('7')
4866 >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10'))
4867 Decimal('-10')
4869 Decimal('-2')
4870 >>> ExtendedContext.max_mag(Decimal(1), -2)
4871 Decimal('-2')
4872 >>> ExtendedContext.max_mag(1, Decimal(-2))
4873 Decimal('-2')
4887 >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
4888 Decimal('2')
4889 >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
4890 Decimal('-10')
4891 >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
4892 Decimal('1.0')
4893 >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
4894 Decimal('7')
4896 Decimal('1')
4897 >>> ExtendedContext.min(Decimal(1), 2)
4898 Decimal('1')
4899 >>> ExtendedContext.min(1, Decimal(29))
4900 Decimal('1')
4908 >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2'))
4909 Decimal('-2')
4910 >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN'))
4911 Decimal('-3')
4913 Decimal('1')
4914 >>> ExtendedContext.min_mag(Decimal(1), -2)
4915 Decimal('1')
4916 >>> ExtendedContext.min_mag(1, Decimal(-2))
4917 Decimal('1')
4929 >>> ExtendedContext.minus(Decimal('1.3'))
4930 Decimal('-1.3')
4931 >>> ExtendedContext.minus(Decimal('-1.3'))
4932 Decimal('1.3')
4934 Decimal('-1')
4947 >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
4948 Decimal('3.60')
4949 >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
4950 Decimal('21')
4951 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
4952 Decimal('0.72')
4953 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
4954 Decimal('-0.0')
4955 >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
4956 Decimal('4.28135971E+11')
4958 Decimal('49')
4959 >>> ExtendedContext.multiply(Decimal(7), 7)
4960 Decimal('49')
4961 >>> ExtendedContext.multiply(7, Decimal(7))
4962 Decimal('49')
4967 raise TypeError("Unable to convert %s to Decimal" % b)
4977 >>> ExtendedContext.next_minus(Decimal('1'))
4978 Decimal('0.999999999')
4979 >>> c.next_minus(Decimal('1E-1007'))
4980 Decimal('0E-1007')
4981 >>> ExtendedContext.next_minus(Decimal('-1.00000003'))
4982 Decimal('-1.00000004')
4983 >>> c.next_minus(Decimal('Infinity'))
4984 Decimal('9.99999999E+999')
4986 Decimal('0.999999999')
4997 >>> ExtendedContext.next_plus(Decimal('1'))
4998 Decimal('1.00000001')
4999 >>> c.next_plus(Decimal('-1E-1007'))
5000 Decimal('-0E-1007')
5001 >>> ExtendedContext.next_plus(Decimal('-1.00000003'))
5002 Decimal('-1.00000002')
5003 >>> c.next_plus(Decimal('-Infinity'))
5004 Decimal('-9.99999999E+999')
5006 Decimal('1.00000001')
5022 >>> c.next_toward(Decimal('1'), Decimal('2'))
5023 Decimal('1.00000001')
5024 >>> c.next_toward(Decimal('-1E-1007'), Decimal('1'))
5025 Decimal('-0E-1007')
5026 >>> c.next_toward(Decimal('-1.00000003'), Decimal('0'))
5027 Decimal('-1.00000002')
5028 >>> c.next_toward(Decimal('1'), Decimal('0'))
5029 Decimal('0.999999999')
5030 >>> c.next_toward(Decimal('1E-1007'), Decimal('-100'))
5031 Decimal('0E-1007')
5032 >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10'))
5033 Decimal('-1.00000004')
5034 >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000'))
5035 Decimal('-0.00')
5037 Decimal('1E-1007')
5038 >>> c.next_toward(Decimal(0), 1)
5039 Decimal('1E-1007')
5040 >>> c.next_toward(0, Decimal(1))
5041 Decimal('1E-1007')
5052 >>> ExtendedContext.normalize(Decimal('2.1'))
5053 Decimal('2.1')
5054 >>> ExtendedContext.normalize(Decimal('-2.0'))
5055 Decimal('-2')
5056 >>> ExtendedContext.normalize(Decimal('1.200'))
5057 Decimal('1.2')
5058 >>> ExtendedContext.normalize(Decimal('-120'))
5059 Decimal('-1.2E+2')
5060 >>> ExtendedContext.normalize(Decimal('120.00'))
5061 Decimal('1.2E+2')
5062 >>> ExtendedContext.normalize(Decimal('0.00'))
5063 Decimal('0')
5065 Decimal('6')
5088 >>> c.number_class(Decimal('Infinity'))
5090 >>> c.number_class(Decimal('1E-10'))
5092 >>> c.number_class(Decimal('2.50'))
5094 >>> c.number_class(Decimal('0.1E-999'))
5096 >>> c.number_class(Decimal('0'))
5098 >>> c.number_class(Decimal('-0'))
5100 >>> c.number_class(Decimal('-0.1E-999'))
5102 >>> c.number_class(Decimal('-1E-10'))
5104 >>> c.number_class(Decimal('-2.50'))
5106 >>> c.number_class(Decimal('-Infinity'))
5108 >>> c.number_class(Decimal('NaN'))
5110 >>> c.number_class(Decimal('-NaN'))
5112 >>> c.number_class(Decimal('sNaN'))
5127 >>> ExtendedContext.plus(Decimal('1.3'))
5128 Decimal('1.3')
5129 >>> ExtendedContext.plus(Decimal('-1.3'))
5130 Decimal('-1.3')
5132 Decimal('-1')
5162 >>> c.power(Decimal('2'), Decimal('3'))
5163 Decimal('8')
5164 >>> c.power(Decimal('-2'), Decimal('3'))
5165 Decimal('-8')
5166 >>> c.power(Decimal('2'), Decimal('-3'))
5167 Decimal('0.125')
5168 >>> c.power(Decimal('1.7'), Decimal('8'))
5169 Decimal('69.7575744')
5170 >>> c.power(Decimal('10'), Decimal('0.301029996'))
5171 Decimal('2.00000000')
5172 >>> c.power(Decimal('Infinity'), Decimal('-1'))
5173 Decimal('0')
5174 >>> c.power(Decimal('Infinity'), Decimal('0'))
5175 Decimal('1')
5176 >>> c.power(Decimal('Infinity'), Decimal('1'))
5177 Decimal('Infinity')
5178 >>> c.power(Decimal('-Infinity'), Decimal('-1'))
5179 Decimal('-0')
5180 >>> c.power(Decimal('-Infinity'), Decimal('0'))
5181 Decimal('1')
5182 >>> c.power(Decimal('-Infinity'), Decimal('1'))
5183 Decimal('-Infinity')
5184 >>> c.power(Decimal('-Infinity'), Decimal('2'))
5185 Decimal('Infinity')
5186 >>> c.power(Decimal('0'), Decimal('0'))
5187 Decimal('NaN')
5189 >>> c.power(Decimal('3'), Decimal('7'), Decimal('16'))
5190 Decimal('11')
5191 >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16'))
5192 Decimal('-11')
5193 >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16'))
5194 Decimal('1')
5195 >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16'))
5196 Decimal('11')
5197 >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789'))
5198 Decimal('11729830')
5199 >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729'))
5200 Decimal('-0')
5201 >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537'))
5202 Decimal('1')
5204 Decimal('823543')
5205 >>> ExtendedContext.power(Decimal(7), 7)
5206 Decimal('823543')
5207 >>> ExtendedContext.power(7, Decimal(7), 2)
5208 Decimal('1')
5213 raise TypeError("Unable to convert %s to Decimal" % b)
5235 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
5236 Decimal('2.170')
5237 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
5238 Decimal('2.17')
5239 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
5240 Decimal('2.2')
5241 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
5242 Decimal('2')
5243 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
5244 Decimal('0E+1')
5245 >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
5246 Decimal('-Infinity')
5247 >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
5248 Decimal('NaN')
5249 >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
5250 Decimal('-0')
5251 >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
5252 Decimal('-0E+5')
5253 >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
5254 Decimal('NaN')
5255 >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
5256 Decimal('NaN')
5257 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
5258 Decimal('217.0')
5259 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
5260 Decimal('217')
5261 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
5262 Decimal('2.2E+2')
5263 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
5264 Decimal('2E+2')
5266 Decimal('1')
5267 >>> ExtendedContext.quantize(Decimal(1), 2)
5268 Decimal('1')
5269 >>> ExtendedContext.quantize(1, Decimal(2))
5270 Decimal('1')
5276 """Just returns 10, as this is Decimal, :)
5279 Decimal('10')
5281 return Decimal(10)
5295 >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
5296 Decimal('2.1')
5297 >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
5298 Decimal('1')
5299 >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
5300 Decimal('-1')
5301 >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
5302 Decimal('0.2')
5303 >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
5304 Decimal('0.1')
5305 >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
5306 Decimal('1.0')
5308 Decimal('4')
5309 >>> ExtendedContext.remainder(Decimal(22), 6)
5310 Decimal('4')
5311 >>> ExtendedContext.remainder(22, Decimal(6))
5312 Decimal('4')
5317 raise TypeError("Unable to convert %s to Decimal" % b)
5331 >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
5332 Decimal('-0.9')
5333 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
5334 Decimal('-2')
5335 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
5336 Decimal('1')
5337 >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
5338 Decimal('-1')
5339 >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
5340 Decimal('0.2')
5341 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
5342 Decimal('0.1')
5343 >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
5344 Decimal('-0.3')
5346 Decimal('3')
5347 >>> ExtendedContext.remainder_near(Decimal(3), 11)
5348 Decimal('3')
5349 >>> ExtendedContext.remainder_near(3, Decimal(11))
5350 Decimal('3')
5364 >>> ExtendedContext.rotate(Decimal('34'), Decimal('8'))
5365 Decimal('400000003')
5366 >>> ExtendedContext.rotate(Decimal('12'), Decimal('9'))
5367 Decimal('12')
5368 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2'))
5369 Decimal('891234567')
5370 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0'))
5371 Decimal('123456789')
5372 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2'))
5373 Decimal('345678912')
5375 Decimal('13333330')
5376 >>> ExtendedContext.rotate(Decimal(1333333), 1)
5377 Decimal('13333330')
5378 >>> ExtendedContext.rotate(1333333, Decimal(1))
5379 Decimal('13333330')
5390 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
5392 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
5394 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
5396 >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
5400 >>> ExtendedContext.same_quantum(Decimal(10000), -1)
5402 >>> ExtendedContext.same_quantum(10000, Decimal(-1))
5411 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2'))
5412 Decimal('0.0750')
5413 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0'))
5414 Decimal('7.50')
5415 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3'))
5416 Decimal('7.50E+3')
5418 Decimal('1E+4')
5419 >>> ExtendedContext.scaleb(Decimal(1), 4)
5420 Decimal('1E+4')
5421 >>> ExtendedContext.scaleb(1, Decimal(4))
5422 Decimal('1E+4')
5437 >>> ExtendedContext.shift(Decimal('34'), Decimal('8'))
5438 Decimal('400000000')
5439 >>> ExtendedContext.shift(Decimal('12'), Decimal('9'))
5440 Decimal('0')
5441 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2'))
5442 Decimal('1234567')
5443 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0'))
5444 Decimal('123456789')
5445 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2'))
5446 Decimal('345678900')
5448 Decimal('888888800')
5449 >>> ExtendedContext.shift(Decimal(88888888), 2)
5450 Decimal('888888800')
5451 >>> ExtendedContext.shift(88888888, Decimal(2))
5452 Decimal('888888800')
5463 >>> ExtendedContext.sqrt(Decimal('0'))
5464 Decimal('0')
5465 >>> ExtendedContext.sqrt(Decimal('-0'))
5466 Decimal('-0')
5467 >>> ExtendedContext.sqrt(Decimal('0.39'))
5468 Decimal('0.624499800')
5469 >>> ExtendedContext.sqrt(Decimal('100'))
5470 Decimal('10')
5471 >>> ExtendedContext.sqrt(Decimal('1'))
5472 Decimal('1')
5473 >>> ExtendedContext.sqrt(Decimal('1.0'))
5474 Decimal('1.0')
5475 >>> ExtendedContext.sqrt(Decimal('1.00'))
5476 Decimal('1.0')
5477 >>> ExtendedContext.sqrt(Decimal('7'))
5478 Decimal('2.64575131')
5479 >>> ExtendedContext.sqrt(Decimal('10'))
5480 Decimal('3.16227766')
5482 Decimal('1.41421356')
5492 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
5493 Decimal('0.23')
5494 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
5495 Decimal('0.00')
5496 Decimal('1.3'), Decimal('2.07'))
5497 Decimal('-0.77')
5499 Decimal('3')
5500 >>> ExtendedContext.subtract(Decimal(8), 5)
5501 Decimal('3')
5502 >>> ExtendedContext.subtract(8, Decimal(5))
5503 Decimal('3')
5508 raise TypeError("Unable to convert %s to Decimal" % b)
5516 can leave up to 3 digits to the left of the decimal place and may
5521 >>> ExtendedContext.to_eng_string(Decimal('123E+1'))
5523 >>> ExtendedContext.to_eng_string(Decimal('123E+3'))
5525 >>> ExtendedContext.to_eng_string(Decimal('123E-10'))
5527 >>> ExtendedContext.to_eng_string(Decimal('-123E-12'))
5529 >>> ExtendedContext.to_eng_string(Decimal('7E-7'))
5531 >>> ExtendedContext.to_eng_string(Decimal('7E+1'))
5533 >>> ExtendedContext.to_eng_string(Decimal('0E+1'))
5558 >>> ExtendedContext.to_integral_exact(Decimal('2.1'))
5559 Decimal('2')
5560 >>> ExtendedContext.to_integral_exact(Decimal('100'))
5561 Decimal('100')
5562 >>> ExtendedContext.to_integral_exact(Decimal('100.0'))
5563 Decimal('100')
5564 >>> ExtendedContext.to_integral_exact(Decimal('101.5'))
5565 Decimal('102')
5566 >>> ExtendedContext.to_integral_exact(Decimal('-101.5'))
5567 Decimal('-102')
5568 >>> ExtendedContext.to_integral_exact(Decimal('10E+5'))
5569 Decimal('1.0E+6')
5570 >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77'))
5571 Decimal('7.89E+77')
5572 >>> ExtendedContext.to_integral_exact(Decimal('-Inf'))
5573 Decimal('-Infinity')
5587 >>> ExtendedContext.to_integral_value(Decimal('2.1'))
5588 Decimal('2')
5589 >>> ExtendedContext.to_integral_value(Decimal('100'))
5590 Decimal('100')
5591 >>> ExtendedContext.to_integral_value(Decimal('100.0'))
5592 Decimal('100')
5593 >>> ExtendedContext.to_integral_value(Decimal('101.5'))
5594 Decimal('102')
5595 >>> ExtendedContext.to_integral_value(Decimal('-101.5'))
5596 Decimal('-102')
5597 >>> ExtendedContext.to_integral_value(Decimal('10E+5'))
5598 Decimal('1.0E+6')
5599 >>> ExtendedContext.to_integral_value(Decimal('7.89E+77'))
5600 Decimal('7.89E+77')
5601 >>> ExtendedContext.to_integral_value(Decimal('-Inf'))
5602 Decimal('-Infinity')
5621 elif isinstance(value, Decimal):
5851 Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__."""
5923 """Compute an approximation to exp(c*10**e), with p decimal places of
5976 # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point
6012 """Convert other to Decimal.
6019 if isinstance(other, Decimal):
6022 return Decimal(other)
6024 return Decimal.from_float(other)
6027 raise TypeError("Unable to convert %s to Decimal" % other)
6031 """Given a Decimal instance self and a Python object other, return
6032 a pair (s, o) of Decimal instances such that "s op o" is
6037 if isinstance(other, Decimal):
6049 return self, Decimal(other.numerator)
6063 return self, Decimal.from_float(other)
6111 # at least one decimal digit, possibly after the decimal point. The
6138 # The functions in this section have little to do with the Decimal
6142 # A format specifier for Decimal looks like:
6185 decimal_point: string to use for decimal point
6230 # determine thousands separator, grouping, and decimal separator, and
6352 intpart: string of digits that must appear before the decimal point
6358 insert separators (decimal separator and thousands separators)
6389 _Infinity = Decimal('Inf')
6390 _NegativeInfinity = Decimal('-Inf')
6391 _NaN = Decimal('NaN')
6392 _Zero = Decimal(0)
6393 _One = Decimal(1)
6394 _NegativeOne = Decimal(-1)