public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libfortran/48651] New: DTOA float conversion issue
@ 2011-04-17 14:00 jvdelisle at gcc dot gnu.org
  2011-04-17 18:07 ` [Bug libfortran/48651] " jvdelisle at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: jvdelisle at gcc dot gnu.org @ 2011-04-17 14:00 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: DTOA float conversion issue
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libfortran
        AssignedTo: jvdelisle@gcc.gnu.org
        ReportedBy: jvdelisle@gcc.gnu.org


See pr48602 comment #17 and #18

Confirmed the DTOA issue. Placed the print of buffer immediately after the DTOA
call

 -------------------------------
buffer=8.999999999999999999999999999999999711111e-02  
  8.99999999999999999999999999999999971E-0002  RD:
buffer=8.999999999999999999999999999999999711111e-02  
       0.89E-01
buffer=9.0e-02                                        
       0.90E-01
buffer=9.0e-02                                        
       0.90D-01
buffer=8.999999999999999999999999999999999711111e-02  
           0.08

The problem only occurs with real(8)


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libfortran/48651] DTOA float conversion issue
  2011-04-17 14:00 [Bug libfortran/48651] New: DTOA float conversion issue jvdelisle at gcc dot gnu.org
@ 2011-04-17 18:07 ` jvdelisle at gcc dot gnu.org
  2011-04-17 18:29 ` jvdelisle at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jvdelisle at gcc dot gnu.org @ 2011-04-17 18:07 UTC (permalink / raw)
  To: gcc-bugs

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

Jerry DeLisle <jvdelisle at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thenlich at users dot
                   |                            |sourceforge.net

--- Comment #1 from Jerry DeLisle <jvdelisle at gcc dot gnu.org> 2011-04-17 18:07:27 UTC ---
Well, this is not a libgfortran issue. I am not convinced it is actually a
problem either.  The resulting value from printf swings to the other side for
double or long double.

#include <stdio.h>

int
main ()
{
  float num = 0.9;
  double dnum = 0.9;
  long double ldnum = 0.0;
  printf("%30.28f\n", num);
  printf("%30.28lf\n", num);
  printf("%30.28f\n", dnum);
  printf("%30.28lf\n", dnum);
  printf("%30.28f\n", ldnum);
  printf("%30.28lf\n", ldnum);
}

Gives:

$ ./a.out 
0.8999999761581420898437500000
0.8999999761581420898437500000
0.9000000000000000222044604925
0.9000000000000000222044604925
0.9000000000000000222044604925
0.9000000000000000222044604925

The result flips to the other side of .9 which is not necessarily wrong.

0.9 - 0.8999999761581420898437500000 = 2.384185791×10⁻⁸

0.9 - 0.9000000000000000222044604925 = −2.220446049×10⁻¹⁷


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libfortran/48651] DTOA float conversion issue
  2011-04-17 14:00 [Bug libfortran/48651] New: DTOA float conversion issue jvdelisle at gcc dot gnu.org
  2011-04-17 18:07 ` [Bug libfortran/48651] " jvdelisle at gcc dot gnu.org
@ 2011-04-17 18:29 ` jvdelisle at gcc dot gnu.org
  2011-04-17 18:34 ` thenlich at users dot sourceforge.net
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jvdelisle at gcc dot gnu.org @ 2011-04-17 18:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jerry DeLisle <jvdelisle at gcc dot gnu.org> 2011-04-17 18:28:55 UTC ---
hmm, notice the typo! i have set ldnum to 0.0 but it still is printing the
value of dnum. What am I doing wrong.

$ cat sprint.c 
#include <stdio.h>

int
main ()
{
  float num = 0.9;
  double dnum = 0.9;
  long double ldnum = 25.6;
  printf("%50.48f\n", num);
  printf("%50.48lf, %d\n", num, sizeof(num));
  printf("%80.78f\n", dnum);
  printf("%80.78lf, %d\n", dnum, sizeof(dnum));
  printf("%80.78f\n", ldnum);
  printf("%80.78lf, %d\n", ldnum, sizeof(ldnum));
}
[jerry@quattro pr48602]$ cat sprint.c 
#include <stdio.h>

int
main ()
{
  float num = 0.9;
  double dnum = 0.9;
  long double ldnum = 25.6;
  printf("%50.48f\n", num);
  printf("%50.48lf, %d\n", num, sizeof(num));
  printf("%80.78f\n", dnum);
  printf("%80.78lf, %d\n", dnum, sizeof(dnum));
  printf("%80.78f\n", ldnum);
  printf("%80.78lf, %d\n", ldnum, sizeof(ldnum));
}
[jerry@quattro pr48602]$ ./a.out 
0.899999976158142089843750000000000000000000000000
0.899999976158142089843750000000000000000000000000, 4
0.900000000000000022204460492503130808472633361816406250000000000000000000000000
0.900000000000000022204460492503130808472633361816406250000000000000000000000000,
8
0.900000000000000022204460492503130808472633361816406250000000000000000000000000
0.900000000000000022204460492503130808472633361816406250000000000000000000000000,
16

The value of ldnum is not surviving.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libfortran/48651] DTOA float conversion issue
  2011-04-17 14:00 [Bug libfortran/48651] New: DTOA float conversion issue jvdelisle at gcc dot gnu.org
  2011-04-17 18:07 ` [Bug libfortran/48651] " jvdelisle at gcc dot gnu.org
  2011-04-17 18:29 ` jvdelisle at gcc dot gnu.org
@ 2011-04-17 18:34 ` thenlich at users dot sourceforge.net
  2011-04-17 19:03 ` jvdelisle at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: thenlich at users dot sourceforge.net @ 2011-04-17 18:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Thomas Henlich <thenlich at users dot sourceforge.net> 2011-04-17 18:34:15 UTC ---
(In reply to comment #2)
> hmm, notice the typo! i have set ldnum to 0.0 but it still is printing the
> value of dnum. What am I doing wrong.
> 
> $ cat sprint.c 
> #include <stdio.h>
> 
> int
> main ()
> {
>   float num = 0.9;
>   double dnum = 0.9;
>   long double ldnum = 25.6;
>   printf("%50.48f\n", num);
>   printf("%50.48lf, %d\n", num, sizeof(num));
>   printf("%80.78f\n", dnum);
>   printf("%80.78lf, %d\n", dnum, sizeof(dnum));
>   printf("%80.78f\n", ldnum);
>   printf("%80.78lf, %d\n", ldnum, sizeof(ldnum));

Check your format strings, use "lf" only for long double.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libfortran/48651] DTOA float conversion issue
  2011-04-17 14:00 [Bug libfortran/48651] New: DTOA float conversion issue jvdelisle at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2011-04-17 18:34 ` thenlich at users dot sourceforge.net
@ 2011-04-17 19:03 ` jvdelisle at gcc dot gnu.org
  2011-04-17 23:39 ` kargl at gcc dot gnu.org
  2011-04-18  2:49 ` jvdelisle at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jvdelisle at gcc dot gnu.org @ 2011-04-17 19:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jerry DeLisle <jvdelisle at gcc dot gnu.org> 2011-04-17 19:03:00 UTC ---
OK Format string needs capital 'L' for long double.

$ cat sprint.c 
#include <stdio.h>

int
main ()
{
  long double ldnum;
  ldnum = 25.6;
  printf("%30.24Lf, %d\n", ldnum, sizeof(ldnum));
}
$ gcc sprint.c 
$ ./a.out 
   25.600000000000001421085472, 16


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libfortran/48651] DTOA float conversion issue
  2011-04-17 14:00 [Bug libfortran/48651] New: DTOA float conversion issue jvdelisle at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2011-04-17 19:03 ` jvdelisle at gcc dot gnu.org
@ 2011-04-17 23:39 ` kargl at gcc dot gnu.org
  2011-04-18  2:49 ` jvdelisle at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: kargl at gcc dot gnu.org @ 2011-04-17 23:39 UTC (permalink / raw)
  To: gcc-bugs

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

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kargl at gcc dot gnu.org

--- Comment #5 from kargl at gcc dot gnu.org 2011-04-17 23:38:48 UTC ---
(In reply to comment #3)
> (In reply to comment #2)
> > hmm, notice the typo! i have set ldnum to 0.0 but it still is printing the
> > value of dnum. What am I doing wrong.
> > 
> > $ cat sprint.c 
> > #include <stdio.h>
> > 
> > int
> > main ()
> > {
> >   float num = 0.9;
> >   double dnum = 0.9;
> >   long double ldnum = 25.6;
> >   printf("%50.48f\n", num);
> >   printf("%50.48lf, %d\n", num, sizeof(num));
> >   printf("%80.78f\n", dnum);
> >   printf("%80.78lf, %d\n", dnum, sizeof(dnum));
> >   printf("%80.78f\n", ldnum);
> >   printf("%80.78lf, %d\n", ldnum, sizeof(ldnum));
> 
> Check your format strings, use "lf" only for long double.

man 3 printf

         Modifier    a, A, e, E, f, F, g, G
         l (ell)     double (ignored, same behavior as without it)
         L           long double

L is for long double.  f and lf are essentially identical for double.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libfortran/48651] DTOA float conversion issue
  2011-04-17 14:00 [Bug libfortran/48651] New: DTOA float conversion issue jvdelisle at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2011-04-17 23:39 ` kargl at gcc dot gnu.org
@ 2011-04-18  2:49 ` jvdelisle at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jvdelisle at gcc dot gnu.org @ 2011-04-18  2:49 UTC (permalink / raw)
  To: gcc-bugs

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

Jerry DeLisle <jvdelisle at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #6 from Jerry DeLisle <jvdelisle at gcc dot gnu.org> 2011-04-18 02:48:03 UTC ---
OK,

After reading the manual.

#include <stdio.h>

int
main ()
{
  float num1 = 0.90;
  double num2 = 0.90l;
  long double num3 = 0.90L;
  printf("%+-#48.*e\n", 46, num1);
  printf("%+-#48.*le\n", 46, num2);
  printf("%+-#48.*Le\n", 46, num3);
}

Gives:

$ ./a.out 
+8.9999997615814208984375000000000000000000000000e-01
+9.0000000000000002220446049250313080847263336182e-01
+8.9999999999999999997831595655028991131985094398e-01

This is rounding to nearest which for double, just happens to be slightly above
.9 rather than slightly below.  I conclude this is correct and not a bug in
either libgfortran or libc.

Its an artifact of inexact biray representation of a decimal number.  Closing
this PR as invalid.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-04-18  2:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-17 14:00 [Bug libfortran/48651] New: DTOA float conversion issue jvdelisle at gcc dot gnu.org
2011-04-17 18:07 ` [Bug libfortran/48651] " jvdelisle at gcc dot gnu.org
2011-04-17 18:29 ` jvdelisle at gcc dot gnu.org
2011-04-17 18:34 ` thenlich at users dot sourceforge.net
2011-04-17 19:03 ` jvdelisle at gcc dot gnu.org
2011-04-17 23:39 ` kargl at gcc dot gnu.org
2011-04-18  2:49 ` jvdelisle at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).