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
 
WojTec

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

Small piece from JavaScript to Delphi won't work

  Alt 16. Jul 2018, 20: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
 


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 07:34 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz