AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi Small piece from JavaScript to Delphi won't work
Thema durchsuchen
Ansicht
Themen-Optionen

Small piece from JavaScript to Delphi won't work

Ein Thema von WojTec · begonnen am 16. Jul 2018 · letzter Beitrag vom 16. Jul 2018
Antwort Antwort
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

Small piece from JavaScript to Delphi won't work

  Alt 16. Jul 2018, 19:31
Delphi-Version: 10.2 Tokyo
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?
  Mit Zitat antworten Zitat
gammatester

Registriert seit: 6. Dez 2005
999 Beiträge
 
#2

AW: Small piece from JavaScript to Delphi won't work

  Alt 16. Jul 2018, 20:18
Your translation of do {..} while is wrong. Your stopping criterion should be as given below
Delphi-Quellcode:
 repeat
    a := Floor(b);
    aux := h1;
    h1 := a*h1+h2;
    h2 := aux;
    aux := k1;
    k1 := a*k1+k2;
    k2 := aux;
    if a=b then break;
    b := 1/(b-a);
  until (Abs(x - h1 / k1) <= x * tolerance);
Note the safe-guard against division by zero. Don't know why JS can divide by zero - or is it quietly ignored?

With these changes you get 9.5 = 19/2.
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#3

Re: Small piece from JavaScript to Delphi won't work

  Alt 16. Jul 2018, 20:49
Nice, is working, thank you!

And how about types, are they correct?
  Mit Zitat antworten Zitat
gammatester

Registriert seit: 6. Dez 2005
999 Beiträge
 
#4

AW: Re: Small piece from JavaScript to Delphi won't work

  Alt 16. Jul 2018, 21:31
Nice, is working, thank you!
And how about types, are they correct?
They should work for x in the Longint range. You should check also if frac(x) is zero and output int(x) and 1. The code crashes e.g. for x=1.0+MaxLongint because k1 will become zero and h1/k1 throws a divide-by-zero exception.

Geändert von gammatester (16. Jul 2018 um 21:46 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:45 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz