Skip to content

Instantly share code, notes, and snippets.

Created January 18, 2013 23:15
Show Gist options
  • Save anonymous/4569504 to your computer and use it in GitHub Desktop.
Save anonymous/4569504 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Jan 18, 2013.
    14 changes: 14 additions & 0 deletions float2rat.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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;
    }