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