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
b01713e7
Kaydet (Commit)
b01713e7
authored
Tem 10, 2008
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue 3285: Fractions from_float() and from_decimal() accept Integral arguments.
üst
3cd1e42d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
12 additions
and
10 deletions
+12
-10
fractions.py
Lib/fractions.py
+6
-2
test_fractions.py
Lib/test/test_fractions.py
+4
-8
NEWS
Misc/NEWS
+2
-0
No files found.
Lib/fractions.py
Dosyayı görüntüle @
b01713e7
...
@@ -110,7 +110,9 @@ class Fraction(Rational):
...
@@ -110,7 +110,9 @@ class Fraction(Rational):
Beware that Fraction.from_float(0.3) != Fraction(3, 10).
Beware that Fraction.from_float(0.3) != Fraction(3, 10).
"""
"""
if
not
isinstance
(
f
,
float
):
if
isinstance
(
f
,
numbers
.
Integral
):
f
=
float
(
f
)
elif
not
isinstance
(
f
,
float
):
raise
TypeError
(
"
%
s.from_float() only takes floats, not
%
r (
%
s)"
%
raise
TypeError
(
"
%
s.from_float() only takes floats, not
%
r (
%
s)"
%
(
cls
.
__name__
,
f
,
type
(
f
)
.
__name__
))
(
cls
.
__name__
,
f
,
type
(
f
)
.
__name__
))
if
math
.
isnan
(
f
)
or
math
.
isinf
(
f
):
if
math
.
isnan
(
f
)
or
math
.
isinf
(
f
):
...
@@ -121,7 +123,9 @@ class Fraction(Rational):
...
@@ -121,7 +123,9 @@ class Fraction(Rational):
def
from_decimal
(
cls
,
dec
):
def
from_decimal
(
cls
,
dec
):
"""Converts a finite Decimal instance to a rational number, exactly."""
"""Converts a finite Decimal instance to a rational number, exactly."""
from
decimal
import
Decimal
from
decimal
import
Decimal
if
not
isinstance
(
dec
,
Decimal
):
if
isinstance
(
dec
,
numbers
.
Integral
):
dec
=
Decimal
(
int
(
dec
))
elif
not
isinstance
(
dec
,
Decimal
):
raise
TypeError
(
raise
TypeError
(
"
%
s.from_decimal() only takes Decimals, not
%
r (
%
s)"
%
"
%
s.from_decimal() only takes Decimals, not
%
r (
%
s)"
%
(
cls
.
__name__
,
dec
,
type
(
dec
)
.
__name__
))
(
cls
.
__name__
,
dec
,
type
(
dec
)
.
__name__
))
...
...
Lib/test/test_fractions.py
Dosyayı görüntüle @
b01713e7
...
@@ -137,10 +137,8 @@ class FractionTest(unittest.TestCase):
...
@@ -137,10 +137,8 @@ class FractionTest(unittest.TestCase):
self
.
assertNotEquals
(
F
(
4
,
2
),
r
)
self
.
assertNotEquals
(
F
(
4
,
2
),
r
)
def
testFromFloat
(
self
):
def
testFromFloat
(
self
):
self
.
assertRaisesMessage
(
self
.
assertRaises
(
TypeError
,
F
.
from_float
,
3
+
4
j
)
TypeError
,
"Fraction.from_float() only takes floats, not 3 (int)"
,
self
.
assertEquals
((
10
,
1
),
_components
(
F
.
from_float
(
10
)))
F
.
from_float
,
3
)
self
.
assertEquals
((
0
,
1
),
_components
(
F
.
from_float
(
-
0.0
)))
self
.
assertEquals
((
0
,
1
),
_components
(
F
.
from_float
(
-
0.0
)))
self
.
assertEquals
((
10
,
1
),
_components
(
F
.
from_float
(
10.0
)))
self
.
assertEquals
((
10
,
1
),
_components
(
F
.
from_float
(
10.0
)))
self
.
assertEquals
((
-
5
,
2
),
_components
(
F
.
from_float
(
-
2.5
)))
self
.
assertEquals
((
-
5
,
2
),
_components
(
F
.
from_float
(
-
2.5
)))
...
@@ -164,10 +162,8 @@ class FractionTest(unittest.TestCase):
...
@@ -164,10 +162,8 @@ class FractionTest(unittest.TestCase):
F
.
from_float
,
nan
)
F
.
from_float
,
nan
)
def
testFromDecimal
(
self
):
def
testFromDecimal
(
self
):
self
.
assertRaisesMessage
(
self
.
assertRaises
(
TypeError
,
F
.
from_decimal
,
3
+
4
j
)
TypeError
,
self
.
assertEquals
(
F
(
10
,
1
),
F
.
from_decimal
(
10
))
"Fraction.from_decimal() only takes Decimals, not 3 (int)"
,
F
.
from_decimal
,
3
)
self
.
assertEquals
(
F
(
0
),
F
.
from_decimal
(
Decimal
(
"-0"
)))
self
.
assertEquals
(
F
(
0
),
F
.
from_decimal
(
Decimal
(
"-0"
)))
self
.
assertEquals
(
F
(
5
,
10
),
F
.
from_decimal
(
Decimal
(
"0.5"
)))
self
.
assertEquals
(
F
(
5
,
10
),
F
.
from_decimal
(
Decimal
(
"0.5"
)))
self
.
assertEquals
(
F
(
5
,
1000
),
F
.
from_decimal
(
Decimal
(
"5e-3"
)))
self
.
assertEquals
(
F
(
5
,
1000
),
F
.
from_decimal
(
Decimal
(
"5e-3"
)))
...
...
Misc/NEWS
Dosyayı görüntüle @
b01713e7
...
@@ -41,6 +41,8 @@ Core and Builtins
...
@@ -41,6 +41,8 @@ Core and Builtins
Library
Library
-------
-------
- Issue #3285: Fractions from_float() and from_decimal() accept Integral arguments.
- Issue #3301: Bisect module modules behaved badly when lo was negative.
- Issue #3301: Bisect module modules behaved badly when lo was negative.
- Issue #839496: SimpleHTTPServer used to open text files in text mode. This is
- Issue #839496: SimpleHTTPServer used to open text files in text mode. This is
...
...
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