http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59836

            Bug ID: 59836
           Summary: Wrong outputs with rounding formats for some values.
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libfortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dominiq at lps dot ens.fr
                CC: jvdelisle at gcc dot gnu.org

This is a split off pr59774 comment 9. The code

 print "(ru,g45.3)", 891.1
 print "(rd,g45.3)", -891.1
end

compiled with trunk (r206559) gives the output

                                       9.    
                                      -9.    

I have also found that the code

print '(RU,F2.0)', 0.6
print '(RD,F3.0)', -0.6
end

gives

7.
-7.

Both problems occur because the Fw.d format is not properly handled when d==0.
The following patch fixes both problems

--- ../_clean/libgfortran/io/write_float.def    2014-01-04 15:51:53.000000000
+0100
+++ libgfortran/io/write_float.def    2014-01-15 22:22:17.000000000 +0100
@@ -373,7 +373,7 @@ output_float (st_parameter_dt *dtp, cons
   updown:

   rchar = '0';
-  if (w > 0 && d == 0 && p == 0)
+  if (ft != FMT_F && nbefore == 0 && w > 0 && d == 0 && p == 0)
     nbefore = 1;
   /* Scan for trailing zeros to see if we really need to round it.  */
   for(i = nbefore + nafter; i < ndigits; i++)
@@ -386,13 +386,14 @@ output_float (st_parameter_dt *dtp, cons
   do_rnd:

   if (nbefore + nafter == 0)
+    /* Handle the case Fw.0 and value < 1.0 */
     {
       ndigits = 0;
       if (nzero_real == d && digits[0] >= rchar)
     {
       /* We rounded to zero but shouldn't have */
-      nzero--;
-      nafter = 1;
+      nbefore = 1;
+          digits--;
       digits[0] = '1';
       ndigits = 1;
     }

The first patch handles the first problem by restricting the test to the E*
formats and the second one fixes the way Fw.0 handles values < 1.0 (yes, I have
seen the formatting issue for the line digits--. I'll fix it when I'll submit
the patch).
AFAICT the line nzero--; does not seem necessary, but I don't know why.

With the patch the first test gives

                                     892.    
                                    -892.    

and the second

1.
-1.

and I did not see any regression when retesting with it.

Reply via email to