Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConvertDoubleToString slightly wrong and does not roundtrip #1

Open
benibela opened this issue Jun 1, 2021 · 2 comments
Open

ConvertDoubleToString slightly wrong and does not roundtrip #1

benibela opened this issue Jun 1, 2021 · 2 comments

Comments

@benibela
Copy link

benibela commented Jun 1, 2021

The last digits of ConvertDoubleToString do not fit. Perhaps something is rounded wrong?

This round trip test

uses PasDblStrUtils;

var j: integer;
    s: single absolute j;
    d, d1: double;
    i: integer;
    stri: string;

begin
  for i := low(i) to high(i) do begin
    j := i;
    d := s;
    stri := ConvertDoubleToString(d);
    d1 := ConvertStringToDouble(stri);
    if d <> d1 then writeln('a: ', d, ' ', stri, ' ', d1);
end;

finds many broken numbers:

a: -1.3872854796815689E-043 -1.387285479681264e-43 -1.3872854796812640E-043
a: -2.7745709593631378E-043 -2.774570959362528e-43 -2.7745709593625280E-043
a: -4.9746095483531006E-043 -4.974609548352007e-43 -4.9746095483520069E-043
a: -5.5491419187262756E-043 -5.549141918725056e-43 -5.5491419187250561E-043
a: -6.5300508437536476E-043 -6.5300508437522124e-43 -6.5300508437522122E-043
a: -7.1045832141268225E-043 -7.104583214125261e-43 -7.1045832141252613E-043
a: -8.0854921391541945E-043 -8.085492139152418e-43 -8.0854921391524182E-043
a: -2.1958346935969884E-042 -2.1958346935946508e-42 -2.1958346935946506E-042
a: -2.3107411676716233E-042 -2.3107411676691635e-42 -2.3107411676691636E-042
a: -2.4256476417462583E-042 -2.4256476417436762e-42 -2.4256476417436763E-042
a: -4.9129524159228087E-042 -4.912952415917579e-42 -4.9129524159175788E-042
a: -5.0278588899974437E-042 -5.0278588899920914e-42 -5.0278588899920915E-042
a: -5.1427653640720787E-042 -5.142765364066604e-42 -5.1427653640666041E-042
a: -5.2576718381467137E-042 -5.257671838141117e-42 -5.2576718381411168E-042
a: -5.3725783122213486E-042 -5.3725783122156294e-42 -5.3725783122156294E-042
a: -5.4874847862959836E-042 -5.487484786290142e-42 -5.4874847862901421E-042
a: -5.6023912603706186E-042 -5.602391260364655e-42 -5.6023912603646548E-042
a: -5.7172977344452536E-042 -5.7172977344391674e-42 -5.7172977344391674E-042
a: -7.3960532947063845E-042 -7.396053294698511e-42 -7.3960532946985108E-042
a: -7.6258662428556545E-042 -7.625866242847537e-42 -7.6258662428475374E-042
a: -7.8556791910049245E-042 -7.855679190996562e-42 -7.8556791909965614E-042
a: -8.0854921391541945E-042 -8.085492139145587e-42 -8.0854921391455867E-042
a: -1.3470682137554467E-041 -1.3470682137540127e-41 -1.3470682137540126E-041
a: -1.3700495085703737E-041 -1.3700495085689152e-41 -1.3700495085689151E-041
a: -1.3930308033853007E-041 -1.3930308033838177e-41 -1.3930308033838177E-041
a: -1.4160120982002277E-041 -1.4160120981987203e-41 -1.4160120981987202E-041
a: -1.4275027456076912E-041 -1.4275027456061715e-41 -1.4275027456061715E-041
a: -1.4389933930151547E-041 -1.4389933930136228e-41 -1.4389933930136227E-041
a: -1.4504840404226182E-041 -1.450484040421074e-41 -1.4504840404210740E-041
a: -1.4619746878300817E-041 -1.4619746878285253e-41 -1.4619746878285253E-041
a: -1.4734653352375452E-041 -1.4734653352359766e-41 -1.4734653352359765E-041
a: -1.4849559826450087E-041 -1.484955982643428e-41 -1.4849559826434280E-041
@BeRo1985
Copy link
Owner

BeRo1985 commented Jun 2, 2021

Please recheck with the lastest version, where I've added Ryu-style algorithm variants.

@benibela
Copy link
Author

benibela commented Jun 4, 2021

Now it is losing zeros:

    WriteLn(ConvertDoubleToString(10));

=> 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants