Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b33d011b
Commit
b33d011b
authored
Jan 25, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix-up signature for approximation.
parent
a5dafec4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
9 deletions
+10
-9
Lib/rational.py
Lib/rational.py
+6
-5
Lib/test/test_rational.py
Lib/test/test_rational.py
+4
-4
No files found.
Lib/rational.py
View file @
b33d011b
...
@@ -195,16 +195,17 @@ class Rational(RationalAbc):
...
@@ -195,16 +195,17 @@ class Rational(RationalAbc):
n
,
d
=
d
,
n
n
,
d
=
d
,
n
return
cf
return
cf
@
classmethod
def
approximate
(
self
,
max_denominator
):
def
approximate_from_float
(
cls
,
f
,
max_denominator
):
'Best rational approximation with a denominator <= max_denominator'
'Best rational approximation to f with a denominator <= max_denominator'
# XXX First cut at algorithm
# XXX First cut at algorithm
# 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
()
if
self
.
denominator
<=
max_denominator
:
return
self
cf
=
self
.
as_continued_fraction
()
result
=
Rational
(
0
)
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
=
self
.
from_continued_fraction
(
cf
[:
i
])
if
new
.
denominator
>
max_denominator
:
if
new
.
denominator
>
max_denominator
:
break
break
result
=
new
result
=
new
...
...
Lib/test/test_rational.py
View file @
b33d011b
...
@@ -155,10 +155,10 @@ class RationalTest(unittest.TestCase):
...
@@ -155,10 +155,10 @@ class RationalTest(unittest.TestCase):
[
-
4
,
1
,
6
,
15
,
1
,
292
,
1
,
1
,
1
,
2
,
1
,
3
,
1
,
14
,
3
,
3
])
[
-
4
,
1
,
6
,
15
,
1
,
292
,
1
,
1
,
1
,
2
,
1
,
3
,
1
,
14
,
3
,
3
])
self
.
assertEqual
(
R
(
0
).
as_continued_fraction
(),
[
0
])
self
.
assertEqual
(
R
(
0
).
as_continued_fraction
(),
[
0
])
def
testApproximateFrom
Float
(
self
):
def
testApproximateFrom
(
self
):
self
.
assertEqual
(
R
.
approximate_from_float
(
math
.
pi
,
10000
),
R
(
355
,
113
))
self
.
assertEqual
(
R
.
from_float
(
math
.
pi
).
approximate
(
10000
),
R
(
355
,
113
))
self
.
assertEqual
(
R
.
approximate_from_float
(
-
math
.
pi
,
10000
),
R
(
-
355
,
113
))
self
.
assertEqual
(
R
.
from_float
(
-
math
.
pi
).
approximate
(
10000
),
R
(
-
355
,
113
))
self
.
assertEqual
(
R
.
approximate_from_float
(
0.0
,
10000
),
R
(
0
))
self
.
assertEqual
(
R
.
from_float
(
0.0
).
approximate
(
10000
),
R
(
0
))
def
testConversions
(
self
):
def
testConversions
(
self
):
self
.
assertTypedEquals
(
-
1
,
trunc
(
R
(
-
11
,
10
)))
self
.
assertTypedEquals
(
-
1
,
trunc
(
R
(
-
11
,
10
)))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment