• 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
475 The returned context manager creates a local decimal context
488 # General Decimal Arithmetic Specification
517 ##### Decimal class #######################################################
519 # Do not subclass Decimal from numbers.Real and do not register it as such
523 class Decimal(object): class
524 """Floating point class for decimal arithmetic."""
527 # Generally, the value of the Decimal instance is given by
533 """Create a decimal point instance.
535 >>> Decimal('3.14') # string input
536 Decimal('3.14')
537 >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
538 Decimal('3.14')
539 >>> Decimal(314) # int
540 Decimal('314')
541 >>> Decimal(Decimal(314)) # another decimal instance
542 Decimal('314')
543 >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
544 Decimal('3.14')
552 # and the Decimal constructor still deal with tuples of
565 "Invalid literal for Decimal: %r" % value)
606 # From another decimal
607 if isinstance(value, Decimal):
625 raise ValueError('Invalid tuple size in creation of Decimal '
673 value = Decimal.from_float(value)
680 raise TypeError("Cannot convert %r to Decimal" % value)
684 """Converts a float to a decimal number, exactly.
686 Note that Decimal.from_float(0.1) is not the same as Decimal('0.1').
689 0x1.999999999999ap-4. The exact equivalent of the value in decimal
692 >>> Decimal.from_float(0.1)
693 Decimal('0.1000000000000000055511151231257827021181583404541015625')
694 >>> Decimal.from_float(float('nan'))
695 Decimal('NaN')
696 >>> Decimal.from_float(float('inf'))
697 Decimal('Infinity')
698 >>> Decimal.from_float(-float('inf'))
699 Decimal('-Infinity')
700 >>> Decimal.from_float(-0.0)
701 Decimal('-0')
722 if cls is Decimal:
828 """Compare the two non-NaN decimal instances self and other.
843 # check for zeros; Decimal('0') == Decimal('-0')
874 # Note: The Decimal standard doesn't cover rich comparisons for
936 """Compare self to other. Return a decimal value:
938 a or b is a NaN ==> Decimal('NaN')
939 a < b ==> Decimal('-1')
940 a == b ==> Decimal('0')
941 a > b ==> Decimal('1')
951 return Decimal(self._cmp(other))
956 # In order to make sure that the hash of a Decimal instance
987 """Express a finite Decimal instance in the form n / d.
992 >>> Decimal('3.14').as_integer_ratio()
994 >>> Decimal('-123e5').as_integer_ratio()
996 >>> Decimal('0.00').as_integer_ratio()
1037 """Represents the number as an instance of Decimal."""
1039 return "Decimal('%s')" % str(self)
1056 # number of digits of self._int to left of decimal point
1060 # decimal point in the mantissa of the output string (that is,
1097 can leave up to 3 digits to the left of the decimal place and may
1116 # -Decimal('0') is Decimal('0'), not Decimal('-0'), except
1141 ans = Decimal(self)
1188 return Decimal(self)
1190 return Decimal(other) # Can't both be infinity here
1249 ans = Decimal(result)
1539 ans = Decimal(self)
1651 return Decimal(0)
1669 return Decimal(self)
1677 self - Decimal instance
1687 return Decimal(self)
1700 return Decimal(self)
1761 return Decimal(self)
1764 # self is a finite, nonzero Decimal
1843 If only one argument is supplied, round a finite Decimal
1849 >>> round(Decimal('123.456'))
1851 >>> round(Decimal('-456.789'))
1853 >>> round(Decimal('-3.0'))
1855 >>> round(Decimal('2.5'))
1857 >>> round(Decimal('3.5'))
1859 >>> round(Decimal('Inf'))
1863 >>> round(Decimal('NaN'))
1869 decimal places using the rounding mode for the current
1873 self.quantize(Decimal('1En')).
1875 >>> round(Decimal('123.456'), 0)
1876 Decimal('123')
1877 >>> round(Decimal('123.456'), 2)
1878 Decimal('123.46')
1879 >>> round(Decimal('123.456'), -2)
1880 Decimal('1E+2')
1881 >>> round(Decimal('-Infinity'), 37)
1882 Decimal('NaN')
1883 >>> round(Decimal('sNaN123'), 0)
1884 Decimal('NaN123')
1905 For a finite Decimal instance self, return the greatest
1920 For a finite Decimal instance self, return the least integer n
2025 # additional restriction for decimal: the modulus must be less
2048 # Decimal integers (i.e. force their exponents to be >= 0)
2565 return Decimal(self) # if both are inf, it is OK
2634 return Decimal(self)
2657 """Round a nonzero, nonspecial Decimal to a fixed number of
2669 return Decimal(self)
2693 return Decimal(self)
2695 return Decimal(self)
2718 return Decimal(self)
2720 return Decimal(self)
2738 return Decimal(self)
2753 # <= sqrt(c) < 10**p, so the closest representable Decimal at
2926 """Returns the same Decimal object.
3067 return Decimal(self)
3124 Currently, the encoding of a Decimal instance is always
3125 canonical, so this method returns True for any Decimal.
3132 A Decimal instance is considered finite if it is neither
3309 ans = Decimal(self._exp + len(self._int) - 1)
3358 # Decimal. Note that no attempt is made to fit the result
3360 ans = Decimal(self.adjusted())
3643 """Just returns 10, as this is Decimal, :)"""
3644 return Decimal(10)
3663 return Decimal(self)
3698 return Decimal(self)
3721 return Decimal(self)
3747 if type(self) is Decimal:
3752 if type(self) is Decimal:
3759 """Format a Decimal instance according to the given specifier.
3769 # there should be at least one digit after the decimal point.
3771 # Decimal---it's presumably there to make sure that
3813 # figure out placement of the decimal point
3828 # find digits before and after decimal point, and get exponent
3840 # done with the decimal-specific stuff; hand over the rest
3845 """Create a decimal instance directly, without any validation,
3852 self = object.__new__(Decimal)
3860 # Register Decimal as a kind of Number (an abstract base class).
3863 _numbers.Number.register(Decimal)
3872 the previous decimal context in __exit__()
3884 """Contains the context for a Decimal instance.
3894 Should be reset by user of Decimal instance.
3986 "'decimal.Context' object has no attribute '%s'" % name)
4109 """Creates a new Decimal instance but using self as context.
4112 IBM Decimal specification."""
4119 d = Decimal(num, context=self)
4126 """Creates a new Decimal instance from a float but rounding using self
4131 Decimal('3.1415')
4136 decimal.Inexact: None
4139 d = Decimal.from_float(f) # An exact conversion
4150 >>> ExtendedContext.abs(Decimal('2.1'))
4151 Decimal('2.1')
4152 >>> ExtendedContext.abs(Decimal('-100'))
4153 Decimal('100')
4154 >>> ExtendedContext.abs(Decimal('101.5'))
4155 Decimal('101.5')
4156 >>> ExtendedContext.abs(Decimal('-101.5'))
4157 Decimal('101.5')
4159 Decimal('1')
4167 >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
4168 Decimal('19.00')
4169 >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
4170 Decimal('1.02E+4')
4171 >>> ExtendedContext.add(1, Decimal(2))
4172 Decimal('3')
4173 >>> ExtendedContext.add(Decimal(8), 5)
4174 Decimal('13')
4176 Decimal('10')
4181 raise TypeError("Unable to convert %s to Decimal" % b)
4189 """Returns the same Decimal object.
4194 >>> ExtendedContext.canonical(Decimal('2.50'))
4195 Decimal('2.50')
4197 if not isinstance(a, Decimal):
4198 raise TypeError("canonical requires a Decimal as an argument.")
4215 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
4216 Decimal('-1')
4217 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
4218 Decimal('0')
4219 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
4220 Decimal('0')
4221 >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
4222 Decimal('1')
4223 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
4224 Decimal('1')
4225 >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
4226 Decimal('-1')
4228 Decimal('-1')
4229 >>> ExtendedContext.compare(Decimal(1), 2)
4230 Decimal('-1')
4231 >>> ExtendedContext.compare(1, Decimal(2))
4232 Decimal('-1')
4244 >>> c.compare_signal(Decimal('2.1'), Decimal('3'))
4245 Decimal('-1')
4246 >>> c.compare_signal(Decimal('2.1'), Decimal('2.1'))
4247 Decimal('0')
4251 >>> c.compare_signal(Decimal('NaN'), Decimal('2.1'))
4252 Decimal('NaN')
4258 >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1'))
4259 Decimal('NaN')
4263 Decimal('-1')
4264 >>> c.compare_signal(Decimal(-1), 2)
4265 Decimal('-1')
4266 >>> c.compare_signal(-1, Decimal(2))
4267 Decimal('-1')
4279 >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9'))
4280 Decimal('-1')
4281 >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12'))
4282 Decimal('-1')
4283 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3'))
4284 Decimal('-1')
4285 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30'))
4286 Decimal('0')
4287 >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300'))
4288 Decimal('1')
4289 >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN'))
4290 Decimal('-1')
4292 Decimal('-1')
4293 >>> ExtendedContext.compare_total(Decimal(1), 2)
4294 Decimal('-1')
4295 >>> ExtendedContext.compare_total(1, Decimal(2))
4296 Decimal('-1')
4312 >>> ExtendedContext.copy_abs(Decimal('2.1'))
4313 Decimal('2.1')
4314 >>> ExtendedContext.copy_abs(Decimal('-100'))
4315 Decimal('100')
4317 Decimal('1')
4323 """Returns a copy of the decimal object.
4325 >>> ExtendedContext.copy_decimal(Decimal('2.1'))
4326 Decimal('2.1')
4327 >>> ExtendedContext.copy_decimal(Decimal('-1.00'))
4328 Decimal('-1.00')
4330 Decimal('1')
4333 return Decimal(a)
4338 >>> ExtendedContext.copy_negate(Decimal('101.5'))
4339 Decimal('-101.5')
4340 >>> ExtendedContext.copy_negate(Decimal('-101.5'))
4341 Decimal('101.5')
4343 Decimal('-1')
4354 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33'))
4355 Decimal('1.50')
4356 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33'))
4357 Decimal('1.50')
4358 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33'))
4359 Decimal('-1.50')
4360 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33'))
4361 Decimal('-1.50')
4363 Decimal('-1')
4364 >>> ExtendedContext.copy_sign(Decimal(1), -2)
4365 Decimal('-1')
4366 >>> ExtendedContext.copy_sign(1, Decimal(-2))
4367 Decimal('-1')
4373 """Decimal division in a specified context.
4375 >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
4376 Decimal('0.333333333')
4377 >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
4378 Decimal('0.666666667')
4379 >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
4380 Decimal('2.5')
4381 >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
4382 Decimal('0.1')
4383 >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
4384 Decimal('1')
4385 >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
4386 Decimal('4.00')
4387 >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
4388 Decimal('1.20')
4389 >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
4390 Decimal('10')
4391 >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
4392 Decimal('1000')
4393 >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
4394 Decimal('1.20E+6')
4396 Decimal('1')
4397 >>> ExtendedContext.divide(Decimal(5), 5)
4398 Decimal('1')
4399 >>> ExtendedContext.divide(5, Decimal(5))
4400 Decimal('1')
4405 raise TypeError("Unable to convert %s to Decimal" % b)
4412 >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
4413 Decimal('0')
4414 >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
4415 Decimal('3')
4416 >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
4417 Decimal('3')
4419 Decimal('3')
4420 >>> ExtendedContext.divide_int(Decimal(10), 3)
4421 Decimal('3')
4422 >>> ExtendedContext.divide_int(10, Decimal(3))
4423 Decimal('3')
4428 raise TypeError("Unable to convert %s to Decimal" % b)
4435 >>> ExtendedContext.divmod(Decimal(8), Decimal(3))
4436 (Decimal('2'), Decimal('2'))
4437 >>> ExtendedContext.divmod(Decimal(8), Decimal(4))
4438 (Decimal('2'), Decimal('0'))
4440 (Decimal('2'), Decimal('0'))
4441 >>> ExtendedContext.divmod(Decimal(8), 4)
4442 (Decimal('2'), Decimal('0'))
4443 >>> ExtendedContext.divmod(8, Decimal(4))
4444 (Decimal('2'), Decimal('0'))
4449 raise TypeError("Unable to convert %s to Decimal" % b)
4459 >>> c.exp(Decimal('-Infinity'))
4460 Decimal('0')
4461 >>> c.exp(Decimal('-1'))
4462 Decimal('0.367879441')
4463 >>> c.exp(Decimal('0'))
4464 Decimal('1')
4465 >>> c.exp(Decimal('1'))
4466 Decimal('2.71828183')
4467 >>> c.exp(Decimal('0.693147181'))
4468 Decimal('2.00000000')
4469 >>> c.exp(Decimal('+Infinity'))
4470 Decimal('Infinity')
4472 Decimal('22026.4658')
4484 >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7'))
4485 Decimal('22')
4486 >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7'))
4487 Decimal('-8')
4488 >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578'))
4489 Decimal('1.38435736E+12')
4491 Decimal('7')
4492 >>> ExtendedContext.fma(1, Decimal(3), 4)
4493 Decimal('7')
4494 >>> ExtendedContext.fma(1, 3, Decimal(4))
4495 Decimal('7')
4503 Currently, the encoding of a Decimal instance is always
4504 canonical, so this method returns True for any Decimal.
4506 >>> ExtendedContext.is_canonical(Decimal('2.50'))
4509 if not isinstance(a, Decimal):
4510 raise TypeError("is_canonical requires a Decimal as an argument.")
4516 A Decimal instance is considered finite if it is neither
4519 >>> ExtendedContext.is_finite(Decimal('2.50'))
4521 >>> ExtendedContext.is_finite(Decimal('-0.3'))
4523 >>> ExtendedContext.is_finite(Decimal('0'))
4525 >>> ExtendedContext.is_finite(Decimal('Inf'))
4527 >>> ExtendedContext.is_finite(Decimal('NaN'))
4538 >>> ExtendedContext.is_infinite(Decimal('2.50'))
4540 >>> ExtendedContext.is_infinite(Decimal('-Inf'))
4542 >>> ExtendedContext.is_infinite(Decimal('NaN'))
4554 >>> ExtendedContext.is_nan(Decimal('2.50'))
4556 >>> ExtendedContext.is_nan(Decimal('NaN'))
4558 >>> ExtendedContext.is_nan(Decimal('-sNaN'))
4573 >>> c.is_normal(Decimal('2.50'))
4575 >>> c.is_normal(Decimal('0.1E-999'))
4577 >>> c.is_normal(Decimal('0.00'))
4579 >>> c.is_normal(Decimal('-Inf'))
4581 >>> c.is_normal(Decimal('NaN'))
4592 >>> ExtendedContext.is_qnan(Decimal('2.50'))
4594 >>> ExtendedContext.is_qnan(Decimal('NaN'))
4596 >>> ExtendedContext.is_qnan(Decimal('sNaN'))
4607 >>> ExtendedContext.is_signed(Decimal('2.50'))
4609 >>> ExtendedContext.is_signed(Decimal('-12'))
4611 >>> ExtendedContext.is_signed(Decimal('-0'))
4625 >>> ExtendedContext.is_snan(Decimal('2.50'))
4627 >>> ExtendedContext.is_snan(Decimal('NaN'))
4629 >>> ExtendedContext.is_snan(Decimal('sNaN'))
4643 >>> c.is_subnormal(Decimal('2.50'))
4645 >>> c.is_subnormal(Decimal('0.1E-999'))
4647 >>> c.is_subnormal(Decimal('0.00'))
4649 >>> c.is_subnormal(Decimal('-Inf'))
4651 >>> c.is_subnormal(Decimal('NaN'))
4662 >>> ExtendedContext.is_zero(Decimal('0'))
4664 >>> ExtendedContext.is_zero(Decimal('2.50'))
4666 >>> ExtendedContext.is_zero(Decimal('-0E+2'))
4682 >>> c.ln(Decimal('0'))
4683 Decimal('-Infinity')
4684 >>> c.ln(Decimal('1.000'))
4685 Decimal('0')
4686 >>> c.ln(Decimal('2.71828183'))
4687 Decimal('1.00000000')
4688 >>> c.ln(Decimal('10'))
4689 Decimal('2.30258509')
4690 >>> c.ln(Decimal('+Infinity'))
4691 Decimal('Infinity')
4693 Decimal('0')
4704 >>> c.log10(Decimal('0'))
4705 Decimal('-Infinity')
4706 >>> c.log10(Decimal('0.001'))
4707 Decimal('-3')
4708 >>> c.log10(Decimal('1.000'))
4709 Decimal('0')
4710 >>> c.log10(Decimal('2'))
4711 Decimal('0.301029996')
4712 >>> c.log10(Decimal('10'))
4713 Decimal('1')
4714 >>> c.log10(Decimal('70'))
4715 Decimal('1.84509804')
4716 >>> c.log10(Decimal('+Infinity'))
4717 Decimal('Infinity')
4719 Decimal('-Infinity')
4721 Decimal('0')
4734 >>> ExtendedContext.logb(Decimal('250'))
4735 Decimal('2')
4736 >>> ExtendedContext.logb(Decimal('2.50'))
4737 Decimal('0')
4738 >>> ExtendedContext.logb(Decimal('0.03'))
4739 Decimal('-2')
4740 >>> ExtendedContext.logb(Decimal('0'))
4741 Decimal('-Infinity')
4743 Decimal('0')
4745 Decimal('1')
4747 Decimal('2')
4757 >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
4758 Decimal('0')
4759 >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1'))
4760 Decimal('0')
4761 >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0'))
4762 Decimal('0')
4763 >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1'))
4764 Decimal('1')
4765 >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010'))
4766 Decimal('1000')
4767 >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10'))
4768 Decimal('10')
4770 Decimal('100')
4771 >>> ExtendedContext.logical_and(Decimal(110), 1101)
4772 Decimal('100')
4773 >>> ExtendedContext.logical_and(110, Decimal(1101))
4774 Decimal('100')
4784 >>> ExtendedContext.logical_invert(Decimal('0'))
4785 Decimal('111111111')
4786 >>> ExtendedContext.logical_invert(Decimal('1'))
4787 Decimal('111111110')
4788 >>> ExtendedContext.logical_invert(Decimal('111111111'))
4789 Decimal('0')
4790 >>> ExtendedContext.logical_invert(Decimal('101010101'))
4791 Decimal('10101010')
4793 Decimal('111110010')
4803 >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
4804 Decimal('0')
4805 >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1'))
4806 Decimal('1')
4807 >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0'))
4808 Decimal('1')
4809 >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1'))
4810 Decimal('1')
4811 >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010'))
4812 Decimal('1110')
4813 >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10'))
4814 Decimal('1110')
4816 Decimal('1111')
4817 >>> ExtendedContext.logical_or(Decimal(110), 1101)
4818 Decimal('1111')
4819 >>> ExtendedContext.logical_or(110, Decimal(1101))
4820 Decimal('1111')
4830 >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
4831 Decimal('0')
4832 >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1'))
4833 Decimal('1')
4834 >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0'))
4835 Decimal('1')
4836 >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1'))
4837 Decimal('0')
4838 >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010'))
4839 Decimal('110')
4840 >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10'))
4841 Decimal('1101')
4843 Decimal('1011')
4844 >>> ExtendedContext.logical_xor(Decimal(110), 1101)
4845 Decimal('1011')
4846 >>> ExtendedContext.logical_xor(110, Decimal(1101))
4847 Decimal('1011')
4861 >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
4862 Decimal('3')
4863 >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
4864 Decimal('3')
4865 >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
4866 Decimal('1')
4867 >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
4868 Decimal('7')
4870 Decimal('2')
4871 >>> ExtendedContext.max(Decimal(1), 2)
4872 Decimal('2')
4873 >>> ExtendedContext.max(1, Decimal(2))
4874 Decimal('2')
4882 >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN'))
4883 Decimal('7')
4884 >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10'))
4885 Decimal('-10')
4887 Decimal('-2')
4888 >>> ExtendedContext.max_mag(Decimal(1), -2)
4889 Decimal('-2')
4890 >>> ExtendedContext.max_mag(1, Decimal(-2))
4891 Decimal('-2')
4905 >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
4906 Decimal('2')
4907 >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
4908 Decimal('-10')
4909 >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
4910 Decimal('1.0')
4911 >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
4912 Decimal('7')
4914 Decimal('1')
4915 >>> ExtendedContext.min(Decimal(1), 2)
4916 Decimal('1')
4917 >>> ExtendedContext.min(1, Decimal(29))
4918 Decimal('1')
4926 >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2'))
4927 Decimal('-2')
4928 >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN'))
4929 Decimal('-3')
4931 Decimal('1')
4932 >>> ExtendedContext.min_mag(Decimal(1), -2)
4933 Decimal('1')
4934 >>> ExtendedContext.min_mag(1, Decimal(-2))
4935 Decimal('1')
4947 >>> ExtendedContext.minus(Decimal('1.3'))
4948 Decimal('-1.3')
4949 >>> ExtendedContext.minus(Decimal('-1.3'))
4950 Decimal('1.3')
4952 Decimal('-1')
4965 >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
4966 Decimal('3.60')
4967 >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
4968 Decimal('21')
4969 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
4970 Decimal('0.72')
4971 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
4972 Decimal('-0.0')
4973 >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
4974 Decimal('4.28135971E+11')
4976 Decimal('49')
4977 >>> ExtendedContext.multiply(Decimal(7), 7)
4978 Decimal('49')
4979 >>> ExtendedContext.multiply(7, Decimal(7))
4980 Decimal('49')
4985 raise TypeError("Unable to convert %s to Decimal" % b)
4995 >>> ExtendedContext.next_minus(Decimal('1'))
4996 Decimal('0.999999999')
4997 >>> c.next_minus(Decimal('1E-1007'))
4998 Decimal('0E-1007')
4999 >>> ExtendedContext.next_minus(Decimal('-1.00000003'))
5000 Decimal('-1.00000004')
5001 >>> c.next_minus(Decimal('Infinity'))
5002 Decimal('9.99999999E+999')
5004 Decimal('0.999999999')
5015 >>> ExtendedContext.next_plus(Decimal('1'))
5016 Decimal('1.00000001')
5017 >>> c.next_plus(Decimal('-1E-1007'))
5018 Decimal('-0E-1007')
5019 >>> ExtendedContext.next_plus(Decimal('-1.00000003'))
5020 Decimal('-1.00000002')
5021 >>> c.next_plus(Decimal('-Infinity'))
5022 Decimal('-9.99999999E+999')
5024 Decimal('1.00000001')
5040 >>> c.next_toward(Decimal('1'), Decimal('2'))
5041 Decimal('1.00000001')
5042 >>> c.next_toward(Decimal('-1E-1007'), Decimal('1'))
5043 Decimal('-0E-1007')
5044 >>> c.next_toward(Decimal('-1.00000003'), Decimal('0'))
5045 Decimal('-1.00000002')
5046 >>> c.next_toward(Decimal('1'), Decimal('0'))
5047 Decimal('0.999999999')
5048 >>> c.next_toward(Decimal('1E-1007'), Decimal('-100'))
5049 Decimal('0E-1007')
5050 >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10'))
5051 Decimal('-1.00000004')
5052 >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000'))
5053 Decimal('-0.00')
5055 Decimal('1E-1007')
5056 >>> c.next_toward(Decimal(0), 1)
5057 Decimal('1E-1007')
5058 >>> c.next_toward(0, Decimal(1))
5059 Decimal('1E-1007')
5070 >>> ExtendedContext.normalize(Decimal('2.1'))
5071 Decimal('2.1')
5072 >>> ExtendedContext.normalize(Decimal('-2.0'))
5073 Decimal('-2')
5074 >>> ExtendedContext.normalize(Decimal('1.200'))
5075 Decimal('1.2')
5076 >>> ExtendedContext.normalize(Decimal('-120'))
5077 Decimal('-1.2E+2')
5078 >>> ExtendedContext.normalize(Decimal('120.00'))
5079 Decimal('1.2E+2')
5080 >>> ExtendedContext.normalize(Decimal('0.00'))
5081 Decimal('0')
5083 Decimal('6')
5106 >>> c.number_class(Decimal('Infinity'))
5108 >>> c.number_class(Decimal('1E-10'))
5110 >>> c.number_class(Decimal('2.50'))
5112 >>> c.number_class(Decimal('0.1E-999'))
5114 >>> c.number_class(Decimal('0'))
5116 >>> c.number_class(Decimal('-0'))
5118 >>> c.number_class(Decimal('-0.1E-999'))
5120 >>> c.number_class(Decimal('-1E-10'))
5122 >>> c.number_class(Decimal('-2.50'))
5124 >>> c.number_class(Decimal('-Infinity'))
5126 >>> c.number_class(Decimal('NaN'))
5128 >>> c.number_class(Decimal('-NaN'))
5130 >>> c.number_class(Decimal('sNaN'))
5145 >>> ExtendedContext.plus(Decimal('1.3'))
5146 Decimal('1.3')
5147 >>> ExtendedContext.plus(Decimal('-1.3'))
5148 Decimal('-1.3')
5150 Decimal('-1')
5180 >>> c.power(Decimal('2'), Decimal('3'))
5181 Decimal('8')
5182 >>> c.power(Decimal('-2'), Decimal('3'))
5183 Decimal('-8')
5184 >>> c.power(Decimal('2'), Decimal('-3'))
5185 Decimal('0.125')
5186 >>> c.power(Decimal('1.7'), Decimal('8'))
5187 Decimal('69.7575744')
5188 >>> c.power(Decimal('10'), Decimal('0.301029996'))
5189 Decimal('2.00000000')
5190 >>> c.power(Decimal('Infinity'), Decimal('-1'))
5191 Decimal('0')
5192 >>> c.power(Decimal('Infinity'), Decimal('0'))
5193 Decimal('1')
5194 >>> c.power(Decimal('Infinity'), Decimal('1'))
5195 Decimal('Infinity')
5196 >>> c.power(Decimal('-Infinity'), Decimal('-1'))
5197 Decimal('-0')
5198 >>> c.power(Decimal('-Infinity'), Decimal('0'))
5199 Decimal('1')
5200 >>> c.power(Decimal('-Infinity'), Decimal('1'))
5201 Decimal('-Infinity')
5202 >>> c.power(Decimal('-Infinity'), Decimal('2'))
5203 Decimal('Infinity')
5204 >>> c.power(Decimal('0'), Decimal('0'))
5205 Decimal('NaN')
5207 >>> c.power(Decimal('3'), Decimal('7'), Decimal('16'))
5208 Decimal('11')
5209 >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16'))
5210 Decimal('-11')
5211 >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16'))
5212 Decimal('1')
5213 >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16'))
5214 Decimal('11')
5215 >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789'))
5216 Decimal('11729830')
5217 >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729'))
5218 Decimal('-0')
5219 >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537'))
5220 Decimal('1')
5222 Decimal('823543')
5223 >>> ExtendedContext.power(Decimal(7), 7)
5224 Decimal('823543')
5225 >>> ExtendedContext.power(7, Decimal(7), 2)
5226 Decimal('1')
5231 raise TypeError("Unable to convert %s to Decimal" % b)
5253 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
5254 Decimal('2.170')
5255 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
5256 Decimal('2.17')
5257 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
5258 Decimal('2.2')
5259 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
5260 Decimal('2')
5261 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
5262 Decimal('0E+1')
5263 >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
5264 Decimal('-Infinity')
5265 >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
5266 Decimal('NaN')
5267 >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
5268 Decimal('-0')
5269 >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
5270 Decimal('-0E+5')
5271 >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
5272 Decimal('NaN')
5273 >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
5274 Decimal('NaN')
5275 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
5276 Decimal('217.0')
5277 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
5278 Decimal('217')
5279 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
5280 Decimal('2.2E+2')
5281 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
5282 Decimal('2E+2')
5284 Decimal('1')
5285 >>> ExtendedContext.quantize(Decimal(1), 2)
5286 Decimal('1')
5287 >>> ExtendedContext.quantize(1, Decimal(2))
5288 Decimal('1')
5294 """Just returns 10, as this is Decimal, :)
5297 Decimal('10')
5299 return Decimal(10)
5313 >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
5314 Decimal('2.1')
5315 >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
5316 Decimal('1')
5317 >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
5318 Decimal('-1')
5319 >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
5320 Decimal('0.2')
5321 >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
5322 Decimal('0.1')
5323 >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
5324 Decimal('1.0')
5326 Decimal('4')
5327 >>> ExtendedContext.remainder(Decimal(22), 6)
5328 Decimal('4')
5329 >>> ExtendedContext.remainder(22, Decimal(6))
5330 Decimal('4')
5335 raise TypeError("Unable to convert %s to Decimal" % b)
5349 >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
5350 Decimal('-0.9')
5351 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
5352 Decimal('-2')
5353 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
5354 Decimal('1')
5355 >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
5356 Decimal('-1')
5357 >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
5358 Decimal('0.2')
5359 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
5360 Decimal('0.1')
5361 >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
5362 Decimal('-0.3')
5364 Decimal('3')
5365 >>> ExtendedContext.remainder_near(Decimal(3), 11)
5366 Decimal('3')
5367 >>> ExtendedContext.remainder_near(3, Decimal(11))
5368 Decimal('3')
5382 >>> ExtendedContext.rotate(Decimal('34'), Decimal('8'))
5383 Decimal('400000003')
5384 >>> ExtendedContext.rotate(Decimal('12'), Decimal('9'))
5385 Decimal('12')
5386 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2'))
5387 Decimal('891234567')
5388 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0'))
5389 Decimal('123456789')
5390 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2'))
5391 Decimal('345678912')
5393 Decimal('13333330')
5394 >>> ExtendedContext.rotate(Decimal(1333333), 1)
5395 Decimal('13333330')
5396 >>> ExtendedContext.rotate(1333333, Decimal(1))
5397 Decimal('13333330')
5408 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
5410 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
5412 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
5414 >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
5418 >>> ExtendedContext.same_quantum(Decimal(10000), -1)
5420 >>> ExtendedContext.same_quantum(10000, Decimal(-1))
5429 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2'))
5430 Decimal('0.0750')
5431 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0'))
5432 Decimal('7.50')
5433 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3'))
5434 Decimal('7.50E+3')
5436 Decimal('1E+4')
5437 >>> ExtendedContext.scaleb(Decimal(1), 4)
5438 Decimal('1E+4')
5439 >>> ExtendedContext.scaleb(1, Decimal(4))
5440 Decimal('1E+4')
5455 >>> ExtendedContext.shift(Decimal('34'), Decimal('8'))
5456 Decimal('400000000')
5457 >>> ExtendedContext.shift(Decimal('12'), Decimal('9'))
5458 Decimal('0')
5459 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2'))
5460 Decimal('1234567')
5461 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0'))
5462 Decimal('123456789')
5463 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2'))
5464 Decimal('345678900')
5466 Decimal('888888800')
5467 >>> ExtendedContext.shift(Decimal(88888888), 2)
5468 Decimal('888888800')
5469 >>> ExtendedContext.shift(88888888, Decimal(2))
5470 Decimal('888888800')
5481 >>> ExtendedContext.sqrt(Decimal('0'))
5482 Decimal('0')
5483 >>> ExtendedContext.sqrt(Decimal('-0'))
5484 Decimal('-0')
5485 >>> ExtendedContext.sqrt(Decimal('0.39'))
5486 Decimal('0.624499800')
5487 >>> ExtendedContext.sqrt(Decimal('100'))
5488 Decimal('10')
5489 >>> ExtendedContext.sqrt(Decimal('1'))
5490 Decimal('1')
5491 >>> ExtendedContext.sqrt(Decimal('1.0'))
5492 Decimal('1.0')
5493 >>> ExtendedContext.sqrt(Decimal('1.00'))
5494 Decimal('1.0')
5495 >>> ExtendedContext.sqrt(Decimal('7'))
5496 Decimal('2.64575131')
5497 >>> ExtendedContext.sqrt(Decimal('10'))
5498 Decimal('3.16227766')
5500 Decimal('1.41421356')
5510 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
5511 Decimal('0.23')
5512 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
5513 Decimal('0.00')
5514 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
5515 Decimal('-0.77')
5517 Decimal('3')
5518 >>> ExtendedContext.subtract(Decimal(8), 5)
5519 Decimal('3')
5520 >>> ExtendedContext.subtract(8, Decimal(5))
5521 Decimal('3')
5526 raise TypeError("Unable to convert %s to Decimal" % b)
5534 can leave up to 3 digits to the left of the decimal place and may
5539 >>> ExtendedContext.to_eng_string(Decimal('123E+1'))
5541 >>> ExtendedContext.to_eng_string(Decimal('123E+3'))
5543 >>> ExtendedContext.to_eng_string(Decimal('123E-10'))
5545 >>> ExtendedContext.to_eng_string(Decimal('-123E-12'))
5547 >>> ExtendedContext.to_eng_string(Decimal('7E-7'))
5549 >>> ExtendedContext.to_eng_string(Decimal('7E+1'))
5551 >>> ExtendedContext.to_eng_string(Decimal('0E+1'))
5576 >>> ExtendedContext.to_integral_exact(Decimal('2.1'))
5577 Decimal('2')
5578 >>> ExtendedContext.to_integral_exact(Decimal('100'))
5579 Decimal('100')
5580 >>> ExtendedContext.to_integral_exact(Decimal('100.0'))
5581 Decimal('100')
5582 >>> ExtendedContext.to_integral_exact(Decimal('101.5'))
5583 Decimal('102')
5584 >>> ExtendedContext.to_integral_exact(Decimal('-101.5'))
5585 Decimal('-102')
5586 >>> ExtendedContext.to_integral_exact(Decimal('10E+5'))
5587 Decimal('1.0E+6')
5588 >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77'))
5589 Decimal('7.89E+77')
5590 >>> ExtendedContext.to_integral_exact(Decimal('-Inf'))
5591 Decimal('-Infinity')
5605 >>> ExtendedContext.to_integral_value(Decimal('2.1'))
5606 Decimal('2')
5607 >>> ExtendedContext.to_integral_value(Decimal('100'))
5608 Decimal('100')
5609 >>> ExtendedContext.to_integral_value(Decimal('100.0'))
5610 Decimal('100')
5611 >>> ExtendedContext.to_integral_value(Decimal('101.5'))
5612 Decimal('102')
5613 >>> ExtendedContext.to_integral_value(Decimal('-101.5'))
5614 Decimal('-102')
5615 >>> ExtendedContext.to_integral_value(Decimal('10E+5'))
5616 Decimal('1.0E+6')
5617 >>> ExtendedContext.to_integral_value(Decimal('7.89E+77'))
5618 Decimal('7.89E+77')
5619 >>> ExtendedContext.to_integral_value(Decimal('-Inf'))
5620 Decimal('-Infinity')
5639 elif isinstance(value, Decimal):
5867 Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__."""
5939 """Compute an approximation to exp(c*10**e), with p decimal places of
5992 # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point
6028 """Convert other to Decimal.
6035 if isinstance(other, Decimal):
6038 return Decimal(other)
6040 return Decimal.from_float(other)
6043 raise TypeError("Unable to convert %s to Decimal" % other)
6047 """Given a Decimal instance self and a Python object other, return
6048 a pair (s, o) of Decimal instances such that "s op o" is
6053 if isinstance(other, Decimal):
6065 return self, Decimal(other.numerator)
6079 return self, Decimal.from_float(other)
6127 # at least one decimal digit, possibly after the decimal point. The
6154 # The functions in this section have little to do with the Decimal
6158 # A format specifier for Decimal looks like:
6202 decimal_point: string to use for decimal point
6247 # determine thousands separator, grouping, and decimal separator, and
6369 intpart: string of digits that must appear before the decimal point
6375 insert separators (decimal separator and thousands separators)
6406 _Infinity = Decimal('Inf')
6407 _NegativeInfinity = Decimal('-Inf')
6408 _NaN = Decimal('NaN')
6409 _Zero = Decimal(0)
6410 _One = Decimal(1)
6411 _NegativeOne = Decimal(-1)