xxxxxxxxxx
23
function rat_approx(x, e=0.001) {
let [a, b, c, d] = [0, 1, 1, 0];
while(true) {
let [g, h] = [a+c, b+d];
let k = g/h;
let f = abs(k-x);
if(f < e) return [g+'/'+h, k];
if(x > k) [a, b] = [g, h];
else [c, d] = [g, h];
}
}
function setup() {
noCanvas()
noLoop()
let r = rat_approx(sqrt(3), 1e-6)
print(r)
}
function draw() {
}