summaryrefslogtreecommitdiffstats
path: root/Lib/rational.py
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-01-24 09:42:52 (GMT)
committerChristian Heimes <christian@cheimes.de>2008-01-24 09:42:52 (GMT)
commitbbffeb61e1c981919d328f0c135d66629b0bac51 (patch)
tree19322d66f4763316126c50df3c200c665753d5c2 /Lib/rational.py
parent39b21ffd3c54e7f6b6d32fde5c91bbfad96084db (diff)
downloadcpython-bbffeb61e1c981919d328f0c135d66629b0bac51.zip
cpython-bbffeb61e1c981919d328f0c135d66629b0bac51.tar.gz
cpython-bbffeb61e1c981919d328f0c135d66629b0bac51.tar.bz2
Merged revisions 60210-60233 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r60213 | christian.heimes | 2008-01-23 15:00:25 +0100 (Wed, 23 Jan 2008) | 1 line Use Py_TYPE() instead of ->ob_type ........ r60214 | armin.rigo | 2008-01-23 15:07:13 +0100 (Wed, 23 Jan 2008) | 3 lines patch 1754489 by vlahan: improve portability of address length calculation for AF_UNIX sockets ........ r60216 | christian.heimes | 2008-01-23 15:20:50 +0100 (Wed, 23 Jan 2008) | 1 line Fixed bug #1915: Python compiles with --enable-unicode=no again. However several extension methods and modules do not work without unicode support. ........ r60221 | christian.heimes | 2008-01-23 18:15:06 +0100 (Wed, 23 Jan 2008) | 2 lines Applied #1069410 The "can't load dll" message box on Windows is suppressed while an extension is loaded by calling SetErrorMode in dynload_win.c. The error is still reported properly. ........ r60224 | guido.van.rossum | 2008-01-23 21:19:01 +0100 (Wed, 23 Jan 2008) | 2 lines Fix two crashers. ........ r60225 | kurt.kaiser | 2008-01-23 23:19:23 +0100 (Wed, 23 Jan 2008) | 3 lines Could not open files in .idlerc directory if latter was hidden on Windows. Issue 1743, Issue 1862. ........ r60226 | guido.van.rossum | 2008-01-23 23:43:27 +0100 (Wed, 23 Jan 2008) | 2 lines Fix misleading comment reported in issue #1917. ........ r60227 | kurt.kaiser | 2008-01-23 23:55:26 +0100 (Wed, 23 Jan 2008) | 2 lines There was an error on exit if no sys.exitfunc was defined. Issue 1647. ........ r60228 | guido.van.rossum | 2008-01-24 00:23:43 +0100 (Thu, 24 Jan 2008) | 2 lines Turn three recently fixed crashers into regular tests. ........ r60229 | raymond.hettinger | 2008-01-24 01:54:21 +0100 (Thu, 24 Jan 2008) | 1 line Add first-cut at an approximation function (still needs rounding tweaks). Add continued fraction conversions. ........ r60230 | raymond.hettinger | 2008-01-24 03:00:25 +0100 (Thu, 24 Jan 2008) | 1 line Minor clean-up and more tests. ........ r60231 | raymond.hettinger | 2008-01-24 03:05:06 +0100 (Thu, 24 Jan 2008) | 1 line Cleanup ........ r60232 | neal.norwitz | 2008-01-24 05:14:50 +0100 (Thu, 24 Jan 2008) | 1 line Fix the tests by restoring __import__. I think the test is still valid. ........ r60233 | neal.norwitz | 2008-01-24 08:40:51 +0100 (Thu, 24 Jan 2008) | 4 lines Fix the test_urllib2net failures that were caused by r58067. I'm not sure this is the correct fix, but at least the test passes now and should be closer to correct. ........
Diffstat (limited to 'Lib/rational.py')
-rwxr-xr-xLib/rational.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/rational.py b/Lib/rational.py
index 71ffff7..4a56cf2 100755
--- a/Lib/rational.py
+++ b/Lib/rational.py
@@ -171,6 +171,42 @@ class Rational(RationalAbc):
else:
return cls(digits, 10 ** -exp)
+ @classmethod
+ def from_continued_fraction(cls, seq):
+ 'Build a Rational 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
+
+ @classmethod
+ def approximate_from_float(cls, f, max_denominator):
+ 'Best rational approximation to f with a denominator <= max_denominator'
+ # XXX First cut at algorithm
+ # Still needs rounding rules as specified at
+ # http://en.wikipedia.org/wiki/Continued_fraction
+ cf = cls.from_float(f).as_continued_fraction()
+ result = Rational(0)
+ for i in range(1, len(cf)):
+ new = cls.from_continued_fraction(cf[:i])
+ if new.denominator > max_denominator:
+ break
+ result = new
+ return result
+
@property
def numerator(a):
return a._numerator