Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60752 | mark.dickinson | 2008-02-12 22:31:59 +0100 (Tue, 12 Feb 2008) | 5 lines
Implementation of Fraction.limit_denominator.
Remove Fraction.to_continued_fraction and
Fraction.from_continued_fraction
........
r60754 | mark.dickinson | 2008-02-12 22:40:53 +0100 (Tue, 12 Feb 2008) | 3 lines
Revert change in r60712: turn alternate constructors back into
classmethods instead of staticmethods.
........
r60755 | mark.dickinson | 2008-02-12 22:46:54 +0100 (Tue, 12 Feb 2008) | 4 lines
Replace R=fractions.Fraction with F=fractions.Fraction in
test_fractions.py. This should have been part of the name
change from Rational to Fraction.
........
r60758 | georg.brandl | 2008-02-13 08:20:22 +0100 (Wed, 13 Feb 2008) | 3 lines
#2063: correct order of utime and stime in os.times()
result on Windows.
........
r60762 | jeffrey.yasskin | 2008-02-13 18:58:04 +0100 (Wed, 13 Feb 2008) | 7 lines
Working on issue #1762: Brought
./python.exe -m timeit -s 'from fractions import Fraction; f = Fraction(3, 2)' 'isinstance(3, Fraction); isinstance(f, Fraction)'
from 12.3 usec/loop to 3.44 usec/loop and
./python.exe -m timeit -s 'from fractions import Fraction' 'Fraction(3, 2)'
from 48.8 usec to 23.6 usec by avoiding genexps and sets in __instancecheck__
and inlining the common case from __subclasscheck__.
........
r60765 | brett.cannon | 2008-02-13 20:15:44 +0100 (Wed, 13 Feb 2008) | 5 lines
Fix --enable-universalsdk and its comment line so that zsh's flag completion
works.
Thanks to Jeroen Ruigrok van der Werven for the fix.
........
r60771 | kurt.kaiser | 2008-02-14 01:08:55 +0100 (Thu, 14 Feb 2008) | 2 lines
Bring NEWS.txt up to date from check-in msgs.
........
r60772 | raymond.hettinger | 2008-02-14 02:08:02 +0100 (Thu, 14 Feb 2008) | 3 lines
Update notes on Decimal.
........
r60773 | raymond.hettinger | 2008-02-14 03:41:22 +0100 (Thu, 14 Feb 2008) | 1 line
Fix decimal repr which should have used single quotes like other reprs.
........
r60785 | jeffrey.yasskin | 2008-02-14 07:12:24 +0100 (Thu, 14 Feb 2008) | 11 lines
Performance optimizations on Fraction's constructor.
./python.exe -m timeit -s 'from fractions import Fraction' 'Fraction(3)`
31.7 usec/loop -> 9.2 usec/loop
./python.exe -m timeit -s 'from fractions import Fraction' 'Fraction(3, 2)'`
27.7 usec/loop -> 9.32 usec/loop
./python.exe -m timeit -s 'from fractions import Fraction; f = Fraction(3, 2)' 'Fraction(f)'
31.9 usec/loop -> 14.3 usec/loop
........
r60786 | jeffrey.yasskin | 2008-02-14 08:49:25 +0100 (Thu, 14 Feb 2008) | 5 lines
Change simple instances (in Fraction) of self.numerator and self.denominator to
self._numerator and self._denominator. This speeds abs() up from 12.2us to
10.8us and trunc() from 2.07us to 1.11us. This doesn't change _add and friends
because they're more complicated.
........
diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py
index 884dd32..80b0123 100644
--- a/Lib/test/test_abc.py
+++ b/Lib/test/test_abc.py
@@ -81,6 +81,16 @@
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 1ef0ad3..ae36dcb 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -1040,7 +1040,7 @@
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 00fd549..dacb587 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -8,7 +8,7 @@
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 @@
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 @@
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"))
+ F.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))
+ 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(-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.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.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(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(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(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(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(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(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(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, 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(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(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(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 @@
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 @@
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)))