Efficiently finding approximate fraction, with tolerance for fp rounding
Convert the float to a Fraction:

&gt;&gt;&gt; from fractions import Fraction
&gt;&gt;&gt; Fraction(0.1 + 0.2)
Fraction(1351079888211149, 4503599627370496)


(this gives you the same ratio as float.as_integer_ratio), and then use the limit_denominator method to return the closest fraction with a denominator of limited size:

&gt;&gt;&gt; Fraction(0.1 + 0.2).limit_denominator(1000000)
Fraction(3, 10)


If you're interested in the maths behind the limit_denominator method, see this answer on Stack Overflow.