mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
Issue 3285: Fractions from_float() and from_decimal() accept Integral arguments.
This commit is contained in:
parent
3cd1e42dca
commit
b01713e7dc
3 changed files with 12 additions and 10 deletions
|
|
@ -110,7 +110,9 @@ class Fraction(Rational):
|
|||
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)" %
|
||||
(cls.__name__, f, type(f).__name__))
|
||||
if math.isnan(f) or math.isinf(f):
|
||||
|
|
@ -121,7 +123,9 @@ class Fraction(Rational):
|
|||
def from_decimal(cls, dec):
|
||||
"""Converts a finite Decimal instance to a rational number, exactly."""
|
||||
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(
|
||||
"%s.from_decimal() only takes Decimals, not %r (%s)" %
|
||||
(cls.__name__, dec, type(dec).__name__))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue