Lines Matching full:decimal
21 This is a Py2.3 implementation of decimal floating point arithmetic based on
22 the General Decimal Arithmetic Specification:
24 http://speleotrove.com/decimal/decarith.html
30 Decimal floating point has finite precision with arbitrarily large bounds.
38 of the expected Decimal('0.00') returned by decimal floating point).
40 Here are some examples of using the decimal module:
42 >>> from decimal import *
44 >>> Decimal(0)
45 Decimal('0')
46 >>> Decimal('1')
47 Decimal('1')
48 >>> Decimal('-.0123')
49 Decimal('-0.0123')
50 >>> Decimal(123456)
51 Decimal('123456')
52 >>> Decimal('123.45e12345678901234567890')
53 Decimal('1.2345E+12345678901234567892')
54 >>> Decimal('1.33') + Decimal('1.27')
55 Decimal('2.60')
56 >>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41')
57 Decimal('-2.20')
58 >>> dig = Decimal(1)
59 >>> print dig / Decimal(3)
62 >>> print dig / Decimal(3)
66 >>> print Decimal(3).sqrt()
68 >>> print Decimal(3) ** 123
70 >>> inf = Decimal(1) / Decimal(0)
73 >>> neginf = Decimal(-1) / Decimal(0)
93 >>> c.divide(Decimal(0), Decimal(0))
94 Decimal('NaN')
101 >>> print c.divide(Decimal(0), Decimal(0))
111 >>> print c.divide(Decimal(0), Decimal(0))
120 'Decimal', 'Context',
467 The returned context manager creates a local decimal context
480 # General Decimal Arithmetic Specification
503 ##### Decimal class #######################################################
505 class Decimal(object): class
506 """Floating point class for decimal arithmetic."""
509 # Generally, the value of the Decimal instance is given by
515 """Create a decimal point instance.
517 >>> Decimal('3.14') # string input
518 Decimal('3.14')
519 >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
520 Decimal('3.14')
521 >>> Decimal(314) # int or long
522 Decimal('314')
523 >>> Decimal(Decimal(314)) # another decimal instance
524 Decimal('314')
525 >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
526 Decimal('3.14')
534 # and the Decimal constructor still deal with tuples of
547 "Invalid literal for Decimal: %r" % value)
588 # From another decimal
589 if isinstance(value, Decimal):
607 raise ValueError('Invalid tuple size in creation of Decimal '
650 value = Decimal.from_float(value)
657 raise TypeError("Cannot convert %r to Decimal" % value)
662 """Converts a float to a decimal number, exactly.
664 Note that Decimal.from_float(0.1) is not the same as Decimal('0.1').
667 0x1.999999999999ap-4. The exact equivalent of the value in decimal
670 >>> Decimal.from_float(0.1)
671 Decimal('0.1000000000000000055511151231257827021181583404541015625')
672 >>> Decimal.from_float(float('nan'))
673 Decimal('NaN')
674 >>> Decimal.from_float(float('inf'))
675 Decimal('Infinity')
676 >>> Decimal.from_float(-float('inf'))
677 Decimal('-Infinity')
678 >>> Decimal.from_float(-0.0)
679 Decimal('-0')
693 if cls is Decimal:
800 """Compare the two non-NaN decimal instances self and other.
815 # check for zeros; Decimal('0') == Decimal('-0')
846 # Note: The Decimal standard doesn't cover rich comparisons for
922 Like __cmp__, but returns Decimal instances.
932 return Decimal(self._cmp(other))
936 # Decimal integers must hash the same as the ints
938 # The hash of a nonspecial noninteger Decimal must depend only
939 # on the value of that Decimal, and not on its representation.
940 # For example: hash(Decimal('100E-1')) == hash(Decimal('10')).
963 # a Decimal instance is exactly representable as a float then
966 if Decimal.from_float(self_as_float) == self:
978 # The value of a nonzero nonspecial Decimal instance is
994 """Represents the number as an instance of Decimal."""
996 return "Decimal('%s')" % str(self)
1013 # number of digits of self._int to left of decimal point
1017 # decimal point in the mantissa of the output string (that is,
1054 can leave up to 3 digits to the left of the decimal place and may
1073 # -Decimal('0') is Decimal('0'), not Decimal('-0'), except
1098 ans = Decimal(self)
1145 return Decimal(self)
1147 return Decimal(other) # Can't both be infinity here
1206 ans = Decimal(result)
1499 ans = Decimal(self)
1610 return Decimal(0)
1636 return Decimal(self)
1644 self - Decimal instance
1654 return Decimal(self)
1667 return Decimal(self)
1728 return Decimal(self)
1731 # self is a finite, nonzero Decimal
1854 # if can't convert other and modulo to Decimal, raise
1899 # additional restriction for decimal: the modulus must be less
1922 # Decimal integers (i.e. force their exponents to be >= 0)
2439 return Decimal(self) # if both are inf, it is OK
2518 return Decimal(self)
2541 """Round a nonzero, nonspecial Decimal to a fixed number of
2553 return Decimal(self)
2577 return Decimal(self)
2579 return Decimal(self)
2602 return Decimal(self)
2604 return Decimal(self)
2622 return Decimal(self)
2637 # <= sqrt(c) < 10**p, so the closest representable Decimal at
2810 """Returns the same Decimal object.
2951 return Decimal(self)
3008 Currently, the encoding of a Decimal instance is always
3009 canonical, so this method returns True for any Decimal.
3016 A Decimal instance is considered finite if it is neither
3193 ans = Decimal(self._exp + len(self._int) - 1)
3242 # Decimal. Note that no attempt is made to fit the result
3244 ans = Decimal(self.adjusted())
3527 """Just returns 10, as this is Decimal, :)"""
3528 return Decimal(10)
3547 return Decimal(self)
3582 return Decimal(self)
3605 return Decimal(self)
3631 if type(self) is Decimal:
3636 if type(self) is Decimal:
3643 """Format a Decimal instance according to the given specifier.
3653 # there should be at least one digit after the decimal point.
3655 # Decimal---it's presumably there to make sure that
3693 # figure out placement of the decimal point
3708 # find digits before and after decimal point, and get exponent
3720 # done with the decimal-specific stuff; hand over the rest
3725 """Create a decimal instance directly, without any validation,
3732 self = object.__new__(Decimal)
3740 # Register Decimal as a kind of Number (an abstract base class).
3743 _numbers.Number.register(Decimal)
3752 the previous decimal context in __exit__()
3764 """Contains the context for a Decimal instance.
3774 Should be reset by user of Decimal instance.
3923 """Creates a new Decimal instance but using self as context.
3926 IBM Decimal specification."""
3933 d = Decimal(num, context=self)
3940 """Creates a new Decimal instance from a float but rounding using self
3945 Decimal('3.1415')
3953 d = Decimal.from_float(f) # An exact conversion
3964 >>> ExtendedContext.abs(Decimal('2.1'))
3965 Decimal('2.1')
3966 >>> ExtendedContext.abs(Decimal('-100'))
3967 Decimal('100')
3968 >>> ExtendedContext.abs(Decimal('101.5'))
3969 Decimal('101.5')
3970 >>> ExtendedContext.abs(Decimal('-101.5'))
3971 Decimal('101.5')
3973 Decimal('1')
3981 >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
3982 Decimal('19.00')
3983 >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
3984 Decimal('1.02E+4')
3985 >>> ExtendedContext.add(1, Decimal(2))
3986 Decimal('3')
3987 >>> ExtendedContext.add(Decimal(8), 5)
3988 Decimal('13')
3990 Decimal('10')
3995 raise TypeError("Unable to convert %s to Decimal" % b)
4003 """Returns the same Decimal object.
4008 >>> ExtendedContext.canonical(Decimal('2.50'))
4009 Decimal('2.50')
4027 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
4028 Decimal('-1')
4029 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
4030 Decimal('0')
4031 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
4032 Decimal('0')
4033 >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
4034 Decimal('1')
4035 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
4036 Decimal('1')
4037 >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
4038 Decimal('-1')
4040 Decimal('-1')
4041 >>> ExtendedContext.compare(Decimal(1), 2)
4042 Decimal('-1')
4043 >>> ExtendedContext.compare(1, Decimal(2))
4044 Decimal('-1')
4056 >>> c.compare_signal(Decimal('2.1'), Decimal('3'))
4057 Decimal('-1')
4058 >>> c.compare_signal(Decimal('2.1'), Decimal('2.1'))
4059 Decimal('0')
4063 >>> c.compare_signal(Decimal('NaN'), Decimal('2.1'))
4064 Decimal('NaN')
4070 >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1'))
4071 Decimal('NaN')
4075 Decimal('-1')
4076 >>> c.compare_signal(Decimal(-1), 2)
4077 Decimal('-1')
4078 >>> c.compare_signal(-1, Decimal(2))
4079 Decimal('-1')
4091 >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9'))
4092 Decimal('-1')
4093 >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12'))
4094 Decimal('-1')
4095 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3'))
4096 Decimal('-1')
4097 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30'))
4098 Decimal('0')
4099 >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300'))
4100 Decimal('1')
4101 >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN'))
4102 Decimal('-1')
4104 Decimal('-1')
4105 >>> ExtendedContext.compare_total(Decimal(1), 2)
4106 Decimal('-1')
4107 >>> ExtendedContext.compare_total(1, Decimal(2))
4108 Decimal('-1')
4124 >>> ExtendedContext.copy_abs(Decimal('2.1'))
4125 Decimal('2.1')
4126 >>> ExtendedContext.copy_abs(Decimal('-100'))
4127 Decimal('100')
4129 Decimal('1')
4135 """Returns a copy of the decimal object.
4137 >>> ExtendedContext.copy_decimal(Decimal('2.1'))
4138 Decimal('2.1')
4139 >>> ExtendedContext.copy_decimal(Decimal('-1.00'))
4140 Decimal('-1.00')
4142 Decimal('1')
4145 return Decimal(a)
4150 >>> ExtendedContext.copy_negate(Decimal('101.5'))
4151 Decimal('-101.5')
4152 >>> ExtendedContext.copy_negate(Decimal('-101.5'))
4153 Decimal('101.5')
4155 Decimal('-1')
4166 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33'))
4167 Decimal('1.50')
4168 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33'))
4169 Decimal('1.50')
4170 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33'))
4171 Decimal('-1.50')
4172 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33'))
4173 Decimal('-1.50')
4175 Decimal('-1')
4176 >>> ExtendedContext.copy_sign(Decimal(1), -2)
4177 Decimal('-1')
4178 >>> ExtendedContext.copy_sign(1, Decimal(-2))
4179 Decimal('-1')
4185 """Decimal division in a specified context.
4187 >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
4188 Decimal('0.333333333')
4189 >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
4190 Decimal('0.666666667')
4191 >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
4192 Decimal('2.5')
4193 >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
4194 Decimal('0.1')
4195 >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
4196 Decimal('1')
4197 >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
4198 Decimal('4.00')
4199 >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
4200 Decimal('1.20')
4201 >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
4202 Decimal('10')
4203 >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
4204 Decimal('1000')
4205 >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
4206 Decimal('1.20E+6')
4208 Decimal('1')
4209 >>> ExtendedContext.divide(Decimal(5), 5)
4210 Decimal('1')
4211 >>> ExtendedContext.divide(5, Decimal(5))
4212 Decimal('1')
4217 raise TypeError("Unable to convert %s to Decimal" % b)
4224 >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
4225 Decimal('0')
4226 >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
4227 Decimal('3')
4228 >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
4229 Decimal('3')
4231 Decimal('3')
4232 >>> ExtendedContext.divide_int(Decimal(10), 3)
4233 Decimal('3')
4234 >>> ExtendedContext.divide_int(10, Decimal(3))
4235 Decimal('3')
4240 raise TypeError("Unable to convert %s to Decimal" % b)
4247 >>> ExtendedContext.divmod(Decimal(8), Decimal(3))
4248 (Decimal('2'), Decimal('2'))
4249 >>> ExtendedContext.divmod(Decimal(8), Decimal(4))
4250 (Decimal('2'), Decimal('0'))
4252 (Decimal('2'), Decimal('0'))
4253 >>> ExtendedContext.divmod(Decimal(8), 4)
4254 (Decimal('2'), Decimal('0'))
4255 >>> ExtendedContext.divmod(8, Decimal(4))
4256 (Decimal('2'), Decimal('0'))
4261 raise TypeError("Unable to convert %s to Decimal" % b)
4271 >>> c.exp(Decimal('-Infinity'))
4272 Decimal('0')
4273 >>> c.exp(Decimal('-1'))
4274 Decimal('0.367879441')
4275 >>> c.exp(Decimal('0'))
4276 Decimal('1')
4277 >>> c.exp(Decimal('1'))
4278 Decimal('2.71828183')
4279 >>> c.exp(Decimal('0.693147181'))
4280 Decimal('2.00000000')
4281 >>> c.exp(Decimal('+Infinity'))
4282 Decimal('Infinity')
4284 Decimal('22026.4658')
4296 >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7'))
4297 Decimal('22')
4298 >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7'))
4299 Decimal('-8')
4300 >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578'))
4301 Decimal('1.38435736E+12')
4303 Decimal('7')
4304 >>> ExtendedContext.fma(1, Decimal(3), 4)
4305 Decimal('7')
4306 >>> ExtendedContext.fma(1, 3, Decimal(4))
4307 Decimal('7')
4315 Currently, the encoding of a Decimal instance is always
4316 canonical, so this method returns True for any Decimal.
4318 >>> ExtendedContext.is_canonical(Decimal('2.50'))
4326 A Decimal instance is considered finite if it is neither
4329 >>> ExtendedContext.is_finite(Decimal('2.50'))
4331 >>> ExtendedContext.is_finite(Decimal('-0.3'))
4333 >>> ExtendedContext.is_finite(Decimal('0'))
4335 >>> ExtendedContext.is_finite(Decimal('Inf'))
4337 >>> ExtendedContext.is_finite(Decimal('NaN'))
4348 >>> ExtendedContext.is_infinite(Decimal('2.50'))
4350 >>> ExtendedContext.is_infinite(Decimal('-Inf'))
4352 >>> ExtendedContext.is_infinite(Decimal('NaN'))
4364 >>> ExtendedContext.is_nan(Decimal('2.50'))
4366 >>> ExtendedContext.is_nan(Decimal('NaN'))
4368 >>> ExtendedContext.is_nan(Decimal('-sNaN'))
4383 >>> c.is_normal(Decimal('2.50'))
4385 >>> c.is_normal(Decimal('0.1E-999'))
4387 >>> c.is_normal(Decimal('0.00'))
4389 >>> c.is_normal(Decimal('-Inf'))
4391 >>> c.is_normal(Decimal('NaN'))
4402 >>> ExtendedContext.is_qnan(Decimal('2.50'))
4404 >>> ExtendedContext.is_qnan(Decimal('NaN'))
4406 >>> ExtendedContext.is_qnan(Decimal('sNaN'))
4417 >>> ExtendedContext.is_signed(Decimal('2.50'))
4419 >>> ExtendedContext.is_signed(Decimal('-12'))
4421 >>> ExtendedContext.is_signed(Decimal('-0'))
4435 >>> ExtendedContext.is_snan(Decimal('2.50'))
4437 >>> ExtendedContext.is_snan(Decimal('NaN'))
4439 >>> ExtendedContext.is_snan(Decimal('sNaN'))
4453 >>> c.is_subnormal(Decimal('2.50'))
4455 >>> c.is_subnormal(Decimal('0.1E-999'))
4457 >>> c.is_subnormal(Decimal('0.00'))
4459 >>> c.is_subnormal(Decimal('-Inf'))
4461 >>> c.is_subnormal(Decimal('NaN'))
4472 >>> ExtendedContext.is_zero(Decimal('0'))
4474 >>> ExtendedContext.is_zero(Decimal('2.50'))
4476 >>> ExtendedContext.is_zero(Decimal('-0E+2'))
4492 >>> c.ln(Decimal('0'))
4493 Decimal('-Infinity')
4494 >>> c.ln(Decimal('1.000'))
4495 Decimal('0')
4496 >>> c.ln(Decimal('2.71828183'))
4497 Decimal('1.00000000')
4498 >>> c.ln(Decimal('10'))
4499 Decimal('2.30258509')
4500 >>> c.ln(Decimal('+Infinity'))
4501 Decimal('Infinity')
4503 Decimal('0')
4514 >>> c.log10(Decimal('0'))
4515 Decimal('-Infinity')
4516 >>> c.log10(Decimal('0.001'))
4517 Decimal('-3')
4518 >>> c.log10(Decimal('1.000'))
4519 Decimal('0')
4520 >>> c.log10(Decimal('2'))
4521 Decimal('0.301029996')
4522 >>> c.log10(Decimal('10'))
4523 Decimal('1')
4524 >>> c.log10(Decimal('70'))
4525 Decimal('1.84509804')
4526 >>> c.log10(Decimal('+Infinity'))
4527 Decimal('Infinity')
4529 Decimal('-Infinity')
4531 Decimal('0')
4544 >>> ExtendedContext.logb(Decimal('250'))
4545 Decimal('2')
4546 >>> ExtendedContext.logb(Decimal('2.50'))
4547 Decimal('0')
4548 >>> ExtendedContext.logb(Decimal('0.03'))
4549 Decimal('-2')
4550 >>> ExtendedContext.logb(Decimal('0'))
4551 Decimal('-Infinity')
4553 Decimal('0')
4555 Decimal('1')
4557 Decimal('2')
4567 >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
4568 Decimal('0')
4569 >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1'))
4570 Decimal('0')
4571 >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0'))
4572 Decimal('0')
4573 >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1'))
4574 Decimal('1')
4575 >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010'))
4576 Decimal('1000')
4577 >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10'))
4578 Decimal('10')
4580 Decimal('100')
4581 >>> ExtendedContext.logical_and(Decimal(110), 1101)
4582 Decimal('100')
4583 >>> ExtendedContext.logical_and(110, Decimal(1101))
4584 Decimal('100')
4594 >>> ExtendedContext.logical_invert(Decimal('0'))
4595 Decimal('111111111')
4596 >>> ExtendedContext.logical_invert(Decimal('1'))
4597 Decimal('111111110')
4598 >>> ExtendedContext.logical_invert(Decimal('111111111'))
4599 Decimal('0')
4600 >>> ExtendedContext.logical_invert(Decimal('101010101'))
4601 Decimal('10101010')
4603 Decimal('111110010')
4613 >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
4614 Decimal('0')
4615 >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1'))
4616 Decimal('1')
4617 >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0'))
4618 Decimal('1')
4619 >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1'))
4620 Decimal('1')
4621 >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010'))
4622 Decimal('1110')
4623 >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10'))
4624 Decimal('1110')
4626 Decimal('1111')
4627 >>> ExtendedContext.logical_or(Decimal(110), 1101)
4628 Decimal('1111')
4629 >>> ExtendedContext.logical_or(110, Decimal(1101))
4630 Decimal('1111')
4640 >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
4641 Decimal('0')
4642 >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1'))
4643 Decimal('1')
4644 >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0'))
4645 Decimal('1')
4646 >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1'))
4647 Decimal('0')
4648 >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010'))
4649 Decimal('110')
4650 >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10'))
4651 Decimal('1101')
4653 Decimal('1011')
4654 >>> ExtendedContext.logical_xor(Decimal(110), 1101)
4655 Decimal('1011')
4656 >>> ExtendedContext.logical_xor(110, Decimal(1101))
4657 Decimal('1011')
4671 >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
4672 Decimal('3')
4673 >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
4674 Decimal('3')
4675 >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
4676 Decimal('1')
4677 >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
4678 Decimal('7')
4680 Decimal('2')
4681 >>> ExtendedContext.max(Decimal(1), 2)
4682 Decimal('2')
4683 >>> ExtendedContext.max(1, Decimal(2))
4684 Decimal('2')
4692 >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN'))
4693 Decimal('7')
4694 >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10'))
4695 Decimal('-10')
4697 Decimal('-2')
4698 >>> ExtendedContext.max_mag(Decimal(1), -2)
4699 Decimal('-2')
4700 >>> ExtendedContext.max_mag(1, Decimal(-2))
4701 Decimal('-2')
4715 >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
4716 Decimal('2')
4717 >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
4718 Decimal('-10')
4719 >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
4720 Decimal('1.0')
4721 >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
4722 Decimal('7')
4724 Decimal('1')
4725 >>> ExtendedContext.min(Decimal(1), 2)
4726 Decimal('1')
4727 >>> ExtendedContext.min(1, Decimal(29))
4728 Decimal('1')
4736 >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2'))
4737 Decimal('-2')
4738 >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN'))
4739 Decimal('-3')
4741 Decimal('1')
4742 >>> ExtendedContext.min_mag(Decimal(1), -2)
4743 Decimal('1')
4744 >>> ExtendedContext.min_mag(1, Decimal(-2))
4745 Decimal('1')
4757 >>> ExtendedContext.minus(Decimal('1.3'))
4758 Decimal('-1.3')
4759 >>> ExtendedContext.minus(Decimal('-1.3'))
4760 Decimal('1.3')
4762 Decimal('-1')
4775 >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
4776 Decimal('3.60')
4777 >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
4778 Decimal('21')
4779 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
4780 Decimal('0.72')
4781 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
4782 Decimal('-0.0')
4783 >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
4784 Decimal('4.28135971E+11')
4786 Decimal('49')
4787 >>> ExtendedContext.multiply(Decimal(7), 7)
4788 Decimal('49')
4789 >>> ExtendedContext.multiply(7, Decimal(7))
4790 Decimal('49')
4795 raise TypeError("Unable to convert %s to Decimal" % b)
4805 >>> ExtendedContext.next_minus(Decimal('1'))
4806 Decimal('0.999999999')
4807 >>> c.next_minus(Decimal('1E-1007'))
4808 Decimal('0E-1007')
4809 >>> ExtendedContext.next_minus(Decimal('-1.00000003'))
4810 Decimal('-1.00000004')
4811 >>> c.next_minus(Decimal('Infinity'))
4812 Decimal('9.99999999E+999')
4814 Decimal('0.999999999')
4825 >>> ExtendedContext.next_plus(Decimal('1'))
4826 Decimal('1.00000001')
4827 >>> c.next_plus(Decimal('-1E-1007'))
4828 Decimal('-0E-1007')
4829 >>> ExtendedContext.next_plus(Decimal('-1.00000003'))
4830 Decimal('-1.00000002')
4831 >>> c.next_plus(Decimal('-Infinity'))
4832 Decimal('-9.99999999E+999')
4834 Decimal('1.00000001')
4850 >>> c.next_toward(Decimal('1'), Decimal('2'))
4851 Decimal('1.00000001')
4852 >>> c.next_toward(Decimal('-1E-1007'), Decimal('1'))
4853 Decimal('-0E-1007')
4854 >>> c.next_toward(Decimal('-1.00000003'), Decimal('0'))
4855 Decimal('-1.00000002')
4856 >>> c.next_toward(Decimal('1'), Decimal('0'))
4857 Decimal('0.999999999')
4858 >>> c.next_toward(Decimal('1E-1007'), Decimal('-100'))
4859 Decimal('0E-1007')
4860 >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10'))
4861 Decimal('-1.00000004')
4862 >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000'))
4863 Decimal('-0.00')
4865 Decimal('1E-1007')
4866 >>> c.next_toward(Decimal(0), 1)
4867 Decimal('1E-1007')
4868 >>> c.next_toward(0, Decimal(1))
4869 Decimal('1E-1007')
4880 >>> ExtendedContext.normalize(Decimal('2.1'))
4881 Decimal('2.1')
4882 >>> ExtendedContext.normalize(Decimal('-2.0'))
4883 Decimal('-2')
4884 >>> ExtendedContext.normalize(Decimal('1.200'))
4885 Decimal('1.2')
4886 >>> ExtendedContext.normalize(Decimal('-120'))
4887 Decimal('-1.2E+2')
4888 >>> ExtendedContext.normalize(Decimal('120.00'))
4889 Decimal('1.2E+2')
4890 >>> ExtendedContext.normalize(Decimal('0.00'))
4891 Decimal('0')
4893 Decimal('6')
4916 >>> c.number_class(Decimal('Infinity'))
4918 >>> c.number_class(Decimal('1E-10'))
4920 >>> c.number_class(Decimal('2.50'))
4922 >>> c.number_class(Decimal('0.1E-999'))
4924 >>> c.number_class(Decimal('0'))
4926 >>> c.number_class(Decimal('-0'))
4928 >>> c.number_class(Decimal('-0.1E-999'))
4930 >>> c.number_class(Decimal('-1E-10'))
4932 >>> c.number_class(Decimal('-2.50'))
4934 >>> c.number_class(Decimal('-Infinity'))
4936 >>> c.number_class(Decimal('NaN'))
4938 >>> c.number_class(Decimal('-NaN'))
4940 >>> c.number_class(Decimal('sNaN'))
4955 >>> ExtendedContext.plus(Decimal('1.3'))
4956 Decimal('1.3')
4957 >>> ExtendedContext.plus(Decimal('-1.3'))
4958 Decimal('-1.3')
4960 Decimal('-1')
4990 >>> c.power(Decimal('2'), Decimal('3'))
4991 Decimal('8')
4992 >>> c.power(Decimal('-2'), Decimal('3'))
4993 Decimal('-8')
4994 >>> c.power(Decimal('2'), Decimal('-3'))
4995 Decimal('0.125')
4996 >>> c.power(Decimal('1.7'), Decimal('8'))
4997 Decimal('69.7575744')
4998 >>> c.power(Decimal('10'), Decimal('0.301029996'))
4999 Decimal('2.00000000')
5000 >>> c.power(Decimal('Infinity'), Decimal('-1'))
5001 Decimal('0')
5002 >>> c.power(Decimal('Infinity'), Decimal('0'))
5003 Decimal('1')
5004 >>> c.power(Decimal('Infinity'), Decimal('1'))
5005 Decimal('Infinity')
5006 >>> c.power(Decimal('-Infinity'), Decimal('-1'))
5007 Decimal('-0')
5008 >>> c.power(Decimal('-Infinity'), Decimal('0'))
5009 Decimal('1')
5010 >>> c.power(Decimal('-Infinity'), Decimal('1'))
5011 Decimal('-Infinity')
5012 >>> c.power(Decimal('-Infinity'), Decimal('2'))
5013 Decimal('Infinity')
5014 >>> c.power(Decimal('0'), Decimal('0'))
5015 Decimal('NaN')
5017 >>> c.power(Decimal('3'), Decimal('7'), Decimal('16'))
5018 Decimal('11')
5019 >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16'))
5020 Decimal('-11')
5021 >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16'))
5022 Decimal('1')
5023 >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16'))
5024 Decimal('11')
5025 >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789'))
5026 Decimal('11729830')
5027 >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729'))
5028 Decimal('-0')
5029 >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537'))
5030 Decimal('1')
5032 Decimal('823543')
5033 >>> ExtendedContext.power(Decimal(7), 7)
5034 Decimal('823543')
5035 >>> ExtendedContext.power(7, Decimal(7), 2)
5036 Decimal('1')
5041 raise TypeError("Unable to convert %s to Decimal" % b)
5063 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
5064 Decimal('2.170')
5065 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
5066 Decimal('2.17')
5067 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
5068 Decimal('2.2')
5069 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
5070 Decimal('2')
5071 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
5072 Decimal('0E+1')
5073 >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
5074 Decimal('-Infinity')
5075 >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
5076 Decimal('NaN')
5077 >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
5078 Decimal('-0')
5079 >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
5080 Decimal('-0E+5')
5081 >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
5082 Decimal('NaN')
5083 >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
5084 Decimal('NaN')
5085 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
5086 Decimal('217.0')
5087 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
5088 Decimal('217')
5089 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
5090 Decimal('2.2E+2')
5091 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
5092 Decimal('2E+2')
5094 Decimal('1')
5095 >>> ExtendedContext.quantize(Decimal(1), 2)
5096 Decimal('1')
5097 >>> ExtendedContext.quantize(1, Decimal(2))
5098 Decimal('1')
5104 """Just returns 10, as this is Decimal, :)
5107 Decimal('10')
5109 return Decimal(10)
5123 >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
5124 Decimal('2.1')
5125 >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
5126 Decimal('1')
5127 >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
5128 Decimal('-1')
5129 >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
5130 Decimal('0.2')
5131 >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
5132 Decimal('0.1')
5133 >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
5134 Decimal('1.0')
5136 Decimal('4')
5137 >>> ExtendedContext.remainder(Decimal(22), 6)
5138 Decimal('4')
5139 >>> ExtendedContext.remainder(22, Decimal(6))
5140 Decimal('4')
5145 raise TypeError("Unable to convert %s to Decimal" % b)
5159 >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
5160 Decimal('-0.9')
5161 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
5162 Decimal('-2')
5163 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
5164 Decimal('1')
5165 >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
5166 Decimal('-1')
5167 >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
5168 Decimal('0.2')
5169 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
5170 Decimal('0.1')
5171 >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
5172 Decimal('-0.3')
5174 Decimal('3')
5175 >>> ExtendedContext.remainder_near(Decimal(3), 11)
5176 Decimal('3')
5177 >>> ExtendedContext.remainder_near(3, Decimal(11))
5178 Decimal('3')
5192 >>> ExtendedContext.rotate(Decimal('34'), Decimal('8'))
5193 Decimal('400000003')
5194 >>> ExtendedContext.rotate(Decimal('12'), Decimal('9'))
5195 Decimal('12')
5196 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2'))
5197 Decimal('891234567')
5198 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0'))
5199 Decimal('123456789')
5200 >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2'))
5201 Decimal('345678912')
5203 Decimal('13333330')
5204 >>> ExtendedContext.rotate(Decimal(1333333), 1)
5205 Decimal('13333330')
5206 >>> ExtendedContext.rotate(1333333, Decimal(1))
5207 Decimal('13333330')
5218 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
5220 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
5222 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
5224 >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
5228 >>> ExtendedContext.same_quantum(Decimal(10000), -1)
5230 >>> ExtendedContext.same_quantum(10000, Decimal(-1))
5239 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2'))
5240 Decimal('0.0750')
5241 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0'))
5242 Decimal('7.50')
5243 >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3'))
5244 Decimal('7.50E+3')
5246 Decimal('1E+4')
5247 >>> ExtendedContext.scaleb(Decimal(1), 4)
5248 Decimal('1E+4')
5249 >>> ExtendedContext.scaleb(1, Decimal(4))
5250 Decimal('1E+4')
5265 >>> ExtendedContext.shift(Decimal('34'), Decimal('8'))
5266 Decimal('400000000')
5267 >>> ExtendedContext.shift(Decimal('12'), Decimal('9'))
5268 Decimal('0')
5269 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2'))
5270 Decimal('1234567')
5271 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0'))
5272 Decimal('123456789')
5273 >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2'))
5274 Decimal('345678900')
5276 Decimal('888888800')
5277 >>> ExtendedContext.shift(Decimal(88888888), 2)
5278 Decimal('888888800')
5279 >>> ExtendedContext.shift(88888888, Decimal(2))
5280 Decimal('888888800')
5291 >>> ExtendedContext.sqrt(Decimal('0'))
5292 Decimal('0')
5293 >>> ExtendedContext.sqrt(Decimal('-0'))
5294 Decimal('-0')
5295 >>> ExtendedContext.sqrt(Decimal('0.39'))
5296 Decimal('0.624499800')
5297 >>> ExtendedContext.sqrt(Decimal('100'))
5298 Decimal('10')
5299 >>> ExtendedContext.sqrt(Decimal('1'))
5300 Decimal('1')
5301 >>> ExtendedContext.sqrt(Decimal('1.0'))
5302 Decimal('1.0')
5303 >>> ExtendedContext.sqrt(Decimal('1.00'))
5304 Decimal('1.0')
5305 >>> ExtendedContext.sqrt(Decimal('7'))
5306 Decimal('2.64575131')
5307 >>> ExtendedContext.sqrt(Decimal('10'))
5308 Decimal('3.16227766')
5310 Decimal('1.41421356')
5320 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
5321 Decimal('0.23')
5322 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
5323 Decimal('0.00')
5324 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
5325 Decimal('-0.77')
5327 Decimal('3')
5328 >>> ExtendedContext.subtract(Decimal(8), 5)
5329 Decimal('3')
5330 >>> ExtendedContext.subtract(8, Decimal(5))
5331 Decimal('3')
5336 raise TypeError("Unable to convert %s to Decimal" % b)
5344 can leave up to 3 digits to the left of the decimal place and may
5349 >>> ExtendedContext.to_eng_string(Decimal('123E+1'))
5351 >>> ExtendedContext.to_eng_string(Decimal('123E+3'))
5353 >>> ExtendedContext.to_eng_string(Decimal('123E-10'))
5355 >>> ExtendedContext.to_eng_string(Decimal('-123E-12'))
5357 >>> ExtendedContext.to_eng_string(Decimal('7E-7'))
5359 >>> ExtendedContext.to_eng_string(Decimal('7E+1'))
5361 >>> ExtendedContext.to_eng_string(Decimal('0E+1'))
5386 >>> ExtendedContext.to_integral_exact(Decimal('2.1'))
5387 Decimal('2')
5388 >>> ExtendedContext.to_integral_exact(Decimal('100'))
5389 Decimal('100')
5390 >>> ExtendedContext.to_integral_exact(Decimal('100.0'))
5391 Decimal('100')
5392 >>> ExtendedContext.to_integral_exact(Decimal('101.5'))
5393 Decimal('102')
5394 >>> ExtendedContext.to_integral_exact(Decimal('-101.5'))
5395 Decimal('-102')
5396 >>> ExtendedContext.to_integral_exact(Decimal('10E+5'))
5397 Decimal('1.0E+6')
5398 >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77'))
5399 Decimal('7.89E+77')
5400 >>> ExtendedContext.to_integral_exact(Decimal('-Inf'))
5401 Decimal('-Infinity')
5415 >>> ExtendedContext.to_integral_value(Decimal('2.1'))
5416 Decimal('2')
5417 >>> ExtendedContext.to_integral_value(Decimal('100'))
5418 Decimal('100')
5419 >>> ExtendedContext.to_integral_value(Decimal('100.0'))
5420 Decimal('100')
5421 >>> ExtendedContext.to_integral_value(Decimal('101.5'))
5422 Decimal('102')
5423 >>> ExtendedContext.to_integral_value(Decimal('-101.5'))
5424 Decimal('-102')
5425 >>> ExtendedContext.to_integral_value(Decimal('10E+5'))
5426 Decimal('1.0E+6')
5427 >>> ExtendedContext.to_integral_value(Decimal('7.89E+77'))
5428 Decimal('7.89E+77')
5429 >>> ExtendedContext.to_integral_value(Decimal('-Inf'))
5430 Decimal('-Infinity')
5449 elif isinstance(value, Decimal):
5695 Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__."""
5767 """Compute an approximation to exp(c*10**e), with p decimal places of
5820 # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point
5856 """Convert other to Decimal.
5863 if isinstance(other, Decimal):
5866 return Decimal(other)
5868 return Decimal.from_float(other)
5871 raise TypeError("Unable to convert %s to Decimal" % other)
5917 # at least one decimal digit, possibly after the decimal point. The
5944 # The functions in this section have little to do with the Decimal
5948 # A format specifier for Decimal looks like:
5990 decimal_point: string to use for decimal point
6036 # determine thousands separator, grouping, and decimal separator, and
6170 intpart: string of digits that must appear before the decimal point
6176 insert separators (decimal separator and thousands separators)
6207 _Infinity = Decimal('Inf')
6208 _NegativeInfinity = Decimal('-Inf')
6209 _NaN = Decimal('NaN')
6210 _Zero = Decimal(0)
6211 _One = Decimal(1)
6212 _NegativeOne = Decimal(-1)