diff options
author | Raymond Hettinger <python@rcn.com> | 2008-01-25 01:23:38 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-01-25 01:23:38 (GMT) |
commit | 9c6d81f5dd083ad536106bf723f705d70ddbe258 (patch) | |
tree | 3f5956a0a872004383221faf5465d6716be3db29 /Lib/rational.py | |
parent | 7ea82253ea6ed6d8930630f5b786489ff8817dcb (diff) | |
download | cpython-9c6d81f5dd083ad536106bf723f705d70ddbe258.zip cpython-9c6d81f5dd083ad536106bf723f705d70ddbe258.tar.gz cpython-9c6d81f5dd083ad536106bf723f705d70ddbe258.tar.bz2 |
Fix-up signature for approximation.
Diffstat (limited to 'Lib/rational.py')
-rwxr-xr-x | Lib/rational.py | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/Lib/rational.py b/Lib/rational.py index 40b9163..d23f433 100755 --- a/Lib/rational.py +++ b/Lib/rational.py @@ -195,16 +195,17 @@ class Rational(RationalAbc): 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' + 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 - cf = cls.from_float(f).as_continued_fraction() + if self.denominator <= max_denominator: + return self + cf = self.as_continued_fraction() result = Rational(0) for i in range(1, len(cf)): - new = cls.from_continued_fraction(cf[:i]) + new = self.from_continued_fraction(cf[:i]) if new.denominator > max_denominator: break result = new |