This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
[PATCH] libgfortran/28354 - 0.99999 printed as 0. instead of 1. by format(f3.0)
- From: Jerry DeLisle <jvdelisle at verizon dot net>
- To: Fortran List <fortran at gcc dot gnu dot org>
- Cc: gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Sun, 27 Aug 2006 20:47:19 -0700
- Subject: [PATCH] libgfortran/28354 - 0.99999 printed as 0. instead of 1. by format(f3.0)
:ADDPATCH fortran:
The attached patch fixes this PR by adjusting the real value prior to processing
to get the correct output when processed through the formatting. I was
pursuing catching this special case further down in the code and modifying the
handling of the nbefore, nafter, nzero, etc variables that define the output.
This was working mostly when I discovered I would have to special case the
rounding as well. Then it occurred to me to handle that before all the
processing and this gave a much cleaner fix.
Regression tested and NIST tested. New test case included.
OK for trunk 4.2? and 4.1 later?
Regards,
Jerry
2006-08-27 Jerry DeLisle <jvdelisle@gcc.gnu.org>
PR libgfortran/28354
* io/write.c: Check for special case of zero precision in format
and pre-round the real value.
Index: io/write.c
===================================================================
*** io/write.c (revision 116492)
--- io/write.c (working copy)
*************** output_float (st_parameter_dt *dtp, cons
*** 426,431 ****
--- 426,440 ----
if (value < 0)
value = -value;
+ /* Special case when format specifies no digits after the decimal point. */
+ if (d == 0)
+ {
+ if (value < 0.5)
+ value = 0.0;
+ else if (value < 1.0)
+ value = value + 0.5;
+ }
+
/* Printf always prints at least two exponent digits. */
if (value == 0)
edigits = 2;
! { dg-do run }
! PR28354 Incorrect rounding of .99999 with f3.0 format specifier
! Test case derived from PR. Submitted by Jerry DeLisle <jvdelisle@gcc.gnu.org>
write(*,50) -0.99999
write(*,50) 0.99999
write(*,50) -9.0
write(*,50) -0.99
write(*,50) -0.999
write(*,50) -0.999
write(*,50) -0.59
write(*,50) -0.49
write(*,100) 37.99999
write(*,100) 10345.0
write(*,100) 333.678
write(*,100) 333.499
50 format(f3.0,"<")
100 format(f8.0,"<")
end
! {dg-output "-1.<"
! {dg-output " 1.<"
! {dg-output "-9.<"
! {dg-output "-1.<"
! {dg-output "-1.<"
! {dg-output "-1.<"
! {dg-output "-1.<"
! {dg-output " 0.<"
! {dg-output " 38.<"
! {dg-output " 10345.<"
! {dg-output " 334.<"
! {dg-output " 333.<"