Skip to content

Instantly share code, notes, and snippets.

@jiucenglou
Created September 8, 2018 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiucenglou/784880960754a9055aeab7ded008904e to your computer and use it in GitHub Desktop.
Save jiucenglou/784880960754a9055aeab7ded008904e to your computer and use it in GitHub Desktop.
Inconsistent ExtendedToStr, FloatToStrF and Format.
// http://forum.lazarus.freepascal.org/index.php/topic,42487.0.html
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
{$IFOPT D+} {$DEFINE DEBUG} {$ENDIF}
{$ASSERTIONS ON}
program TruncBug;
{$IFNDEF FPC}{$APPTYPE CONSOLE}{$ENDIF}
uses
SysUtils;
var
A,B: Extended;
begin
A := 0.3050;
B := 0.6659963050;
(* FPC 3.1.1-r39514 Delphi7 Delphi25Tokyo
Win32: 0.31; 0.66599630 0.31; 0.66599631 0.31; 0.66599631
Win64: 0.31; 0.66599631 0.31; 0.66599631
Lnx64: 0.31; 0.66599630
*)
Writeln(FloatToStrF(A, ffFixed, 20, 2) + '; '+ FloatToStrF(B, ffFixed, 20, 8));
(* FPC 3.1.1-r39514 Delphi7 Delphi25Tokyo
Win32: 0.31; 0.66599630 0.31; 0.66599631 0.31; 0.66599631
Win64: 0.31; 0.66599631 0.31; 0.66599631
Lnx64: 0.31; 0.66599630
*)
Writeln(Format('%.2n; %.8n',[A,B]));
end.
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
{$IFOPT D+} {$DEFINE DEBUG} {$ENDIF}
{$ASSERTIONS ON}
program TruncBugSyn;
{$IFNDEF FPC}{$APPTYPE CONSOLE}{$ENDIF}
uses
SysUtils,
SynCommons;
var
A,B: Extended;
begin
A := 0.3050;
B := 0.6659963050;
(* FPC 3.1.1-r39514 Delphi7 Delphi25Tokyo
Win32: 0; 0.6659963 0.31; 0.66599631 0.31; 0.66599631
Win64: 0; 0.6659963 0; 0.6659963
Lnx64: 0; 0.6659963
*)
Writeln(ExtendedToStr(A, 2) + '; '+ ExtendedToStr(B, 8));
(* FPC 3.1.1-r39514 Delphi7 Delphi25Tokyo
Win32: 0.305; 0.665996305 0.305; 0.665996305 0.305; 0.665996305
Win64: 0.305; 0.665996305 0.305; 0.665996305
Lnx64: 0.305; 0.665996305
*)
Writeln(FormatUTF8('%; %', [A,B]));
(* FPC 3.1.1-r39514 Delphi7 Delphi25Tokyo
Win32: 0.31; 0.66599630 0.31; 0.66599631 0.31; 0.66599631
Win64: 0.31; 0.66599631 0.31; 0.66599631
Lnx64: 0.31; 0.66599630
*)
Writeln(FloatToStrF(A, ffFixed, 20, 2) + '; '+ FloatToStrF(B, ffFixed, 20, 8));
(* FPC 3.1.1-r39514 Delphi7 Delphi25Tokyo
Win32: 0.31; 0.66599630 0.31; 0.66599631 0.31; 0.66599631
Win64: 0.31; 0.66599631 0.31; 0.66599631
Lnx64: 0.31; 0.66599630
*)
Writeln(Format('%.2n; %.8n',[A,B]));
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment