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
0aa52a16
Kaydet (Commit)
0aa52a16
authored
Şub 12, 2008
tarafından
Mark Dickinson
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Revert change in r60712: turn alternate constructors back into
classmethods instead of staticmethods.
üst
e1b82479
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
13 deletions
+13
-13
fractions.py
Lib/fractions.py
+13
-13
No files found.
Lib/fractions.py
Dosyayı görüntüle @
0aa52a16
...
...
@@ -106,39 +106,39 @@ class Fraction(Rational):
self
.
_denominator
=
int
(
denominator
//
g
)
return
self
@
static
method
def
from_float
(
f
):
@
class
method
def
from_float
(
cls
,
f
):
"""Converts a finite float to a rational number, exactly.
Beware that Fraction.from_float(0.3) != Fraction(3, 10).
"""
if
not
isinstance
(
f
,
float
):
raise
TypeError
(
"
Fraction.from_float() only takes floats, "
"not
%
r (
%
s)"
%
(
f
,
type
(
f
)
.
__name__
))
raise
TypeError
(
"
%
s.from_float() only takes floats, not
%
r (
%
s)"
%
(
cls
.
__name__
,
f
,
type
(
f
)
.
__name__
))
if
math
.
isnan
(
f
)
or
math
.
isinf
(
f
):
raise
TypeError
(
"Cannot convert
%
r to
Fraction."
%
f
)
return
Fraction
(
*
f
.
as_integer_ratio
())
raise
TypeError
(
"Cannot convert
%
r to
%
s."
%
(
f
,
cls
.
__name__
)
)
return
cls
(
*
f
.
as_integer_ratio
())
@
static
method
def
from_decimal
(
dec
):
@
class
method
def
from_decimal
(
cls
,
dec
):
"""Converts a finite Decimal instance to a rational number, exactly."""
from
decimal
import
Decimal
if
not
isinstance
(
dec
,
Decimal
):
raise
TypeError
(
"
Fraction
.from_decimal() only takes Decimals, not
%
r (
%
s)"
%
(
dec
,
type
(
dec
)
.
__name__
))
"
%
s
.from_decimal() only takes Decimals, not
%
r (
%
s)"
%
(
cls
.
__name__
,
dec
,
type
(
dec
)
.
__name__
))
if
not
dec
.
is_finite
():
# Catches infinities and nans.
raise
TypeError
(
"Cannot convert
%
s to
Fraction."
%
dec
)
raise
TypeError
(
"Cannot convert
%
s to
%
s."
%
(
dec
,
cls
.
__name__
)
)
sign
,
digits
,
exp
=
dec
.
as_tuple
()
digits
=
int
(
''
.
join
(
map
(
str
,
digits
)))
if
sign
:
digits
=
-
digits
if
exp
>=
0
:
return
Fraction
(
digits
*
10
**
exp
)
return
cls
(
digits
*
10
**
exp
)
else
:
return
Fraction
(
digits
,
10
**
-
exp
)
return
cls
(
digits
,
10
**
-
exp
)
def
limit_denominator
(
self
,
max_denominator
=
1000000
):
"""Closest Fraction to self with denominator at most max_denominator.
...
...
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