I have written this function that converts a double
to a fraction returning a string:
function getFraction(x: double):string;
var h1, h2, k1, k2, y, a, aux : double;
begin
//Setup the values
h1 := 1;
h2 := 0;
k1 := 0;
k2 := 1;
y := x;
//Generates the fraction
repeat
begin
a := floor(y);
aux := h1;
h1 := a*h1+h2;
h2 := aux;
aux := k1;
k1 := a*k1+k2;
k2 := aux;
y := 1/(y-a) ;
end;
until ( abs(x-h1/k1) > x*0.000001);
//Output
Result := FloatToStr(h1) + '/' + FloatToStr(k1);
end;
And then I call it in this way: Edit7.Text := getFraction(x);
where the x
is a double. I am using lazarus and I always have this error:
I am sure that the code above has not logic hassles because it becomes from a Java class that I have implemented last week in a project (code).
What am I missing? The full code of my Delphi project can be found here.