[fpc-pascal] Formatting negative zero floating point numbers

Mark mark at zbuffer.de
Tue Sep 22 23:06:34 CEST 2026


On 9/22/26 15:08, Robert Wolfe via fpc-pascal wrote:

> Have you tried using the Pascal Str() function like so:

Yes, that works, but as mentioned, I need control over the number of digits for the exponent. That's why I'm using FloatToStrF.

For now I am working around the issue as follows:

program main;

uses
   sysutils;

function IsNegativeZero(const d: Double): Boolean;
begin
   Result := UInt64(d) = UInt64($8000000000000000);
end;

procedure WriteDoubleValue(var f: TextFile; v: Double);
var
   sign :String;
begin
   if IsNegativeZero(v) then
     sign := '-'
   else
     sign := '';

   // need to use FloatToStrF to change exponent digits from 3 to 2
   Writeln(f, format('%s%s(%16.16x)', [ sign, FloatToStrF(v, ffExponent, 17, 2), UInt64(v) ]));
end;

begin
    WriteDoubleValue(StdOut, 0.0);
    WriteDoubleValue(StdOut, -0.0);
end.

which gives:

0.0000000000000000E+00(0000000000000000)
-0.0000000000000000E+00(8000000000000000)

Any opinion from the fpc/rtl maintainers? Is the behavior of Format and FloatToStrF to  omit a minus sign for negative zero values - in contrast to what Write and Str are doing - a bug or not?

Mark


More information about the fpc-pascal mailing list