MCPcopy Index your code
hub / github.com/python/cpython / from_float

Method from_float

Lib/fractions.py:336–347  ·  view source on GitHub ↗

Converts a finite float to a rational number, exactly. Beware that Fraction.from_float(0.3) != Fraction(3, 10).

(cls, f)

Source from the content-addressed store, hash-verified

334
335 @classmethod
336 def from_float(cls, f):
337 """Converts a finite float to a rational number, exactly.
338
339 Beware that Fraction.from_float(0.3) != Fraction(3, 10).
340
341 """
342 if isinstance(f, numbers.Integral):
343 return cls(f)
344 elif not isinstance(f, float):
345 raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
346 (cls.__name__, f, type(f).__name__))
347 return cls._from_coprime_ints(*f.as_integer_ratio())
348
349 @classmethod
350 def from_decimal(cls, dec):

Callers 10

_richcmpMethod · 0.95
__eq__Method · 0.45
_richcmpMethod · 0.45
testFromFloatMethod · 0.45
test_binary_floatsMethod · 0.45
test_from_numberMethod · 0.45
test_from_floatMethod · 0.45
test_float_operationMethod · 0.45

Calls 3

_from_coprime_intsMethod · 0.80
clsClass · 0.50
as_integer_ratioMethod · 0.45

Tested by 8

_richcmpMethod · 0.36
testFromFloatMethod · 0.36
test_binary_floatsMethod · 0.36
test_from_numberMethod · 0.36
test_from_floatMethod · 0.36
test_float_operationMethod · 0.36