This is original code to convert float to fraction:
Code:
function float2rat(x) {
var tolerance = 1.0E-6;
var h1=1; var h2=0;
var k1=0; var k2=1;
var b = x;
do {
var a = Math.floor(b);
var 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;
}
Delphi-Quellcode:
uses Math;
function float2rat(x: Extended): string;
const
tolerance = 1.0E-6;
var
h1, h2, k1, k2, a, aux: Int64;
b: Extended;
begin
h1:=1;
h2:=0;
k1:=0;
k2:=1;
b := x;
repeat
begin
a := Floor(b);
aux := h1;
h1 := a*h1+h2;
h2 := aux;
aux := k1;
k1 := a*k1+k2;
k2 := aux;
b := 1/(b-a);
end until (Abs(x - h1 / k1) > x * tolerance);
Result := Format('%d / %d', [h1, k1]) ;
end;
Code:
// JS: float2rat(9.5) = 19/2
// D: float2rat(9.5) = 9/1
What's wrong with my port?