I am using the class constructor Fraction to create fractions. What is the difference between using the Fraction class constructor and from_float method when creating fractions from floating-point numbers?
I tested it with different numbers and I got same answers. for instance:
from fractions import Fraction
f1 = Fraction(0.3)
f2 = Fraction.from_float(0.3)
print(f1) # Output: 5404319552844595/18014398509481984
print(f2) # Output: 5404319552844595/18014398509481984
From the documentation:
In other words, in Python < 3.2, you had to use
Fraction.from_floatto construct aFractionfrom afloat. Since 3.2 this has become unnecessary, but still exists probably so as not to break backwards compatibility. You may also want to use it for more explicit type checking, asFraction.from_floatwould raise an error if you passed some other type, which theFractionconstructor might silently accept and potentially lead to subtle bugs.