How to extract fraction from a floating point number I'm making some tests with float type (floating point number) with programming and in some of my tests I need to extract the fraction that originates the float value.
Let $ x $ be a floating point number, and $ a, b \in \mathbb{Z} $, where $ x \approxeq \frac{a}{b} $, I want to discover $ a $ and $ b $.
e.g., if I have $ 0.5 $, I need a method (maybe numerical?) that gives me $ \frac{1}{2} $ or some of its multiples.
If there's more than one method, I would like the fastest or simples to implement with a programming languagg.

My favorite method would be continued fractions.
If the fraction is exact the process will (should! ;-)) converge :
(illustrations using the free pari/gp)

contfrac(0.14285)
  = [0, 7, 2857]

with the exact answer : $0+\dfrac 1{7+\dfrac 1{2857}}=\dfrac{2857}{20000}$
else you'll get a large integer after some terms (the difficult part is to decide when the number becomes large!) and should just ignore it (and the next terms) :

contfrac(0.142857142)
  = [0, 7, 23809523, 1, 2]

so that the answer will be nearly $\;0+\dfrac 17$
