[fpc-pascal] Formatting negative zero floating point numbers

Mark mark at zbuffer.de
Mon Sep 21 17:31:20 CEST 2026


I have a free pascal and a C program that both generate numeric output in text format. I expect both programs to produce the same output and I automatically verify that with a simple textual diff in my build process. That works fine in general except for negative zero floating point values (https://en.wikipedia.org/wiki/Signed_zero). The issue is that FloatToStrF does not output a minus sign for those values. Here are short examples to reproduce the behavior. The C output is what I would expect.

C:

#include <stdio.h>

int main() {
     double d = -0.0;
     printf("%e\n", d);
     return 0;
}

$ ./main
-0.000000e+00

Pascal (fpc-3.2.2):

program main;

uses
   sysutils;

var
   d :Double;
   s :String;
begin
    d := -0.0;

    Writeln(d);

    WriteStr(s, d);
    Writeln(s);

    Str(d:0:17, s);
    Writeln(s);

    Writeln(FloatToStrF(d, ffExponent, 17, 3));
    Writeln(Format('%e', [d]));
end.

$ ./main
-0.0000000000000000E+000
-0.0000000000000000E+000
-0.00000000000000000
0.0000000000000000E+000
0.0000000000000000E+000


The output of FloatToStrF and Format is different to what I expect and get with C. However, it is also different to what Write/WriteStr/Str produces which seems inconsistent to me. Is that actually the expected behavior? If yes, is there another way to get the desired output? Using WriteStr is no alternative for my use case, since I need control over the number of digits for the exponent.

Thanks,
Mark


More information about the fpc-pascal mailing list