Bug 93592 - Invalid UP/DOWN rounding with EN descriptor
Summary: Invalid UP/DOWN rounding with EN descriptor
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 9.2.0
: P4 normal
Target Milestone: 8.5
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2020-02-05 10:18 UTC by Thomas Henlich
Modified: 2020-07-25 15:26 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-07-11 00:00:00


Attachments
Extensive test with RU and EN (1.77 KB, text/plain)
2020-07-19 21:59 UTC, Dominique d'Humieres
Details
Extensive test with RD and EN (1.77 KB, text/plain)
2020-07-19 22:01 UTC, Dominique d'Humieres
Details
Extensive test with RZ and EN (1.73 KB, text/plain)
2020-07-21 15:23 UTC, Dominique d'Humieres
Details
Extensive test with RN and EN (1.72 KB, text/plain)
2020-07-21 15:24 UTC, Dominique d'Humieres
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Henlich 2020-02-05 10:18:02 UTC
When using the EN descriptor with d=0 (ENw.0) and rounding mode UP or DOWN the result is totally wrong:

program test_en
  print "(RU,EN9.0)",  800.0_8 !   8.E+00 expected 800.E+00
  print "(RU,EN9.0)",  901.0_8 !  10.E+00 expected 901.E+00
  print "(RD,EN9.0)", -999.6_8 ! -10.E+00 expected  -1.E+03
