Skip to content

Instantly share code, notes, and snippets.

@FelixLuciano
Created June 15, 2020 02:11
Show Gist options
  • Save FelixLuciano/a6249f04ede1317ce48be3ab29593c17 to your computer and use it in GitHub Desktop.
Save FelixLuciano/a6249f04ede1317ce48be3ab29593c17 to your computer and use it in GitHub Desktop.
function rationalize (value) {
let numerator = 0
let denominator = 1
while (value != numerator / denominator) {
const ratio = numerator / denominator
if (ratio < value) numerator++
else if (ratio > value) denominator++
else break
}
return [ numerator, denominator ]
}
// rationalize(3.14)
// -> [ 157, 50 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment