function float2rat(x) { tolerance = 1.e-4; h1=1; h2=0; k1=0; k2=1; b = x; do { a = Math.floor(b); aux = h1; h1 = a*h1+h2; h2 = aux; aux = k1; k1 = a*k1+k2; k2 = aux; b = 1/(b-a); } while (Math.abs(x-h1/k1) > x*tolerance); return h1+"/"+k1; }