end
Comment 1 Dominique d'Humieres 2020-07-11 20:46:11 UTC
Confirmed since at least GCC7.
Comment 2 Dominique d'Humieres 2020-07-19 20:50:12 UTC
--- ../_clean/libgfortran/io/write_float.def	2020-06-13 03:11:55.000000000 +0200
+++ libgfortran/io/write_float.def	2020-07-19 22:46:57.000000000 +0200
@@ -399,7 +399,7 @@ build_float_string (st_parameter_dt *dtp
   updown:
 
   rchar = '0';
-  if (ft != FMT_F && w > 0 && d == 0 && p == 0)
+  if (ft != FMT_F && ft !=FMT_EN && 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++)

should do the trick. I am currently try to reuse gfortran.dg/fmt_en.f90 for a more general testing.
Comment 3 Dominique d'Humieres 2020-07-19 21:59:34 UTC
Created attachment 48894 [details]
Extensive test with RU and EN
Comment 4 Dominique d'Humieres 2020-07-19 22:01:08 UTC
Created attachment 48895 [details]
Extensive test with RD and EN

It would be nice to check that I did not make any mistake.
Comment 5 Dominique d'Humieres 2020-07-21 15:23:44 UTC
Created attachment 48909 [details]
Extensive test with RZ and EN
Comment 6 Dominique d'Humieres 2020-07-21 15:24:24 UTC
Created attachment 48910 [details]
Extensive test with RN and EN
Comment 7 Dominique d'Humieres 2020-07-21 15:28:00 UTC
Could someone used to git commit the fix and the tests and back port them to all the active branches?
Comment 8 anlauf 2020-07-21 19:25:40 UTC
(In reply to Dominique d'Humieres from comment #7)
> Could someone used to git commit the fix and the tests and back port them to
> all the active branches?

I think the normal procedure would be to submit this for review first.
Comment 9 Dominique d'Humieres 2020-07-21 22:15:16 UTC
Patch submitted at https://gcc.gnu.org/pipermail/fortran/2020-July/054769.html.
Comment 10 Dominique d'Humieres 2020-07-23 21:24:04 UTC
The patch applies and regtest cleanly down to GCC8.
IMO this is nasty wrong code and it should be fixed on all the active branches.
Comment 11 GCC Commits 2020-07-24 18:48:15 UTC
The master branch has been updated by Thomas Kथà¤nig <tkoenig@gcc.gnu.org>:

https://gcc.gnu.org/g:05e0971bcf94a481cbfa2731484f024a67dbd4a5

commit r11-2309-g05e0971bcf94a481cbfa2731484f024a67dbd4a5
Author: Dominique d'Humieres <dhumieres.dominique@free.fr>
Date:   Fri Jul 24 20:07:12 2020 +0200

    PR 93592 - Invalid UP/DOWN rounding with EN descriptor.
    
    The fix is obvious (I have added a comment). The tests are probably
    an overkill, but it does not hurt.
    
    libgfortran/ChangeLog:
    
            PR fortran/93592
            * io/write_float.def (build_float_string): Do not reset
              nbefore for FMT_F and FMT_EN.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/93592
            * gfortran.dg/fmt_en.f90: Adjust test.
            * gfortran.dg/fmt_en_rd.f90: New test.
            * gfortran.dg/fmt_en_rn.f90: New test.
            * gfortran.dg/fmt_en_ru.f90: New test.
            * gfortran.dg/fmt_en_rz.f90: New test.
Comment 12 GCC Commits 2020-07-25 10:55:29 UTC
The releases/gcc-10 branch has been updated by Thomas Kथà¤nig <tkoenig@gcc.gnu.org>:

https://gcc.gnu.org/g:b45f51d467c6c192c70f57e517bec3a3c0c9d1f4

commit r10-8531-gb45f51d467c6c192c70f57e517bec3a3c0c9d1f4
Author: Dominique d'Humieres <dhumieres.dominique@free.fr>
Date:   Fri Jul 24 20:07:12 2020 +0200

    PR 93592 - Invalid UP/DOWN rounding with EN descriptor.
    
    The fix is obvious (I have added a comment). The tests are probably
    an overkill, but it does not hurt.
    
    libgfortran/ChangeLog:
    
            PR fortran/93592
            * io/write_float.def (build_float_string): Do not reset
              nbefore for FMT_F and FMT_EN.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/93592
            * gfortran.dg/fmt_en.f90: Adjust test.
            * gfortran.dg/fmt_en_rd.f90: New test.
            * gfortran.dg/fmt_en_rn.f90: New test.
            * gfortran.dg/fmt_en_ru.f90: New test.
            * gfortran.dg/fmt_en_rz.f90: New test.
    
    (cherry picked from commit 05e0971bcf94a481cbfa2731484f024a67dbd4a5)
Comment 13 GCC Commits 2020-07-25 10:58:49 UTC
The releases/gcc-9 branch has been updated by Thomas Kथà¤nig <tkoenig@gcc.gnu.org>:

https://gcc.gnu.org/g:eca316afa46907e6b0e1513ecbc3010aaf2590f3

commit r9-8765-geca316afa46907e6b0e1513ecbc3010aaf2590f3
Author: Dominique d'Humieres <dhumieres.dominique@free.fr>
Date:   Fri Jul 24 20:07:12 2020 +0200

    PR 93592 - Invalid UP/DOWN rounding with EN descriptor.
    
    The fix is obvious (I have added a comment). The tests are probably
    an overkill, but it does not hurt.
    
    libgfortran/ChangeLog:
    
            PR fortran/93592
            * io/write_float.def (build_float_string): Do not reset
              nbefore for FMT_F and FMT_EN.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/93592
            * gfortran.dg/fmt_en.f90: Adjust test.
            * gfortran.dg/fmt_en_rd.f90: New test.
            * gfortran.dg/fmt_en_rn.f90: New test.
            * gfortran.dg/fmt_en_ru.f90: New test.
            * gfortran.dg/fmt_en_rz.f90: New test.
    
    (cherry picked from commit 05e0971bcf94a481cbfa2731484f024a67dbd4a5)
Comment 14 GCC Commits 2020-07-25 11:45:04 UTC
The releases/gcc-8 branch has been updated by Thomas Kथà¤nig <tkoenig@gcc.gnu.org>:

https://gcc.gnu.org/g:1757a63809d6c6073c87978c22dcfffe85753c0f

commit r8-10375-g1757a63809d6c6073c87978c22dcfffe85753c0f
Author: Dominique d'Humieres <dhumieres.dominique@free.fr>
Date:   Fri Jul 24 20:07:12 2020 +0200

    PR 93592 - Invalid UP/DOWN rounding with EN descriptor.
    
    The fix is obvious (I have added a comment). The tests are probably
    an overkill, but it does not hurt.
    
    libgfortran/ChangeLog:
    
            PR fortran/93592
            * io/write_float.def (build_float_string): Do not reset
              nbefore for FMT_F and FMT_EN.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/93592
            * gfortran.dg/fmt_en.f90: Adjust test.
            * gfortran.dg/fmt_en_rd.f90: New test.
            * gfortran.dg/fmt_en_rn.f90: New test.
            * gfortran.dg/fmt_en_ru.f90: New test.
            * gfortran.dg/fmt_en_rz.f90: New test.
    
    (cherry picked from commit 05e0971bcf94a481cbfa2731484f024a67dbd4a5)
Comment 15 Dominique d'Humieres 2020-07-25 15:26:28 UTC
Patch committed to all the active branches. Closing.

Tanks for the report.