Skip to content

FloatToStrF and Format give bad result for some input values

Original Reporter info from Mantis: abusigin
  • Reporter name: Anthony Busigin

Description:

Very simple to reproduce problem. See example below.

Steps to reproduce:

program Test;

uses
  SysUtils;

var
  s1,s2 : String;
  x : double;

begin
  x := 100000;
  repeat
    s1 := FloatToStrF(x,ffGeneral,7{precision},1{digits});
    s2 := Format('%.7g',[x]);
    writeln('x = ',x);
    writeln('s1 = ',s1);
    writeln('s2 = ',s2);
    x := 10*x;
  until x > 1e8;
end.    

Additional information:

Output results are:

x = 1.00000000000000E+005
s1 = 100000
s2 = 100000
x = 1.00000000000000E+006
s1 = 1
s2 = 1
x = 1.00000000000000E+007
s1 = 1E7
s2 = 1E007
x = 1.00000000000000E+008
s1 = 1E8
s2 = 1E008

Results for x = 1E6 are incorrect. This is a silent bug, which is serious because the user may be unaware that results are incorrect.

Mantis conversion info:

  • Mantis ID: 23904
  • OS: Windows
  • OS Build: 7
  • Build: [2013/02/02]
  • Platform: i386
  • Version: 2.6.0
  • Fixed in version: 2.6.1
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information