MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / from_float

Method from_float

tools/python-3.11.9-amd64/Lib/fractions.py:169–180  ·  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

167
168 @classmethod
169 def from_float(cls, f):
170 """Converts a finite float to a rational number, exactly.
171
172 Beware that Fraction.from_float(0.3) != Fraction(3, 10).
173
174 """
175 if isinstance(f, numbers.Integral):
176 return cls(f)
177 elif not isinstance(f, float):
178 raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
179 (cls.__name__, f, type(f).__name__))
180 return cls(*f.as_integer_ratio())
181
182 @classmethod
183 def from_decimal(cls, dec):

Callers 2

_richcmpMethod · 0.95
__eq__Method · 0.45

Calls 2

typeClass · 0.50
as_integer_ratioMethod · 0.45

Tested by

no test coverage detected