Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,24 +339,24 @@ def from_float(cls, f):
Beware that Fraction.from_float(0.3) != Fraction(3, 10).

"""
if isinstance(f, float):
return cls._from_coprime_ints(*f.as_integer_ratio())
Comment on lines +342 to +343

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a subclass of float, f.as_integer_ratio() doesn't necessarily return a normalised integer pair. I think we should restrict this optimisation to exact floats.

Suggested change
if isinstance(f, float):
return cls._from_coprime_ints(*f.as_integer_ratio())
if type(f) is float:
return cls._from_coprime_ints(*f.as_integer_ratio())

The intention is to optimise the common case, and that is certainly (and by a margin) exact float, not arbitrary subclasses.
We then need to keep the fallback code that handles subclasses, as it was before.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This affects only float subclasses. The float.as_integer_ration was already documented to return coprime ints.

if isinstance(f, numbers.Integral):
return cls(f)
elif not isinstance(f, float):
raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
(cls.__name__, f, type(f).__name__))
return cls._from_coprime_ints(*f.as_integer_ratio())
raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
(cls.__name__, f, type(f).__name__))

@classmethod
def from_decimal(cls, dec):
"""Converts a finite Decimal instance to a rational number, exactly."""
from decimal import Decimal
if isinstance(dec, Decimal):
return cls._from_coprime_ints(*dec.as_integer_ratio())
Comment on lines +353 to +354

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if isinstance(dec, Decimal):
return cls._from_coprime_ints(*dec.as_integer_ratio())
if type(dec) is Decimal:
return cls._from_coprime_ints(*dec.as_integer_ratio())

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

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__))
return cls._from_coprime_ints(*dec.as_integer_ratio())
dec = int(dec)
return cls._from_coprime_ints(*dec.as_integer_ratio())
raise TypeError("%s.from_decimal() only takes Decimals, not %r (%s)" %
(cls.__name__, dec, type(dec).__name__))

@classmethod
def _from_coprime_ints(cls, numerator, denominator, /):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Optimize (~x1.4 speedup) :meth:`fractions.Fraction.from_decimal` and
:meth:`fractions.Fraction.from_float` for :class:`~decimal.Decimal` and
:class:`float` inputs, respectively. Patch by Sergey B Kirpichev.
Loading