Git Product home page Git Product logo

pasdblstrutils's Issues

pchar interface

You could make a function ConvertStringToDouble(StringValue: pchar; len: SizeUInt; ... function, so it can be used with substrings/shortstrings without copying

ConvertStringToDouble slightly wrong

I have run it with the test cases of https://github.com/nigeltao/parse-number-fxx-test-data and it finds many slightly broken numbers

For example:

parsing failed: 8.589934335999999523162841796875e+09 ( 8.5899343360000000E+009) got  8.5899343359999990E+009
parsing failed: 9008309105029287 ( 9.0083091050292880E+015) got  9.0083091050292860E+015
parsing failed: 9009640786128499 ( 9.0096407861285000E+015) got  9.0096407861284980E+015
parsing failed: 9009650242502435 ( 9.0096502425024360E+015) got  9.0096502425024340E+015
parsing failed: 9009859112919659 ( 9.0098591129196600E+015) got  9.0098591129196580E+015
parsing failed: 9010553528704491 ( 9.0105535287044920E+015) got  9.0105535287044900E+015
parsing failed: 9010674631189967 ( 9.0106746311899680E+015) got  9.0106746311899660E+015
parsing failed: 9011342073688603 ( 9.0113420736886040E+015) got  9.0113420736886020E+015
parsing failed: 9011683300034947 ( 9.0116833000349480E+015) got  9.0116833000349460E+015
parsing failed: 9013156506727407 ( 9.0131565067274080E+015) got  9.0131565067274060E+015

... parsingfail.txt

program Project1;

{$mode objfpc}{$H+}

uses sysutils, PasDblStrUtils;

var s, expected: string;
  t: TStringArray;
  d: double;
  code: integer;
  expectedU64: QWord;
  expectedD: double absolute expectedU64;
begin
  while not eof do begin
    try
      readln(s);
      t := s.split(' ');
      //val(t[3], d, code);
      //if code <> 0 then writeln(s);
      d := ConvertStringToDouble(t[3]);
      expected := '$'+t[2];
      val(expected, expectedU64, code);
      if code <> 0 then writeln('true failed: ',s);
      if expectedD <> d then
        writeln('parsing failed: ', t[3], ' (', expectedD, ') got ',d);
    except
      writeln('exception: ', s);
    end;
  end;
end.

performance

So it is usually faster than StrToFloat, but often slower than the traditional Val:



input: 123
val: 56 ns
strtofloat: 157 ns
bero: 76 ns


input: 12345678
val: 74 ns
strtofloat: 176 ns
bero: 86 ns


input: 123456789012345
val: 95 ns
strtofloat: 227 ns
bero: 97 ns


input: 1234567890123456789
val: 113 ns
strtofloat: 253 ns
bero: 104 ns


input: 123456781234567812345678
val: 153 ns
strtofloat: 285 ns
bero: 30997 ns


input: 1.2345e100
val: 105 ns
strtofloat: 241 ns
bero: 93 ns


input: 0.0000000012345678
val: 103 ns
strtofloat: 250 ns
bero: 117 ns


input: -1.234e-100
val: 106 ns
strtofloat: 241 ns
bero: 102 ns

{$mode objfpc}{$H+}

uses  Classes, PasDblStrUtils,math,bbdebugtools,sysutils
  { you can add units after this };

const FLT: array of string = ('123', '12345678', '123456789012345', '1234567890123456789',  '123456781234567812345678','1.2345e100', '0.0000000012345678', '-1.234e-100');
var s: string;
    d: double;
    i, ca: Integer;
    time: Cardinal;
begin
  for ca := low(FLT) to high(FLT) do begin
    s := FLT[ca];
    writeln;
    writeln;
    writeln('input: ',s);
    logging := true;
    time := GetTickCount;
    for i := 1 to 10000000 do
      val(s, d);
    time := GetTickCount - time;
    writeln('val: ', time div 10, ' ns');

    time := GetTickCount;
    for i := 1 to 10000000 do
      d := strtofloat(s);
    time := GetTickCount - time;
    writeln('strtofloat: ', time div 10, ' ns');

    time := GetTickCount;
    for i := 1 to 10000000 do
      d := ConvertStringToDouble(s);
    time := GetTickCount - time;
    writeln('bero: ', time div 10, ' ns');
  end;

end.

You could implement Eisel-Lemire to make it faster on the short numbers.

Although the longer numbers are actually probematic with 123456781234567812345678 where it is 200-times slower than Val

ConvertDoubleToString slightly wrong and does not roundtrip

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.