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

            Bug ID: 122643
           Summary: Code generated with -O1 -fexpensive-optimizations
                    produces NaN on aarch64
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rnhmjoj at eurofusion dot eu
  Target Milestone: ---

The following program compiled with -O1 -fexpensive-optimizations on aarch64
outputs: "nan, 0.000000". Instead, when compiled without optimizations or on
other architectures (with or without optimizations) the output is the expected
one: "0.000000 0.000000".

   #include <stdio.h>
   #include <math.h>

   void main() {
     int n = 41;
     double x;

     for (int i = 1; i <= n; i++) {
       x = sqrt(1 - (i-1) * (1.0/(n-1)));
     }
     printf("%f %f\n", x, sqrt(1 - (n-1) * (1.0/(n-1))));
   }

Note: the problem was actually discovered in a Fortran program compiled with
gfortran, so it's not specific to the C front end.

Reply via email to