summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/abc.py15
-rw-r--r--Lib/decimal.py484
-rwxr-xr-xLib/fractions.py160
-rw-r--r--Lib/idlelib/NEWS.txt7
-rw-r--r--Lib/numbers.py9
-rw-r--r--Lib/test/test_abc.py10
-rw-r--r--Lib/test/test_decimal.py2
-rw-r--r--Lib/test/test_fractions.py421
8 files changed, 569 insertions, 539 deletions
diff --git a/Lib/abc.py b/Lib/abc.py
index 8fb61c603c..d86b6ea78e 100644
--- a/Lib/abc.py
+++ b/Lib/abc.py
@@ -162,8 +162,19 @@ class ABCMeta(type):
def __instancecheck__(cls, instance):
"""Override for isinstance(instance, cls)."""
- return any(cls.__subclasscheck__(c)
- for c in {instance.__class__, type(instance)})
+ # Inline the cache checking
+ subclass = instance.__class__
+ if subclass in cls._abc_cache:
+ return True
+ subtype = type(instance)
+ if subtype is subclass:
+ if (cls._abc_negative_cache_version ==
+ ABCMeta._abc_invalidation_counter and
+ subclass in cls._abc_negative_cache):
+ return False
+ # Fall back to the subclass check.
+ return cls.__subclasscheck__(subclass)
+ return any(cls.__subclasscheck__(c) for c in {subclass, subtype})
def __subclasscheck__(cls, subclass):
"""Override for issubclass(subclass, cls)."""
diff --git a/Lib/decimal.py b/Lib/decimal.py
index 2745065910..94a45684e8 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -35,26 +35,26 @@ issues associated with binary floating point. The package is especially
useful for financial applications or for contexts where users have
expectations that are at odds with binary floating point (for instance,
in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
-of the expected Decimal("0.00") returned by decimal floating point).
+of the expected Decimal('0.00') returned by decimal floating point).
Here are some examples of using the decimal module:
>>> from decimal import *
>>> setcontext(ExtendedContext)
>>> Decimal(0)
-Decimal("0")
->>> Decimal("1")
-Decimal("1")
->>> Decimal("-.0123")
-Decimal("-0.0123")
+Decimal('0')
+>>> Decimal('1')
+Decimal('1')
+>>> Decimal('-.0123')
+Decimal('-0.0123')
>>> Decimal(123456)
-Decimal("123456")
->>> Decimal("123.45e12345678901234567890")
-Decimal("1.2345E+12345678901234567892")
->>> Decimal("1.33") + Decimal("1.27")
-Decimal("2.60")
->>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41")
-Decimal("-2.20")
+Decimal('123456')
+>>> Decimal('123.45e12345678901234567890')
+Decimal('1.2345E+12345678901234567892')
+>>> Decimal('1.33') + Decimal('1.27')
+Decimal('2.60')
+>>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41')
+Decimal('-2.20')
>>> dig = Decimal(1)
>>> print(dig / Decimal(3))
0.333333333
@@ -91,7 +91,7 @@ decimal.DivisionByZero: x / 0
>>> print(c.flags[InvalidOperation])
0
>>> c.divide(Decimal(0), Decimal(0))
-Decimal("NaN")
+Decimal('NaN')
>>> c.traps[InvalidOperation] = 1
>>> print(c.flags[InvalidOperation])
1
@@ -517,15 +517,15 @@ class Decimal(_numbers.Real, _numbers.Inexact):
"""Create a decimal point instance.
>>> Decimal('3.14') # string input
- Decimal("3.14")
+ Decimal('3.14')
>>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
- Decimal("3.14")
+ Decimal('3.14')
>>> Decimal(314) # int
- Decimal("314")
+ Decimal('314')
>>> Decimal(Decimal(314)) # another decimal instance
- Decimal("314")
+ Decimal('314')
>>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
- Decimal("3.14")
+ Decimal('3.14')
"""
# Note that the coefficient, self._int, is actually stored as
@@ -885,7 +885,7 @@ class Decimal(_numbers.Real, _numbers.Inexact):
#
# The hash of a nonspecial noninteger Decimal must depend only
# on the value of that Decimal, and not on its representation.
- # For example: hash(Decimal("100E-1")) == hash(Decimal("10")).
+ # For example: hash(Decimal('100E-1')) == hash(Decimal('10')).
if self._is_special:
if self._isnan():
raise TypeError('Cannot hash a NaN value.')
@@ -919,7 +919,7 @@ class Decimal(_numbers.Real, _numbers.Inexact):
def __repr__(self):
"""Represents the number as an instance of Decimal."""
# Invariant: eval(repr(d)) == d
- return 'Decimal("%s")' % str(self)
+ return "Decimal('%s')" % str(self)
def __str__(self, eng=False, context=None):
"""Return string representation of the number in scientific notation.
@@ -3637,13 +3637,13 @@ class Context(object):
the plus operation on the operand.
>>> ExtendedContext.abs(Decimal('2.1'))
- Decimal("2.1")
+ Decimal('2.1')
>>> ExtendedContext.abs(Decimal('-100'))
- Decimal("100")
+ Decimal('100')
>>> ExtendedContext.abs(Decimal('101.5'))
- Decimal("101.5")
+ Decimal('101.5')
>>> ExtendedContext.abs(Decimal('-101.5'))
- Decimal("101.5")
+ Decimal('101.5')
"""
return a.__abs__(context=self)
@@ -3651,9 +3651,9 @@ class Context(object):
"""Return the sum of the two operands.
>>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
- Decimal("19.00")
+ Decimal('19.00')
>>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
- Decimal("1.02E+4")
+ Decimal('1.02E+4')
"""
return a.__add__(b, context=self)
@@ -3667,7 +3667,7 @@ class Context(object):
received object already is in its canonical form.
>>> ExtendedContext.canonical(Decimal('2.50'))
- Decimal("2.50")
+ Decimal('2.50')
"""
return a.canonical(context=self)
@@ -3686,17 +3686,17 @@ class Context(object):
zero or negative zero, or '1' if the result is greater than zero.
>>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
- Decimal("-1")
+ Decimal('-1')
>>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
- Decimal("-1")
+ Decimal('-1')
"""
return a.compare(b, context=self)
@@ -3708,21 +3708,21 @@ class Context(object):
>>> c = ExtendedContext
>>> c.compare_signal(Decimal('2.1'), Decimal('3'))
- Decimal("-1")
+ Decimal('-1')
>>> c.compare_signal(Decimal('2.1'), Decimal('2.1'))
- Decimal("0")
+ Decimal('0')
>>> c.flags[InvalidOperation] = 0
>>> print(c.flags[InvalidOperation])
0
>>> c.compare_signal(Decimal('NaN'), Decimal('2.1'))
- Decimal("NaN")
+ Decimal('NaN')
>>> print(c.flags[InvalidOperation])
1
>>> c.flags[InvalidOperation] = 0
>>> print(c.flags[InvalidOperation])
0
>>> c.compare_signal(Decimal('sNaN'), Decimal('2.1'))
- Decimal("NaN")
+ Decimal('NaN')
>>> print(c.flags[InvalidOperation])
1
"""
@@ -3736,17 +3736,17 @@ class Context(object):
representations.
>>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9'))
- Decimal("-1")
+ Decimal('-1')
>>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12'))
- Decimal("-1")
+ Decimal('-1')
>>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3'))
- Decimal("-1")
+ Decimal('-1')
>>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN'))
- Decimal("-1")
+ Decimal('-1')
"""
return a.compare_total(b)
@@ -3761,9 +3761,9 @@ class Context(object):
"""Returns a copy of the operand with the sign set to 0.
>>> ExtendedContext.copy_abs(Decimal('2.1'))
- Decimal("2.1")
+ Decimal('2.1')
>>> ExtendedContext.copy_abs(Decimal('-100'))
- Decimal("100")
+ Decimal('100')
"""
return a.copy_abs()
@@ -3771,9 +3771,9 @@ class Context(object):
"""Returns a copy of the decimal objet.
>>> ExtendedContext.copy_decimal(Decimal('2.1'))
- Decimal("2.1")
+ Decimal('2.1')
>>> ExtendedContext.copy_decimal(Decimal('-1.00'))
- Decimal("-1.00")
+ Decimal('-1.00')
"""
return Decimal(a)
@@ -3781,9 +3781,9 @@ class Context(object):
"""Returns a copy of the operand with the sign inverted.
>>> ExtendedContext.copy_negate(Decimal('101.5'))
- Decimal("-101.5")
+ Decimal('-101.5')
>>> ExtendedContext.copy_negate(Decimal('-101.5'))
- Decimal("101.5")
+ Decimal('101.5')
"""
return a.copy_negate()
@@ -3794,13 +3794,13 @@ class Context(object):
equal to the sign of the second operand.
>>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33'))
- Decimal("1.50")
+ Decimal('1.50')
>>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33'))
- Decimal("1.50")
+ Decimal('1.50')
>>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33'))
- Decimal("-1.50")
+ Decimal('-1.50')
>>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33'))
- Decimal("-1.50")
+ Decimal('-1.50')
"""
return a.copy_sign(b)
@@ -3808,25 +3808,25 @@ class Context(object):
"""Decimal division in a specified context.
>>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
- Decimal("0.333333333")
+ Decimal('0.333333333')
>>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
- Decimal("0.666666667")
+ Decimal('0.666666667')
>>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
- Decimal("2.5")
+ Decimal('2.5')
>>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
- Decimal("0.1")
+ Decimal('0.1')
>>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
- Decimal("4.00")
+ Decimal('4.00')
>>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
- Decimal("1.20")
+ Decimal('1.20')
>>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
- Decimal("10")
+ Decimal('10')
>>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
- Decimal("1000")
+ Decimal('1000')
>>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
- Decimal("1.20E+6")
+ Decimal('1.20E+6')
"""
return a.__truediv__(b, context=self)
@@ -3834,11 +3834,11 @@ class Context(object):
"""Divides two numbers and returns the integer part of the result.
>>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
- Decimal("3")
+ Decimal('3')
>>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
- Decimal("3")
+ Decimal('3')
"""
return a.__floordiv__(b, context=self)
@@ -3852,17 +3852,17 @@ class Context(object):
>>> c.Emin = -999
>>> c.Emax = 999
>>> c.exp(Decimal('-Infinity'))
- Decimal("0")
+ Decimal('0')
>>> c.exp(Decimal('-1'))
- Decimal("0.367879441")
+ Decimal('0.367879441')
>>> c.exp(Decimal('0'))
- Decimal("1")
+ Decimal('1')
>>> c.exp(Decimal('1'))
- Decimal("2.71828183")
+ Decimal('2.71828183')
>>> c.exp(Decimal('0.693147181'))
- Decimal("2.00000000")
+ Decimal('2.00000000')
>>> c.exp(Decimal('+Infinity'))
- Decimal("Infinity")
+ Decimal('Infinity')
"""
return a.exp(context=self)
@@ -3874,11 +3874,11 @@ class Context(object):
multiplication, using add, all with only one final rounding.
>>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7'))
- Decimal("22")
+ Decimal('22')
>>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7'))
- Decimal("-8")
+ Decimal('-8')
>>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578'))
- Decimal("1.38435736E+12")
+ Decimal('1.38435736E+12')
"""
return a.fma(b, c, context=self)
@@ -4032,15 +4032,15 @@ class Context(object):
>>> c.Emin = -999
>>> c.Emax = 999
>>> c.ln(Decimal('0'))
- Decimal("-Infinity")
+ Decimal('-Infinity')
>>> c.ln(Decimal('1.000'))
- Decimal("0")
+ Decimal('0')
>>> c.ln(Decimal('2.71828183'))
- Decimal("1.00000000")
+ Decimal('1.00000000')
>>> c.ln(Decimal('10'))
- Decimal("2.30258509")
+ Decimal('2.30258509')
>>> c.ln(Decimal('+Infinity'))
- Decimal("Infinity")
+ Decimal('Infinity')
"""
return a.ln(context=self)
@@ -4051,19 +4051,19 @@ class Context(object):
>>> c.Emin = -999
>>> c.Emax = 999
>>> c.log10(Decimal('0'))
- Decimal("-Infinity")
+ Decimal('-Infinity')
>>> c.log10(Decimal('0.001'))
- Decimal("-3")
+ Decimal('-3')
>>> c.log10(Decimal('1.000'))
- Decimal("0")
+ Decimal('0')
>>> c.log10(Decimal('2'))
- Decimal("0.301029996")
+ Decimal('0.301029996')
>>> c.log10(Decimal('10'))
- Decimal("1")
+ Decimal('1')
>>> c.log10(Decimal('70'))
- Decimal("1.84509804")
+ Decimal('1.84509804')
>>> c.log10(Decimal('+Infinity'))
- Decimal("Infinity")
+ Decimal('Infinity')
"""
return a.log10(context=self)
@@ -4076,13 +4076,13 @@ class Context(object):
value of that digit and without limiting the resulting exponent).
>>> ExtendedContext.logb(Decimal('250'))
- Decimal("2")
+ Decimal('2')
>>> ExtendedContext.logb(Decimal('2.50'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.logb(Decimal('0.03'))
- Decimal("-2")
+ Decimal('-2')
>>> ExtendedContext.logb(Decimal('0'))
- Decimal("-Infinity")
+ Decimal('-Infinity')
"""
return a.logb(context=self)
@@ -4092,17 +4092,17 @@ class Context(object):
The operands must be both logical numbers.
>>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.logical_and(Decimal('0'), Decimal('1'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.logical_and(Decimal('1'), Decimal('0'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.logical_and(Decimal('1'), Decimal('1'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010'))
- Decimal("1000")
+ Decimal('1000')
>>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10'))
- Decimal("10")
+ Decimal('10')
"""
return a.logical_and(b, context=self)
@@ -4112,13 +4112,13 @@ class Context(object):
The operand must be a logical number.
>>> ExtendedContext.logical_invert(Decimal('0'))
- Decimal("111111111")
+ Decimal('111111111')
>>> ExtendedContext.logical_invert(Decimal('1'))
- Decimal("111111110")
+ Decimal('111111110')
>>> ExtendedContext.logical_invert(Decimal('111111111'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.logical_invert(Decimal('101010101'))
- Decimal("10101010")
+ Decimal('10101010')
"""
return a.logical_invert(context=self)
@@ -4128,17 +4128,17 @@ class Context(object):
The operands must be both logical numbers.
>>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.logical_or(Decimal('0'), Decimal('1'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.logical_or(Decimal('1'), Decimal('0'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.logical_or(Decimal('1'), Decimal('1'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010'))
- Decimal("1110")
+ Decimal('1110')
>>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10'))
- Decimal("1110")
+ Decimal('1110')
"""
return a.logical_or(b, context=self)
@@ -4148,17 +4148,17 @@ class Context(object):
The operands must be both logical numbers.
>>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010'))
- Decimal("110")
+ Decimal('110')
>>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10'))
- Decimal("1101")
+ Decimal('1101')
"""
return a.logical_xor(b, context=self)
@@ -4172,13 +4172,13 @@ class Context(object):
infinity) of the two operands is chosen as the result.
>>> ExtendedContext.max(Decimal('3'), Decimal('2'))
- Decimal("3")
+ Decimal('3')
>>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
- Decimal("3")
+ Decimal('3')
>>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
- Decimal("7")
+ Decimal('7')
"""
return a.max(b, context=self)
@@ -4196,13 +4196,13 @@ class Context(object):
infinity) of the two operands is chosen as the result.
>>> ExtendedContext.min(Decimal('3'), Decimal('2'))
- Decimal("2")
+ Decimal('2')
>>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
- Decimal("-10")
+ Decimal('-10')
>>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
- Decimal("1.0")
+ Decimal('1.0')
>>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
- Decimal("7")
+ Decimal('7')
"""
return a.min(b, context=self)
@@ -4218,9 +4218,9 @@ class Context(object):
has the same exponent as the operand.
>>> ExtendedContext.minus(Decimal('1.3'))
- Decimal("-1.3")
+ Decimal('-1.3')
>>> ExtendedContext.minus(Decimal('-1.3'))
- Decimal("1.3")
+ Decimal('1.3')
"""
return a.__neg__(context=self)
@@ -4233,15 +4233,15 @@ class Context(object):
of the two operands.
>>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
- Decimal("3.60")
+ Decimal('3.60')
>>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
- Decimal("21")
+ Decimal('21')
>>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
- Decimal("0.72")
+ Decimal('0.72')
>>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
- Decimal("-0.0")
+ Decimal('-0.0')
>>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
- Decimal("4.28135971E+11")
+ Decimal('4.28135971E+11')
"""
return a.__mul__(b, context=self)
@@ -4252,13 +4252,13 @@ class Context(object):
>>> c.Emin = -999
>>> c.Emax = 999
>>> ExtendedContext.next_minus(Decimal('1'))
- Decimal("0.999999999")
+ Decimal('0.999999999')
>>> c.next_minus(Decimal('1E-1007'))
- Decimal("0E-1007")
+ Decimal('0E-1007')
>>> ExtendedContext.next_minus(Decimal('-1.00000003'))
- Decimal("-1.00000004")
+ Decimal('-1.00000004')
>>> c.next_minus(Decimal('Infinity'))
- Decimal("9.99999999E+999")
+ Decimal('9.99999999E+999')
"""
return a.next_minus(context=self)
@@ -4269,13 +4269,13 @@ class Context(object):
>>> c.Emin = -999
>>> c.Emax = 999
>>> ExtendedContext.next_plus(Decimal('1'))
- Decimal("1.00000001")
+ Decimal('1.00000001')
>>> c.next_plus(Decimal('-1E-1007'))
- Decimal("-0E-1007")
+ Decimal('-0E-1007')
>>> ExtendedContext.next_plus(Decimal('-1.00000003'))
- Decimal("-1.00000002")
+ Decimal('-1.00000002')
>>> c.next_plus(Decimal('-Infinity'))
- Decimal("-9.99999999E+999")
+ Decimal('-9.99999999E+999')
"""
return a.next_plus(context=self)
@@ -4291,19 +4291,19 @@ class Context(object):
>>> c.Emin = -999
>>> c.Emax = 999
>>> c.next_toward(Decimal('1'), Decimal('2'))
- Decimal("1.00000001")
+ Decimal('1.00000001')
>>> c.next_toward(Decimal('-1E-1007'), Decimal('1'))
- Decimal("-0E-1007")
+ Decimal('-0E-1007')
>>> c.next_toward(Decimal('-1.00000003'), Decimal('0'))
- Decimal("-1.00000002")
+ Decimal('-1.00000002')
>>> c.next_toward(Decimal('1'), Decimal('0'))
- Decimal("0.999999999")
+ Decimal('0.999999999')
>>> c.next_toward(Decimal('1E-1007'), Decimal('-100'))
- Decimal("0E-1007")
+ Decimal('0E-1007')
>>> c.next_toward(Decimal('-1.00000003'), Decimal('-10'))
- Decimal("-1.00000004")
+ Decimal('-1.00000004')
>>> c.next_toward(Decimal('0.00'), Decimal('-0.0000'))
- Decimal("-0.00")
+ Decimal('-0.00')
"""
return a.next_toward(b, context=self)
@@ -4314,17 +4314,17 @@ class Context(object):
result.
>>> ExtendedContext.normalize(Decimal('2.1'))
- Decimal("2.1")
+ Decimal('2.1')
>>> ExtendedContext.normalize(Decimal('-2.0'))
- Decimal("-2")
+ Decimal('-2')
>>> ExtendedContext.normalize(Decimal('1.200'))
- Decimal("1.2")
+ Decimal('1.2')
>>> ExtendedContext.normalize(Decimal('-120'))
- Decimal("-1.2E+2")
+ Decimal('-1.2E+2')
>>> ExtendedContext.normalize(Decimal('120.00'))
- Decimal("1.2E+2")
+ Decimal('1.2E+2')
>>> ExtendedContext.normalize(Decimal('0.00'))
- Decimal("0")
+ Decimal('0')
"""
return a.normalize(context=self)
@@ -4383,9 +4383,9 @@ class Context(object):
has the same exponent as the operand.
>>> ExtendedContext.plus(Decimal('1.3'))
- Decimal("1.3")
+ Decimal('1.3')
>>> ExtendedContext.plus(Decimal('-1.3'))
- Decimal("-1.3")
+ Decimal('-1.3')
"""
return a.__pos__(context=self)
@@ -4415,46 +4415,46 @@ class Context(object):
>>> c.Emin = -999
>>> c.Emax = 999
>>> c.power(Decimal('2'), Decimal('3'))
- Decimal("8")
+ Decimal('8')
>>> c.power(Decimal('-2'), Decimal('3'))
- Decimal("-8")
+ Decimal('-8')
>>> c.power(Decimal('2'), Decimal('-3'))
- Decimal("0.125")
+ Decimal('0.125')
>>> c.power(Decimal('1.7'), Decimal('8'))
- Decimal("69.7575744")
+ Decimal('69.7575744')
>>> c.power(Decimal('10'), Decimal('0.301029996'))
- Decimal("2.00000000")
+ Decimal('2.00000000')
>>> c.power(Decimal('Infinity'), Decimal('-1'))
- Decimal("0")
+ Decimal('0')
>>> c.power(Decimal('Infinity'), Decimal('0'))
- Decimal("1")
+ Decimal('1')
>>> c.power(Decimal('Infinity'), Decimal('1'))
- Decimal("Infinity")
+ Decimal('Infinity')
>>> c.power(Decimal('-Infinity'), Decimal('-1'))
- Decimal("-0")
+ Decimal('-0')
>>> c.power(Decimal('-Infinity'), Decimal('0'))
- Decimal("1")
+ Decimal('1')
>>> c.power(Decimal('-Infinity'), Decimal('1'))
- Decimal("-Infinity")
+ Decimal('-Infinity')
>>> c.power(Decimal('-Infinity'), Decimal('2'))
- Decimal("Infinity")
+ Decimal('Infinity')
>>> c.power(Decimal('0'), Decimal('0'))
- Decimal("NaN")
+ Decimal('NaN')
>>> c.power(Decimal('3'), Decimal('7'), Decimal('16'))
- Decimal("11")
+ Decimal('11')
>>> c.power(Decimal('-3'), Decimal('7'), Decimal('16'))
- Decimal("-11")
+ Decimal('-11')
>>> c.power(Decimal('-3'), Decimal('8'), Decimal('16'))
- Decimal("1")
+ Decimal('1')
>>> c.power(Decimal('3'), Decimal('7'), Decimal('-16'))
- Decimal("11")
+ Decimal('11')
>>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789'))
- Decimal("11729830")
+ Decimal('11729830')
>>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729'))
- Decimal("-0")
+ Decimal('-0')
>>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537'))
- Decimal("1")
+ Decimal('1')
"""
return a.__pow__(b, modulo, context=self)
@@ -4477,35 +4477,35 @@ class Context(object):
if the result is subnormal and inexact.
>>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
- Decimal("2.170")
+ Decimal('2.170')
>>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
- Decimal("2.17")
+ Decimal('2.17')
>>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
- Decimal("2.2")
+ Decimal('2.2')
>>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
- Decimal("2")
+ Decimal('2')
>>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
- Decimal("0E+1")
+ Decimal('0E+1')
>>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
- Decimal("-Infinity")
+ Decimal('-Infinity')
>>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
- Decimal("NaN")
+ Decimal('NaN')
>>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
- Decimal("-0")
+ Decimal('-0')
>>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
- Decimal("-0E+5")
+ Decimal('-0E+5')
>>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
- Decimal("NaN")
+ Decimal('NaN')
>>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
- Decimal("NaN")
+ Decimal('NaN')
>>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
- Decimal("217.0")
+ Decimal('217.0')
>>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
- Decimal("217")
+ Decimal('217')
>>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
- Decimal("2.2E+2")
+ Decimal('2.2E+2')
>>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
- Decimal("2E+2")
+ Decimal('2E+2')
"""
return a.quantize(b, context=self)
@@ -4513,7 +4513,7 @@ class Context(object):
"""Just returns 10, as this is Decimal, :)
>>> ExtendedContext.radix()
- Decimal("10")
+ Decimal('10')
"""
return Decimal(10)
@@ -4530,17 +4530,17 @@ class Context(object):
remainder cannot be calculated).
>>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
- Decimal("2.1")
+ Decimal('2.1')
>>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
- Decimal("-1")
+ Decimal('-1')
>>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
- Decimal("0.2")
+ Decimal('0.2')
>>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
- Decimal("0.1")
+ Decimal('0.1')
>>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
- Decimal("1.0")
+ Decimal('1.0')
"""
return a.__mod__(b, context=self)
@@ -4555,19 +4555,19 @@ class Context(object):
remainder cannot be calculated).
>>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
- Decimal("-0.9")
+ Decimal('-0.9')
>>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
- Decimal("-2")
+ Decimal('-2')
>>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
- Decimal("-1")
+ Decimal('-1')
>>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
- Decimal("0.2")
+ Decimal('0.2')
>>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
- Decimal("0.1")
+ Decimal('0.1')
>>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
- Decimal("-0.3")
+ Decimal('-0.3')
"""
return a.remainder_near(b, context=self)
@@ -4581,15 +4581,15 @@ class Context(object):
positive or to the right otherwise.
>>> ExtendedContext.rotate(Decimal('34'), Decimal('8'))
- Decimal("400000003")
+ Decimal('400000003')
>>> ExtendedContext.rotate(Decimal('12'), Decimal('9'))
- Decimal("12")
+ Decimal('12')
>>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2'))
- Decimal("891234567")
+ Decimal('891234567')
>>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0'))
- Decimal("123456789")
+ Decimal('123456789')
>>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2'))
- Decimal("345678912")
+ Decimal('345678912')
"""
return a.rotate(b, context=self)
@@ -4614,11 +4614,11 @@ class Context(object):
"""Returns the first operand after adding the second value its exp.
>>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2'))
- Decimal("0.0750")
+ Decimal('0.0750')
>>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0'))
- Decimal("7.50")
+ Decimal('7.50')
>>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3'))
- Decimal("7.50E+3")
+ Decimal('7.50E+3')
"""
return a.scaleb (b, context=self)
@@ -4633,15 +4633,15 @@ class Context(object):
coefficient are zeros.
>>> ExtendedContext.shift(Decimal('34'), Decimal('8'))
- Decimal("400000000")
+ Decimal('400000000')
>>> ExtendedContext.shift(Decimal('12'), Decimal('9'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2'))
- Decimal("1234567")
+ Decimal('1234567')
>>> ExtendedContext.shift(Decimal('123456789'), Decimal('0'))
- Decimal("123456789")
+ Decimal('123456789')
>>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2'))
- Decimal("345678900")
+ Decimal('345678900')
"""
return a.shift(b, context=self)
@@ -4652,23 +4652,23 @@ class Context(object):
algorithm.
>>> ExtendedContext.sqrt(Decimal('0'))
- Decimal("0")
+ Decimal('0')
>>> ExtendedContext.sqrt(Decimal('-0'))
- Decimal("-0")
+ Decimal('-0')
>>> ExtendedContext.sqrt(Decimal('0.39'))
- Decimal("0.624499800")
+ Decimal('0.624499800')
>>> ExtendedContext.sqrt(Decimal('100'))
- Decimal("10")
+ Decimal('10')
>>> ExtendedContext.sqrt(Decimal('1'))
- Decimal("1")
+ Decimal('1')
>>> ExtendedContext.sqrt(Decimal('1.0'))
- Decimal("1.0")
+ Decimal('1.0')
>>> ExtendedContext.sqrt(Decimal('1.00'))
- Decimal("1.0")
+ Decimal('1.0')
>>> ExtendedContext.sqrt(Decimal('7'))
- Decimal("2.64575131")
+ Decimal('2.64575131')
>>> ExtendedContext.sqrt(Decimal('10'))
- Decimal("3.16227766")
+ Decimal('3.16227766')
>>> ExtendedContext.prec
9
"""
@@ -4678,11 +4678,11 @@ class Context(object):
"""Return the difference between the two operands.
>>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
- Decimal("0.23")
+ Decimal('0.23')
>>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
- Decimal("0.00")
+ Decimal('0.00')
>>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
- Decimal("-0.77")
+ Decimal('-0.77')
"""
return a.__sub__(b, context=self)
@@ -4711,21 +4711,21 @@ class Context(object):
context.
>>> ExtendedContext.to_integral_exact(Decimal('2.1'))
- Decimal("2")
+ Decimal('2')
>>> ExtendedContext.to_integral_exact(Decimal('100'))
- Decimal("100")
+ Decimal('100')
>>> ExtendedContext.to_integral_exact(Decimal('100.0'))
- Decimal("100")
+ Decimal('100')
>>> ExtendedContext.to_integral_exact(Decimal('101.5'))
- Decimal("102")
+ Decimal('102')
>>> ExtendedContext.to_integral_exact(Decimal('-101.5'))
- Decimal("-102")
+ Decimal('-102')
>>> ExtendedContext.to_integral_exact(Decimal('10E+5'))
- Decimal("1.0E+6")
+ Decimal('1.0E+6')
>>> ExtendedContext.to_integral_exact(Decimal('7.89E+77'))
- Decimal("7.89E+77")
+ Decimal('7.89E+77')
>>> ExtendedContext.to_integral_exact(Decimal('-Inf'))
- Decimal("-Infinity")
+ Decimal('-Infinity')
"""
return a.to_integral_exact(context=self)
@@ -4739,21 +4739,21 @@ class Context(object):
be set. The rounding mode is taken from the context.
>>> ExtendedContext.to_integral_value(Decimal('2.1'))
- Decimal("2")
+ Decimal('2')
>>> ExtendedContext.to_integral_value(Decimal('100'))
- Decimal("100")
+ Decimal('100')
>>> ExtendedContext.to_integral_value(Decimal('100.0'))
- Decimal("100")
+ Decimal('100')
>>> ExtendedContext.to_integral_value(Decimal('101.5'))
- Decimal("102")
+ Decimal('102')
>>> ExtendedContext.to_integral_value(Decimal('-101.5'))
- Decimal("-102")
+ Decimal('-102')
>>> ExtendedContext.to_integral_value(Decimal('10E+5'))
- Decimal("1.0E+6")
+ Decimal('1.0E+6')
>>> ExtendedContext.to_integral_value(Decimal('7.89E+77'))
- Decimal("7.89E+77")
+ Decimal('7.89E+77')
>>> ExtendedContext.to_integral_value(Decimal('-Inf'))
- Decimal("-Infinity")
+ Decimal('-Infinity')
"""
return a.to_integral_value(context=self)
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 25b9f020bf..f368576e63 100755
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -62,7 +62,7 @@ class Fraction(numbers.Rational):
"""
self = super(Fraction, cls).__new__(cls)
- if denominator == 1:
+ if not isinstance(numerator, int) and denominator == 1:
if isinstance(numerator, str):
# Handle construction from strings.
input = numerator
@@ -84,24 +84,22 @@ class Fraction(numbers.Rational):
if m.group('sign') == '-':
numerator = -numerator
- elif (not isinstance(numerator, numbers.Integral) and
- isinstance(numerator, numbers.Rational)):
- # Handle copies from other rationals.
+ elif isinstance(numerator, numbers.Rational):
+ # Handle copies from other rationals. Integrals get
+ # caught here too, but it doesn't matter because
+ # denominator is already 1.
other_rational = numerator
numerator = other_rational.numerator
denominator = other_rational.denominator
- if (not isinstance(numerator, numbers.Integral) or
- not isinstance(denominator, numbers.Integral)):
- raise TypeError("Fraction(%(numerator)s, %(denominator)s):"
- " Both arguments must be integral." % locals())
-
if denominator == 0:
raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
+ numerator = numerator.__index__()
+ denominator = denominator.__index__()
g = gcd(numerator, denominator)
- self._numerator = int(numerator // g)
- self._denominator = int(denominator // g)
+ self._numerator = numerator // g
+ self._denominator = denominator // g
return self
@classmethod
@@ -138,42 +136,60 @@ class Fraction(numbers.Rational):
else:
return cls(digits, 10 ** -exp)
- @classmethod
- def from_continued_fraction(cls, seq):
- 'Build a Fraction from a continued fraction expessed as a sequence'
- n, d = 1, 0
- for e in reversed(seq):
- n, d = d, n
- n += e * d
- return cls(n, d) if seq else cls(0)
-
- def as_continued_fraction(self):
- 'Return continued fraction expressed as a list'
- n = self.numerator
- d = self.denominator
- cf = []
- while d:
- e = int(n // d)
- cf.append(e)
- n -= e * d
- n, d = d, n
- return cf
-
- def approximate(self, max_denominator):
- 'Best rational approximation with a denominator <= max_denominator'
- # XXX First cut at algorithm
- # Still needs rounding rules as specified at
- # http://en.wikipedia.org/wiki/Continued_fraction
- if self.denominator <= max_denominator:
- return self
- cf = self.as_continued_fraction()
- result = Fraction(0)
- for i in range(1, len(cf)):
- new = self.from_continued_fraction(cf[:i])
- if new.denominator > max_denominator:
+ def limit_denominator(self, max_denominator=1000000):
+ """Closest Fraction to self with denominator at most max_denominator.
+
+ >>> Fraction('3.141592653589793').limit_denominator(10)
+ Fraction(22, 7)
+ >>> Fraction('3.141592653589793').limit_denominator(100)
+ Fraction(311, 99)
+ >>> Fraction(1234, 5678).limit_denominator(10000)
+ Fraction(1234, 5678)
+
+ """
+ # Algorithm notes: For any real number x, define a *best upper
+ # approximation* to x to be a rational number p/q such that:
+ #
+ # (1) p/q >= x, and
+ # (2) if p/q > r/s >= x then s > q, for any rational r/s.
+ #
+ # Define *best lower approximation* similarly. Then it can be
+ # proved that a rational number is a best upper or lower
+ # approximation to x if, and only if, it is a convergent or
+ # semiconvergent of the (unique shortest) continued fraction
+ # associated to x.
+ #
+ # To find a best rational approximation with denominator <= M,
+ # we find the best upper and lower approximations with
+ # denominator <= M and take whichever of these is closer to x.
+ # In the event of a tie, the bound with smaller denominator is
+ # chosen. If both denominators are equal (which can happen
+ # only when max_denominator == 1 and self is midway between
+ # two integers) the lower bound---i.e., the floor of self, is
+ # taken.
+
+ if max_denominator < 1:
+ raise ValueError("max_denominator should be at least 1")
+ if self._denominator <= max_denominator:
+ return Fraction(self)
+
+ p0, q0, p1, q1 = 0, 1, 1, 0
+ n, d = self._numerator, self._denominator
+ while True:
+ a = n//d
+ q2 = q0+a*q1
+ if q2 > max_denominator:
break
- result = new
- return result
+ p0, q0, p1, q1 = p1, q1, p0+a*p1, q2
+ n, d = d, n-a*d
+
+ k = (max_denominator-q0)//q1
+ bound1 = Fraction(p0+k*p1, q0+k*q1)
+ bound2 = Fraction(p1, q1)
+ if abs(bound2 - self) <= abs(bound1-self):
+ return bound2
+ else:
+ return bound1
@property
def numerator(a):
@@ -185,14 +201,14 @@ class Fraction(numbers.Rational):
def __repr__(self):
"""repr(self)"""
- return ('Fraction(%r,%r)' % (self.numerator, self.denominator))
+ return ('Fraction(%r, %r)' % (self._numerator, self._denominator))
def __str__(self):
"""str(self)"""
- if self.denominator == 1:
- return str(self.numerator)
+ if self._denominator == 1:
+ return str(self._numerator)
else:
- return '%s/%s' % (self.numerator, self.denominator)
+ return '%s/%s' % (self._numerator, self._denominator)
def _operator_fallbacks(monomorphic_operator, fallback_operator):
"""Generates forward and reverse operators given a purely-rational
@@ -360,11 +376,11 @@ class Fraction(numbers.Rational):
if b.denominator == 1:
power = b.numerator
if power >= 0:
- return Fraction(a.numerator ** power,
- a.denominator ** power)
+ return Fraction(a._numerator ** power,
+ a._denominator ** power)
else:
- return Fraction(a.denominator ** -power,
- a.numerator ** -power)
+ return Fraction(a._denominator ** -power,
+ a._numerator ** -power)
else:
# A fractional power will generally produce an
# irrational number.
@@ -374,36 +390,36 @@ class Fraction(numbers.Rational):
def __rpow__(b, a):
"""a ** b"""
- if b.denominator == 1 and b.numerator >= 0:
+ if b._denominator == 1 and b._numerator >= 0:
# If a is an int, keep it that way if possible.
- return a ** b.numerator
+ return a ** b._numerator
if isinstance(a, numbers.Rational):
return Fraction(a.numerator, a.denominator) ** b
- if b.denominator == 1:
- return a ** b.numerator
+ if b._denominator == 1:
+ return a ** b._numerator
return a ** float(b)
def __pos__(a):
"""+a: Coerces a subclass instance to Fraction"""
- return Fraction(a.numerator, a.denominator)
+ return Fraction(a._numerator, a._denominator)
def __neg__(a):
"""-a"""
- return Fraction(-a.numerator, a.denominator)
+ return Fraction(-a._numerator, a._denominator)
def __abs__(a):
"""abs(a)"""
- return Fraction(abs(a.numerator), a.denominator)
+ return Fraction(abs(a._numerator), a._denominator)
def __trunc__(a):
"""trunc(a)"""
- if a.numerator < 0:
- return -(-a.numerator // a.denominator)
+ if a._numerator < 0:
+ return -(-a._numerator // a._denominator)
else:
- return a.numerator // a.denominator
+ return a._numerator // a._denominator
def __floor__(a):
"""Will be math.floor(a) in 3.0."""
@@ -447,22 +463,22 @@ class Fraction(numbers.Rational):
"""
# XXX since this method is expensive, consider caching the result
- if self.denominator == 1:
+ if self._denominator == 1:
# Get integers right.
- return hash(self.numerator)
+ return hash(self._numerator)
# Expensive check, but definitely correct.
if self == float(self):
return hash(float(self))
else:
# Use tuple's hash to avoid a high collision rate on
# simple fractions.
- return hash((self.numerator, self.denominator))
+ return hash((self._numerator, self._denominator))
def __eq__(a, b):
"""a == b"""
if isinstance(b, numbers.Rational):
- return (a.numerator == b.numerator and
- a.denominator == b.denominator)
+ return (a._numerator == b.numerator and
+ a._denominator == b.denominator)
if isinstance(b, numbers.Complex) and b.imag == 0:
b = b.real
if isinstance(b, float):
@@ -517,7 +533,7 @@ class Fraction(numbers.Rational):
def __bool__(a):
"""a != 0"""
- return a.numerator != 0
+ return a._numerator != 0
# support for pickling, copy, and deepcopy
@@ -527,9 +543,9 @@ class Fraction(numbers.Rational):
def __copy__(self):
if type(self) == Fraction:
return self # I'm immutable; therefore I am my own clone
- return self.__class__(self.numerator, self.denominator)
+ return self.__class__(self._numerator, self._denominator)
def __deepcopy__(self, memo):
if type(self) == Fraction:
return self # My components are also immutable
- return self.__class__(self.numerator, self.denominator)
+ return self.__class__(self._numerator, self._denominator)
diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt
index d51d94e202..cd8565ce16 100644
--- a/Lib/idlelib/NEWS.txt
+++ b/Lib/idlelib/NEWS.txt
@@ -70,6 +70,13 @@ What's New in IDLE 2.6a1?
- Clean up EditorWindow close.
+- Patch 1693258: Fix for duplicate "preferences" menu-OS X. Backport of r56204.
+
+- OSX: Avoid crash for those versions of Tcl/Tk which don't have a console
+
+- Bug in idlelib.MultiCall: Options dialog was crashing IDLE if there was an
+ option in config-extensions w/o a value. Patch #1672481, Tal Einat
+
- Corrected some bugs in AutoComplete. Also, Page Up/Down in ACW implemented;
mouse and cursor selection in ACWindow implemented; double Tab inserts
current selection and closes ACW (similar to double-click and Return); scroll
diff --git a/Lib/numbers.py b/Lib/numbers.py
index a4b7a6d260..4217d084be 100644
--- a/Lib/numbers.py
+++ b/Lib/numbers.py
@@ -46,9 +46,11 @@ Inexact.register(float)
# Inexact.register(decimal.Decimal)
-## Notes on Decimal and it how relates to the numeric tower
-## --------------------------------------------------------
-## Decimal is Real except that it does not support rich comparisons.
+## Notes on Decimal
+## ----------------
+## Decimal has all of the methods specified by the Real abc, but it should
+## not be registered as a Real because decimals do not interoperate with
+## binary floats.
##
## Decimal has some of the characteristics of Integrals. It provides
## logical operations but not as operators. The logical operations only apply
@@ -304,7 +306,6 @@ class Real(Complex):
return +self
Real.register(float)
-# Real.register(decimal.Decimal)
class Rational(Real, Exact):
diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py
index 884dd322b4..80b0123c4b 100644
--- a/Lib/test/test_abc.py
+++ b/Lib/test/test_abc.py
@@ -81,6 +81,16 @@ class TestABC(unittest.TestCase):
self.assertEqual(issubclass(C, A), True)
self.assertEqual(isinstance(c, A), True)
+ def test_isinstance_invalidation(self):
+ class A(metaclass=abc.ABCMeta):
+ pass
+ class B:
+ pass
+ b = B()
+ self.assertEqual(isinstance(b, A), False)
+ A.register(B)
+ self.assertEqual(isinstance(b, A), True)
+
def test_registration_builtins(self):
class A(metaclass=abc.ABCMeta):
pass
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 1ef0ad3025..ae36dcbcdc 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -1040,7 +1040,7 @@ class DecimalUsabilityTest(unittest.TestCase):
d = Decimal('15.32')
self.assertEqual(str(d), '15.32') # str
- self.assertEqual(repr(d), 'Decimal("15.32")') # repr
+ self.assertEqual(repr(d), "Decimal('15.32')") # repr
def test_tonum_methods(self):
#Test float, int and long methods.
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index 00fd549d72..dacb58796c 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -8,7 +8,7 @@ import fractions
import unittest
from copy import copy, deepcopy
from pickle import dumps, loads
-R = fractions.Fraction
+F = fractions.Fraction
gcd = fractions.gcd
@@ -49,77 +49,76 @@ class FractionTest(unittest.TestCase):
self.fail("%s not raised" % exc_type.__name__)
def testInit(self):
- self.assertEquals((0, 1), _components(R()))
- self.assertEquals((7, 1), _components(R(7)))
- self.assertEquals((7, 3), _components(R(R(7, 3))))
+ self.assertEquals((0, 1), _components(F()))
+ self.assertEquals((7, 1), _components(F(7)))
+ self.assertEquals((7, 3), _components(F(F(7, 3))))
- self.assertEquals((-1, 1), _components(R(-1, 1)))
- self.assertEquals((-1, 1), _components(R(1, -1)))
- self.assertEquals((1, 1), _components(R(-2, -2)))
- self.assertEquals((1, 2), _components(R(5, 10)))
- self.assertEquals((7, 15), _components(R(7, 15)))
- self.assertEquals((10**23, 1), _components(R(10**23)))
+ self.assertEquals((-1, 1), _components(F(-1, 1)))
+ self.assertEquals((-1, 1), _components(F(1, -1)))
+ self.assertEquals((1, 1), _components(F(-2, -2)))
+ self.assertEquals((1, 2), _components(F(5, 10)))
+ self.assertEquals((7, 15), _components(F(7, 15)))
+ self.assertEquals((10**23, 1), _components(F(10**23)))
self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)",
- R, 12, 0)
- self.assertRaises(TypeError, R, 1.5)
- self.assertRaises(TypeError, R, 1.5 + 3j)
+ F, 12, 0)
+ self.assertRaises(AttributeError, F, 1.5)
+ self.assertRaises(AttributeError, F, 1.5 + 3j)
- self.assertRaises(TypeError, R, R(1, 2), 3)
- self.assertRaises(TypeError, R, "3/2", 3)
+ self.assertRaises(AttributeError, F, F(1, 2), 3)
+ self.assertRaises(AttributeError, F, "3/2", 3)
def testFromString(self):
- self.assertEquals((5, 1), _components(R("5")))
- self.assertEquals((3, 2), _components(R("3/2")))
- self.assertEquals((3, 2), _components(R(" \n +3/2")))
- self.assertEquals((-3, 2), _components(R("-3/2 ")))
- self.assertEquals((3, 2), _components(R(" 03/02 \n ")))
- self.assertEquals((3, 2), _components(R(" 03/02 \n ")))
- self.assertEquals((16, 5), _components(R(" 3.2 ")))
- self.assertEquals((-16, 5), _components(R(" -3.2 ")))
- self.assertEquals((-3, 1), _components(R(" -3. ")))
- self.assertEquals((3, 5), _components(R(" .6 ")))
+ self.assertEquals((5, 1), _components(F("5")))
+ self.assertEquals((3, 2), _components(F("3/2")))
+ self.assertEquals((3, 2), _components(F(" \n +3/2")))
+ self.assertEquals((-3, 2), _components(F("-3/2 ")))
+ self.assertEquals((13, 2), _components(F(" 013/02 \n ")))
+ self.assertEquals((16, 5), _components(F(" 3.2 ")))
+ self.assertEquals((-16, 5), _components(F(" -3.2 ")))
+ self.assertEquals((-3, 1), _components(F(" -3. ")))
+ self.assertEquals((3, 5), _components(F(" .6 ")))
self.assertRaisesMessage(
ZeroDivisionError, "Fraction(3, 0)",
- R, "3/0")
+ F, "3/0")
self.assertRaisesMessage(
ValueError, "Invalid literal for Fraction: 3/",
- R, "3/")
+ F, "3/")
self.assertRaisesMessage(
ValueError, "Invalid literal for Fraction: 3 /2",
- R, "3 /2")
+ F, "3 /2")
self.assertRaisesMessage(
# Denominators don't need a sign.
ValueError, "Invalid literal for Fraction: 3/+2",
- R, "3/+2")
+ F, "3/+2")
self.assertRaisesMessage(
# Imitate float's parsing.
ValueError, "Invalid literal for Fraction: + 3/2",
- R, "+ 3/2")
+ F, "+ 3/2")
self.assertRaisesMessage(
# Avoid treating '.' as a regex special character.
ValueError, "Invalid literal for Fraction: 3a2",
- R, "3a2")
+ F, "3a2")
self.assertRaisesMessage(
# Only parse ordinary decimals, not scientific form.
ValueError, "Invalid literal for Fraction: 3.2e4",
- R, "3.2e4")
+ F, "3.2e4")
self.assertRaisesMessage(
# Don't accept combinations of decimals and rationals.
ValueError, "Invalid literal for Fraction: 3/7.2",
- R, "3/7.2")
+ F, "3/7.2")
self.assertRaisesMessage(
# Don't accept combinations of decimals and rationals.
ValueError, "Invalid literal for Fraction: 3.2/7",
- R, "3.2/7")
+ F, "3.2/7")
self.assertRaisesMessage(
# Allow 3. and .3, but not .
ValueError, "Invalid literal for Fraction: .",
- R, ".")
+ F, ".")
def testImmutable(self):
- r = R(7, 3)
+ r = F(7, 3)
r.__init__(2, 15)
self.assertEquals((7, 3), _components(r))
@@ -132,267 +131,253 @@ class FractionTest(unittest.TestCase):
r._denominator = 2
self.assertEquals((4, 2), _components(r))
# Which breaks some important operations:
- self.assertNotEquals(R(4, 2), r)
+ self.assertNotEquals(F(4, 2), r)
def testFromFloat(self):
self.assertRaisesMessage(
TypeError, "Fraction.from_float() only takes floats, not 3 (int)",
- R.from_float, 3)
+ F.from_float, 3)
- self.assertEquals((0, 1), _components(R.from_float(-0.0)))
- self.assertEquals((10, 1), _components(R.from_float(10.0)))
- self.assertEquals((-5, 2), _components(R.from_float(-2.5)))
+ self.assertEquals((0, 1), _components(F.from_float(-0.0)))
+ self.assertEquals((10, 1), _components(F.from_float(10.0)))
+ self.assertEquals((-5, 2), _components(F.from_float(-2.5)))
self.assertEquals((99999999999999991611392, 1),
- _components(R.from_float(1e23)))
- self.assertEquals(float(10**23), float(R.from_float(1e23)))
+ _components(F.from_float(1e23)))
+ self.assertEquals(float(10**23), float(F.from_float(1e23)))
self.assertEquals((3602879701896397, 1125899906842624),
- _components(R.from_float(3.2)))
- self.assertEquals(3.2, float(R.from_float(3.2)))
+ _components(F.from_float(3.2)))
+ self.assertEquals(3.2, float(F.from_float(3.2)))
inf = 1e1000
nan = inf - inf
self.assertRaisesMessage(
TypeError, "Cannot convert inf to Fraction.",
- R.from_float, inf)
+ F.from_float, inf)
self.assertRaisesMessage(
TypeError, "Cannot convert -inf to Fraction.",
- R.from_float, -inf)
+ F.from_float, -inf)
self.assertRaisesMessage(
TypeError, "Cannot convert nan to Fraction.",
- R.from_float, nan)
+ F.from_float, nan)
def testFromDecimal(self):
self.assertRaisesMessage(
TypeError,
"Fraction.from_decimal() only takes Decimals, not 3 (int)",
- R.from_decimal, 3)
- self.assertEquals(R(0), R.from_decimal(Decimal("-0")))
- self.assertEquals(R(5, 10), R.from_decimal(Decimal("0.5")))
- self.assertEquals(R(5, 1000), R.from_decimal(Decimal("5e-3")))
- self.assertEquals(R(5000), R.from_decimal(Decimal("5e3")))
- self.assertEquals(1 - R(1, 10**30),
- R.from_decimal(Decimal("0." + "9" * 30)))
+ F.from_decimal, 3)
+ self.assertEquals(F(0), F.from_decimal(Decimal("-0")))
+ self.assertEquals(F(5, 10), F.from_decimal(Decimal("0.5")))
+ self.assertEquals(F(5, 1000), F.from_decimal(Decimal("5e-3")))
+ self.assertEquals(F(5000), F.from_decimal(Decimal("5e3")))
+ self.assertEquals(1 - F(1, 10**30),
+ F.from_decimal(Decimal("0." + "9" * 30)))
self.assertRaisesMessage(
TypeError, "Cannot convert Infinity to Fraction.",
- R.from_decimal, Decimal("inf"))
+ F.from_decimal, Decimal("inf"))
self.assertRaisesMessage(
TypeError, "Cannot convert -Infinity to Fraction.",
- R.from_decimal, Decimal("-inf"))
+ F.from_decimal, Decimal("-inf"))
self.assertRaisesMessage(
TypeError, "Cannot convert NaN to Fraction.",
- R.from_decimal, Decimal("nan"))
+ F.from_decimal, Decimal("nan"))
self.assertRaisesMessage(
TypeError, "Cannot convert sNaN to Fraction.",
- R.from_decimal, Decimal("snan"))
-
- def testFromContinuedFraction(self):
- self.assertRaises(TypeError, R.from_continued_fraction, None)
- phi = R.from_continued_fraction([1]*100)
- self.assertEquals(round(phi - (1 + 5 ** 0.5) / 2, 10), 0.0)
-
- minusphi = R.from_continued_fraction([-1]*100)
- self.assertEquals(round(minusphi + (1 + 5 ** 0.5) / 2, 10), 0.0)
-
- self.assertEquals(R.from_continued_fraction([0]), R(0))
- self.assertEquals(R.from_continued_fraction([]), R(0))
-
- def testAsContinuedFraction(self):
- self.assertEqual(R.from_float(math.pi).as_continued_fraction()[:15],
- [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 3, 3])
- self.assertEqual(R.from_float(-math.pi).as_continued_fraction()[:16],
- [-4, 1, 6, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 3, 3])
- self.assertEqual(R(0).as_continued_fraction(), [0])
-
- def testApproximateFrom(self):
- self.assertEqual(R.from_float(math.pi).approximate(10000), R(355, 113))
- self.assertEqual(R.from_float(-math.pi).approximate(10000), R(-355, 113))
- self.assertEqual(R.from_float(0.0).approximate(10000), R(0))
+ F.from_decimal, Decimal("snan"))
+
+ def testLimitDenominator(self):
+ rpi = F('3.1415926535897932')
+ self.assertEqual(rpi.limit_denominator(10000), F(355, 113))
+ self.assertEqual(-rpi.limit_denominator(10000), F(-355, 113))
+ self.assertEqual(rpi.limit_denominator(113), F(355, 113))
+ self.assertEqual(rpi.limit_denominator(112), F(333, 106))
+ self.assertEqual(F(201, 200).limit_denominator(100), F(1))
+ self.assertEqual(F(201, 200).limit_denominator(101), F(102, 101))
+ self.assertEqual(F(0).limit_denominator(10000), F(0))
def testConversions(self):
- self.assertTypedEquals(-1, math.trunc(R(-11, 10)))
- self.assertTypedEquals(-2, math.floor(R(-11, 10)))
- self.assertTypedEquals(-1, math.ceil(R(-11, 10)))
- self.assertTypedEquals(-1, math.ceil(R(-10, 10)))
- self.assertTypedEquals(-1, int(R(-11, 10)))
-
- self.assertTypedEquals(0, round(R(-1, 10)))
- self.assertTypedEquals(0, round(R(-5, 10)))
- self.assertTypedEquals(-2, round(R(-15, 10)))
- self.assertTypedEquals(-1, round(R(-7, 10)))
-
- self.assertEquals(False, bool(R(0, 1)))
- self.assertEquals(True, bool(R(3, 2)))
- self.assertTypedEquals(0.1, float(R(1, 10)))
+ self.assertTypedEquals(-1, math.trunc(F(-11, 10)))
+ self.assertTypedEquals(-2, math.floor(F(-11, 10)))
+ self.assertTypedEquals(-1, math.ceil(F(-11, 10)))
+ self.assertTypedEquals(-1, math.ceil(F(-10, 10)))
+ self.assertTypedEquals(-1, int(F(-11, 10)))
+ self.assertTypedEquals(0, round(F(-1, 10)))
+ self.assertTypedEquals(0, round(F(-5, 10)))
+ self.assertTypedEquals(-2, round(F(-15, 10)))
+ self.assertTypedEquals(-1, round(F(-7, 10)))
+
+ self.assertEquals(False, bool(F(0, 1)))
+ self.assertEquals(True, bool(F(3, 2)))
+ self.assertTypedEquals(0.1, float(F(1, 10)))
# Check that __float__ isn't implemented by converting the
# numerator and denominator to float before dividing.
self.assertRaises(OverflowError, float, int('2'*400+'7'))
self.assertAlmostEquals(2.0/3,
- float(R(int('2'*400+'7'), int('3'*400+'1'))))
+ float(F(int('2'*400+'7'), int('3'*400+'1'))))
- self.assertTypedEquals(0.1+0j, complex(R(1,10)))
+ self.assertTypedEquals(0.1+0j, complex(F(1,10)))
def testRound(self):
- self.assertTypedEquals(R(-200), round(R(-150), -2))
- self.assertTypedEquals(R(-200), round(R(-250), -2))
- self.assertTypedEquals(R(30), round(R(26), -1))
- self.assertTypedEquals(R(-2, 10), round(R(-15, 100), 1))
- self.assertTypedEquals(R(-2, 10), round(R(-25, 100), 1))
+ self.assertTypedEquals(F(-200), round(F(-150), -2))
+ self.assertTypedEquals(F(-200), round(F(-250), -2))
+ self.assertTypedEquals(F(30), round(F(26), -1))
+ self.assertTypedEquals(F(-2, 10), round(F(-15, 100), 1))
+ self.assertTypedEquals(F(-2, 10), round(F(-25, 100), 1))
def testArithmetic(self):
- self.assertEquals(R(1, 2), R(1, 10) + R(2, 5))
- self.assertEquals(R(-3, 10), R(1, 10) - R(2, 5))
- self.assertEquals(R(1, 25), R(1, 10) * R(2, 5))
- self.assertEquals(R(1, 4), R(1, 10) / R(2, 5))
- self.assertTypedEquals(2, R(9, 10) // R(2, 5))
- self.assertTypedEquals(10**23, R(10**23, 1) // R(1))
- self.assertEquals(R(2, 3), R(-7, 3) % R(3, 2))
- self.assertEquals(R(8, 27), R(2, 3) ** R(3))
- self.assertEquals(R(27, 8), R(2, 3) ** R(-3))
- self.assertTypedEquals(2.0, R(4) ** R(1, 2))
- z = pow(R(-1), R(1, 2))
+ self.assertEquals(F(1, 2), F(1, 10) + F(2, 5))
+ self.assertEquals(F(-3, 10), F(1, 10) - F(2, 5))
+ self.assertEquals(F(1, 25), F(1, 10) * F(2, 5))
+ self.assertEquals(F(1, 4), F(1, 10) / F(2, 5))
+ self.assertTypedEquals(2, F(9, 10) // F(2, 5))
+ self.assertTypedEquals(10**23, F(10**23, 1) // F(1))
+ self.assertEquals(F(2, 3), F(-7, 3) % F(3, 2))
+ self.assertEquals(F(8, 27), F(2, 3) ** F(3))
+ self.assertEquals(F(27, 8), F(2, 3) ** F(-3))
+ self.assertTypedEquals(2.0, F(4) ** F(1, 2))
+ z = pow(F(-1), F(1, 2))
self.assertAlmostEquals(z.real, 0)
self.assertEquals(z.imag, 1)
def testMixedArithmetic(self):
- self.assertTypedEquals(R(11, 10), R(1, 10) + 1)
- self.assertTypedEquals(1.1, R(1, 10) + 1.0)
- self.assertTypedEquals(1.1 + 0j, R(1, 10) + (1.0 + 0j))
- self.assertTypedEquals(R(11, 10), 1 + R(1, 10))
- self.assertTypedEquals(1.1, 1.0 + R(1, 10))
- self.assertTypedEquals(1.1 + 0j, (1.0 + 0j) + R(1, 10))
-
- self.assertTypedEquals(R(-9, 10), R(1, 10) - 1)
- self.assertTypedEquals(-0.9, R(1, 10) - 1.0)
- self.assertTypedEquals(-0.9 + 0j, R(1, 10) - (1.0 + 0j))
- self.assertTypedEquals(R(9, 10), 1 - R(1, 10))
- self.assertTypedEquals(0.9, 1.0 - R(1, 10))
- self.assertTypedEquals(0.9 + 0j, (1.0 + 0j) - R(1, 10))
-
- self.assertTypedEquals(R(1, 10), R(1, 10) * 1)
- self.assertTypedEquals(0.1, R(1, 10) * 1.0)
- self.assertTypedEquals(0.1 + 0j, R(1, 10) * (1.0 + 0j))
- self.assertTypedEquals(R(1, 10), 1 * R(1, 10))
- self.assertTypedEquals(0.1, 1.0 * R(1, 10))
- self.assertTypedEquals(0.1 + 0j, (1.0 + 0j) * R(1, 10))
-
- self.assertTypedEquals(R(1, 10), R(1, 10) / 1)
- self.assertTypedEquals(0.1, R(1, 10) / 1.0)
- self.assertTypedEquals(0.1 + 0j, R(1, 10) / (1.0 + 0j))
- self.assertTypedEquals(R(10, 1), 1 / R(1, 10))
- self.assertTypedEquals(10.0, 1.0 / R(1, 10))
- self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / R(1, 10))
-
- self.assertTypedEquals(0, R(1, 10) // 1)
- self.assertTypedEquals(0, R(1, 10) // 1.0)
- self.assertTypedEquals(10, 1 // R(1, 10))
- self.assertTypedEquals(10**23, 10**22 // R(1, 10))
- self.assertTypedEquals(10, 1.0 // R(1, 10))
-
- self.assertTypedEquals(R(1, 10), R(1, 10) % 1)
- self.assertTypedEquals(0.1, R(1, 10) % 1.0)
- self.assertTypedEquals(R(0, 1), 1 % R(1, 10))
- self.assertTypedEquals(0.0, 1.0 % R(1, 10))
+ self.assertTypedEquals(F(11, 10), F(1, 10) + 1)
+ self.assertTypedEquals(1.1, F(1, 10) + 1.0)
+ self.assertTypedEquals(1.1 + 0j, F(1, 10) + (1.0 + 0j))
+ self.assertTypedEquals(F(11, 10), 1 + F(1, 10))
+ self.assertTypedEquals(1.1, 1.0 + F(1, 10))
+ self.assertTypedEquals(1.1 + 0j, (1.0 + 0j) + F(1, 10))
+
+ self.assertTypedEquals(F(-9, 10), F(1, 10) - 1)
+ self.assertTypedEquals(-0.9, F(1, 10) - 1.0)
+ self.assertTypedEquals(-0.9 + 0j, F(1, 10) - (1.0 + 0j))
+ self.assertTypedEquals(F(9, 10), 1 - F(1, 10))
+ self.assertTypedEquals(0.9, 1.0 - F(1, 10))
+ self.assertTypedEquals(0.9 + 0j, (1.0 + 0j) - F(1, 10))
+
+ self.assertTypedEquals(F(1, 10), F(1, 10) * 1)
+ self.assertTypedEquals(0.1, F(1, 10) * 1.0)
+ self.assertTypedEquals(0.1 + 0j, F(1, 10) * (1.0 + 0j))
+ self.assertTypedEquals(F(1, 10), 1 * F(1, 10))
+ self.assertTypedEquals(0.1, 1.0 * F(1, 10))
+ self.assertTypedEquals(0.1 + 0j, (1.0 + 0j) * F(1, 10))
+
+ self.assertTypedEquals(F(1, 10), F(1, 10) / 1)
+ self.assertTypedEquals(0.1, F(1, 10) / 1.0)
+ self.assertTypedEquals(0.1 + 0j, F(1, 10) / (1.0 + 0j))
+ self.assertTypedEquals(F(10, 1), 1 / F(1, 10))
+ self.assertTypedEquals(10.0, 1.0 / F(1, 10))
+ self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / F(1, 10))
+
+ self.assertTypedEquals(0, F(1, 10) // 1)
+ self.assertTypedEquals(0, F(1, 10) // 1.0)
+ self.assertTypedEquals(10, 1 // F(1, 10))
+ self.assertTypedEquals(10**23, 10**22 // F(1, 10))
+ self.assertTypedEquals(10, 1.0 // F(1, 10))
+
+ self.assertTypedEquals(F(1, 10), F(1, 10) % 1)
+ self.assertTypedEquals(0.1, F(1, 10) % 1.0)
+ self.assertTypedEquals(F(0, 1), 1 % F(1, 10))
+ self.assertTypedEquals(0.0, 1.0 % F(1, 10))
# No need for divmod since we don't override it.
# ** has more interesting conversion rules.
- self.assertTypedEquals(R(100, 1), R(1, 10) ** -2)
- self.assertTypedEquals(R(100, 1), R(10, 1) ** 2)
- self.assertTypedEquals(0.1, R(1, 10) ** 1.0)
- self.assertTypedEquals(0.1 + 0j, R(1, 10) ** (1.0 + 0j))
- self.assertTypedEquals(4 , 2 ** R(2, 1))
- z = pow(-1, R(1, 2))
+ self.assertTypedEquals(F(100, 1), F(1, 10) ** -2)
+ self.assertTypedEquals(F(100, 1), F(10, 1) ** 2)
+ self.assertTypedEquals(0.1, F(1, 10) ** 1.0)
+ self.assertTypedEquals(0.1 + 0j, F(1, 10) ** (1.0 + 0j))
+ self.assertTypedEquals(4 , 2 ** F(2, 1))
+ z = pow(-1, F(1, 2))
self.assertAlmostEquals(0, z.real)
self.assertEquals(1, z.imag)
- self.assertTypedEquals(R(1, 4) , 2 ** R(-2, 1))
- self.assertTypedEquals(2.0 , 4 ** R(1, 2))
- self.assertTypedEquals(0.25, 2.0 ** R(-2, 1))
- self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** R(1, 10))
+ self.assertTypedEquals(F(1, 4) , 2 ** F(-2, 1))
+ self.assertTypedEquals(2.0 , 4 ** F(1, 2))
+ self.assertTypedEquals(0.25, 2.0 ** F(-2, 1))
+ self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10))
def testMixingWithDecimal(self):
# Decimal refuses mixed comparisons.
self.assertRaisesMessage(
TypeError,
"unsupported operand type(s) for +: 'Fraction' and 'Decimal'",
- operator.add, R(3,11), Decimal('3.1415926'))
- self.assertNotEquals(R(5, 2), Decimal('2.5'))
+ operator.add, F(3,11), Decimal('3.1415926'))
+ self.assertNotEquals(F(5, 2), Decimal('2.5'))
def testComparisons(self):
- self.assertTrue(R(1, 2) < R(2, 3))
- self.assertFalse(R(1, 2) < R(1, 2))
- self.assertTrue(R(1, 2) <= R(2, 3))
- self.assertTrue(R(1, 2) <= R(1, 2))
- self.assertFalse(R(2, 3) <= R(1, 2))
- self.assertTrue(R(1, 2) == R(1, 2))
- self.assertFalse(R(1, 2) == R(1, 3))
- self.assertFalse(R(1, 2) != R(1, 2))
- self.assertTrue(R(1, 2) != R(1, 3))
+ self.assertTrue(F(1, 2) < F(2, 3))
+ self.assertFalse(F(1, 2) < F(1, 2))
+ self.assertTrue(F(1, 2) <= F(2, 3))
+ self.assertTrue(F(1, 2) <= F(1, 2))
+ self.assertFalse(F(2, 3) <= F(1, 2))
+ self.assertTrue(F(1, 2) == F(1, 2))
+ self.assertFalse(F(1, 2) == F(1, 3))
+ self.assertFalse(F(1, 2) != F(1, 2))
+ self.assertTrue(F(1, 2) != F(1, 3))
def testMixedLess(self):
- self.assertTrue(2 < R(5, 2))
- self.assertFalse(2 < R(4, 2))
- self.assertTrue(R(5, 2) < 3)
- self.assertFalse(R(4, 2) < 2)
+ self.assertTrue(2 < F(5, 2))
+ self.assertFalse(2 < F(4, 2))
+ self.assertTrue(F(5, 2) < 3)
+ self.assertFalse(F(4, 2) < 2)
- self.assertTrue(R(1, 2) < 0.6)
- self.assertFalse(R(1, 2) < 0.4)
- self.assertTrue(0.4 < R(1, 2))
- self.assertFalse(0.5 < R(1, 2))
+ self.assertTrue(F(1, 2) < 0.6)
+ self.assertFalse(F(1, 2) < 0.4)
+ self.assertTrue(0.4 < F(1, 2))
+ self.assertFalse(0.5 < F(1, 2))
def testMixedLessEqual(self):
- self.assertTrue(0.5 <= R(1, 2))
- self.assertFalse(0.6 <= R(1, 2))
- self.assertTrue(R(1, 2) <= 0.5)
- self.assertFalse(R(1, 2) <= 0.4)
- self.assertTrue(2 <= R(4, 2))
- self.assertFalse(2 <= R(3, 2))
- self.assertTrue(R(4, 2) <= 2)
- self.assertFalse(R(5, 2) <= 2)
+ self.assertTrue(0.5 <= F(1, 2))
+ self.assertFalse(0.6 <= F(1, 2))
+ self.assertTrue(F(1, 2) <= 0.5)
+ self.assertFalse(F(1, 2) <= 0.4)
+ self.assertTrue(2 <= F(4, 2))
+ self.assertFalse(2 <= F(3, 2))
+ self.assertTrue(F(4, 2) <= 2)
+ self.assertFalse(F(5, 2) <= 2)
def testBigFloatComparisons(self):
# Because 10**23 can't be represented exactly as a float:
- self.assertFalse(R(10**23) == float(10**23))
+ self.assertFalse(F(10**23) == float(10**23))
# The first test demonstrates why these are important.
- self.assertFalse(1e23 < float(R(math.trunc(1e23) + 1)))
- self.assertTrue(1e23 < R(math.trunc(1e23) + 1))
- self.assertFalse(1e23 <= R(math.trunc(1e23) - 1))
- self.assertTrue(1e23 > R(math.trunc(1e23) - 1))
- self.assertFalse(1e23 >= R(math.trunc(1e23) + 1))
+ self.assertFalse(1e23 < float(F(math.trunc(1e23) + 1)))
+ self.assertTrue(1e23 < F(math.trunc(1e23) + 1))
+ self.assertFalse(1e23 <= F(math.trunc(1e23) - 1))
+ self.assertTrue(1e23 > F(math.trunc(1e23) - 1))
+ self.assertFalse(1e23 >= F(math.trunc(1e23) + 1))
def testBigComplexComparisons(self):
- self.assertFalse(R(10**23) == complex(10**23))
- self.assertTrue(R(10**23) > complex(10**23))
- self.assertFalse(R(10**23) <= complex(10**23))
+ self.assertFalse(F(10**23) == complex(10**23))
+ self.assertTrue(F(10**23) > complex(10**23))
+ self.assertFalse(F(10**23) <= complex(10**23))
def testMixedEqual(self):
- self.assertTrue(0.5 == R(1, 2))
- self.assertFalse(0.6 == R(1, 2))
- self.assertTrue(R(1, 2) == 0.5)
- self.assertFalse(R(1, 2) == 0.4)
- self.assertTrue(2 == R(4, 2))
- self.assertFalse(2 == R(3, 2))
- self.assertTrue(R(4, 2) == 2)
- self.assertFalse(R(5, 2) == 2)
+ self.assertTrue(0.5 == F(1, 2))
+ self.assertFalse(0.6 == F(1, 2))
+ self.assertTrue(F(1, 2) == 0.5)
+ self.assertFalse(F(1, 2) == 0.4)
+ self.assertTrue(2 == F(4, 2))
+ self.assertFalse(2 == F(3, 2))
+ self.assertTrue(F(4, 2) == 2)
+ self.assertFalse(F(5, 2) == 2)
def testStringification(self):
- self.assertEquals("Fraction(7,3)", repr(R(7, 3)))
- self.assertEquals("7/3", str(R(7, 3)))
- self.assertEquals("7", str(R(7, 1)))
+ self.assertEquals("Fraction(7, 3)", repr(F(7, 3)))
+ self.assertEquals("7/3", str(F(7, 3)))
+ self.assertEquals("7", str(F(7, 1)))
def testHash(self):
- self.assertEquals(hash(2.5), hash(R(5, 2)))
- self.assertEquals(hash(10**50), hash(R(10**50)))
- self.assertNotEquals(hash(float(10**23)), hash(R(10**23)))
+ self.assertEquals(hash(2.5), hash(F(5, 2)))
+ self.assertEquals(hash(10**50), hash(F(10**50)))
+ self.assertNotEquals(hash(float(10**23)), hash(F(10**23)))
def testApproximatePi(self):
# Algorithm borrowed from
# http://docs.python.org/lib/decimal-recipes.html
- three = R(3)
+ three = F(3)
lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
- while abs(s - lasts) > R(1, 10**9):
+ while abs(s - lasts) > F(1, 10**9):
lasts = s
n, na = n+na, na+8
d, da = d+da, da+32
@@ -403,9 +388,9 @@ class FractionTest(unittest.TestCase):
def testApproximateCos1(self):
# Algorithm borrowed from
# http://docs.python.org/lib/decimal-recipes.html
- x = R(1)
- i, lasts, s, fact, num, sign = 0, 0, R(1), 1, 1, 1
- while abs(s - lasts) > R(1, 10**9):
+ x = F(1)
+ i, lasts, s, fact, num, sign = 0, 0, F(1), 1, 1, 1
+ while abs(s - lasts) > F(1, 10**9):
lasts = s
i += 2
fact *= i * (i-1)
@@ -415,7 +400,7 @@ class FractionTest(unittest.TestCase):
self.assertAlmostEquals(math.cos(1), s)
def test_copy_deepcopy_pickle(self):
- r = R(13, 7)
+ r = F(13, 7)
self.assertEqual(r, loads(dumps(r)))
self.assertEqual(id(r), id(copy(r)))
self.assertEqual(id(r), id(deepcopy(r)))