https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118016

            Bug ID: 118016
           Summary: GCC adds excess precision to floating point literals,
                    and therefore rounds incorrectly (x87 FPU,
                    -fexcess-precision=standard)
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: geza.herman at gmail dot com
  Target Milestone: ---

Maybe this is not a bug, but still it is unexpected behavior.

If this program is compiled with "-m32 -fexcess-precision=standard", the
program returns 0, meaning that 4.1 is not equal to 4.1
(https://godbolt.org/z/5PGaa5sKe):

int main() {
    double v = 1.1;
    return v == 1.1;
}

The reason is that the 1.1 literal represented with excess precision. We may
say that this OK (allowed by the standard?). Now, see this example
(https://godbolt.org/z/sv3EaoEKE):

#include <stdio.h>
int main() {
    double v = 9000000000000001.499999;
    printf("%f\n", v);
}

This program prints 9000000000000002.000000, instead of the closer value of
9000000000000001.000000.  This problem happens because of double rounding (the
literal first rounded to long double, then to double).  I would not expect that
'v' won't receive the closest possible value to the literal.

There is a stackoverflow thread about this:
https://stackoverflow.com/questions/79273119/why-does-gcc-compare-seemingly-equal-floating-point-values-as-different-with-f

Reply via email to