mirror of
https://github.com/python/cpython.git
synced 2025-07-30 06:34:15 +00:00
Minor clean-up and more tests.
This commit is contained in:
parent
cf10926088
commit
eb461904eb
2 changed files with 15 additions and 2 deletions
|
@ -179,7 +179,9 @@ class Rational(RationalAbc):
|
||||||
for e in reversed(seq):
|
for e in reversed(seq):
|
||||||
n, d = d, n
|
n, d = d, n
|
||||||
n += e * d
|
n += e * d
|
||||||
return cls(n, d)
|
if seq:
|
||||||
|
return cls(n, d)
|
||||||
|
return cls(0)
|
||||||
|
|
||||||
def as_continued_fraction(self):
|
def as_continued_fraction(self):
|
||||||
'Return continued fraction expressed as a list'
|
'Return continued fraction expressed as a list'
|
||||||
|
@ -200,7 +202,7 @@ class Rational(RationalAbc):
|
||||||
# Still needs rounding rules as specified at
|
# Still needs rounding rules as specified at
|
||||||
# http://en.wikipedia.org/wiki/Continued_fraction
|
# http://en.wikipedia.org/wiki/Continued_fraction
|
||||||
cf = cls.from_float(f).as_continued_fraction()
|
cf = cls.from_float(f).as_continued_fraction()
|
||||||
result = new = Rational(0, 1)
|
result = Rational(0)
|
||||||
for i in range(1, len(cf)):
|
for i in range(1, len(cf)):
|
||||||
new = cls.from_continued_fraction(cf[:i])
|
new = cls.from_continued_fraction(cf[:i])
|
||||||
if new.denominator > max_denominator:
|
if new.denominator > max_denominator:
|
||||||
|
|
|
@ -140,12 +140,23 @@ class RationalTest(unittest.TestCase):
|
||||||
phi = R.from_continued_fraction([1]*100)
|
phi = R.from_continued_fraction([1]*100)
|
||||||
self.assertEquals(round(phi - (1 + 5 ** 0.5) / 2, 10), 0.0)
|
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):
|
def testAsContinuedFraction(self):
|
||||||
self.assertEqual(R.from_float(math.pi).as_continued_fraction()[:15],
|
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])
|
[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 testApproximateFromFloat(self):
|
def testApproximateFromFloat(self):
|
||||||
self.assertEqual(R.approximate_from_float(math.pi, 10000), R(355, 113))
|
self.assertEqual(R.approximate_from_float(math.pi, 10000), R(355, 113))
|
||||||
|
self.assertEqual(R.approximate_from_float(-math.pi, 10000), R(-355, 113))
|
||||||
|
self.assertEqual(R.approximate_from_float(0.0, 10000), R(0))
|
||||||
|
|
||||||
def testConversions(self):
|
def testConversions(self):
|
||||||
self.assertTypedEquals(-1, trunc(R(-11, 10)))
|
self.assertTypedEquals(-1, trunc(R(-11, 10)))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue