Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
9c6d81f5
Kaydet (Commit)
9c6d81f5
authored
Ock 25, 2008
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fix-up signature for approximation.
üst
7ea82253
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
9 deletions
+10
-9
rational.py
Lib/rational.py
+6
-5
test_rational.py
Lib/test/test_rational.py
+4
-4
No files found.
Lib/rational.py
Dosyayı görüntüle @
9c6d81f5
...
@@ -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
Dosyayı görüntüle @
9c6d81f5
...
@@ -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