Skip to content

Instantly share code, notes, and snippets.

@devsnek
Created March 29, 2017 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devsnek/28864a9228539e130efaf99e2e5c1c81 to your computer and use it in GitHub Desktop.
Save devsnek/28864a9228539e130efaf99e2e5c1c81 to your computer and use it in GitHub Desktop.
function tofrac(x0) {
let x = x0;
let a = Math.floor(x);
let [h, k, h1, k1] = [a, 1, 1, 0];
while (x - a > Number.EPSILON * k * k) {
x = 1 / (x - a);
a = Math.floor(x);
let [h2, k2] = [h1, k1];
[h1, k1] = [h, k];
h = h2 + a * h1;
k = k2 + a * k1;
}
return `${h}/${k}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment