Converts a finite float to a rational number, exactly. Beware that Fraction.from_float(0.3) != Fraction(3, 10).
(cls, f)
| 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): |