public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/50773] New: float values are printed with greater precision than the float data type has when given as an argument to printf()
@ 2011-10-18  9:34 momchil at xaxo dot eu
  2011-10-18 10:06 ` [Bug c/50773] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: momchil at xaxo dot eu @ 2011-10-18  9:34 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 50773
           Summary: float values are printed with greater precision than
                    the float data type has when given as an argument to
                    printf()
    Classification: Unclassified
           Product: gcc
           Version: 4.5.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: momchil@xaxo.eu


test case:

#include <stdio.h>

int
main(int argc, char *argv[])
{
    float a = 268517138.f;
    printf(" 268517138.f = %f\t%f\n", 268517138.f, a);

    return 0;
}

output when compiling with -fexcess-precision=standard:
 268517138.f = 268517138.000000    268517152.000000

output when compiling with -fexcess-precision=fast:
 268517138.f = 268517152.000000    268517152.000000

reference thread: http://gcc.gnu.org/ml/gcc-help/2011-10/msg00120.html

gcc versions tested:

Using built-in specs.
COLLECT_GCC=gcc46
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc46/gcc/i386-portbld-freebsd8.1/4.6.0/lto-wrapper
Target: i386-portbld-freebsd8.1
Configured with: ./../gcc-4.6-20110101/configure --enable-lto=no --disable-nls
--libdir=/usr/local/lib/gcc46 --libexecdir=/usr/local/libexec/gcc46
--program-suffix=46 --with-as=/usr/local/bin/as --with-gmp=/usr/local
--with-gxx-include-dir=/usr/local/lib/gcc46/include/c++/
--with-ld=/usr/local/bin/ld --with-libiconv-prefix=/usr/local
--with-system-zlib --disable-rpath --prefix=/usr/local --mandir=/usr/local/man
--infodir=/usr/local/info/gcc46 --build=i386-portbld-freebsd8.1
Thread model: posix
gcc version 4.6.0 20110101 (experimental) (GCC) 

Using built-in specs.
COLLECT_GCC=gcc45
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc45/gcc/i386-portbld-freebsd8.2/4.5.3/lto-wrapper
Target: i386-portbld-freebsd8.2
Configured with: ./../gcc-4.5-20110310/configure --enable-lto=yes
--with-libelf=/usr/local --disable-nls --libdir=/usr/local/lib/gcc45
--libexecdir=/usr/local/libexec/gcc45 --program-suffix=45
--with-as=/usr/local/bin/as --with-gmp=/usr/local
--with-gxx-include-dir=/usr/local/lib/gcc45/include/c++/
--with-ld=/usr/local/bin/ld --with-libiconv-prefix=/usr/local
--with-system-zlib --disable-rpath --enable-libgcj --prefix=/usr/local
--mandir=/usr/local/man --infodir=/usr/local/info/gcc45
--build=i386-portbld-freebsd8.2
Thread model: posix
gcc version 4.5.3 20110310 (prerelease) (GCC)


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

* [Bug c/50773] float values are printed with greater precision than the float data type has when given as an argument to printf()
  2011-10-18  9:34 [Bug c/50773] New: float values are printed with greater precision than the float data type has when given as an argument to printf() momchil at xaxo dot eu
@ 2011-10-18 10:06 ` rguenth at gcc dot gnu.org
  2011-10-18 10:21 ` aph at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-10-18 10:06 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

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

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-10-18 10:05:56 UTC ---
%f prints doubles, float arguments to variadic argument functions are promoted
to double (thus, you can't print a "float" value with printf)


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

* [Bug c/50773] float values are printed with greater precision than the float data type has when given as an argument to printf()
  2011-10-18  9:34 [Bug c/50773] New: float values are printed with greater precision than the float data type has when given as an argument to printf() momchil at xaxo dot eu
  2011-10-18 10:06 ` [Bug c/50773] " rguenth at gcc dot gnu.org
@ 2011-10-18 10:21 ` aph at gcc dot gnu.org
  2011-10-18 10:43 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: aph at gcc dot gnu.org @ 2011-10-18 10:21 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Haley <aph at gcc dot gnu.org> changed:

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

--- Comment #2 from Andrew Haley <aph at gcc dot gnu.org> 2011-10-18 10:20:40 UTC ---
This is definitely a C front-end bug.  If you look at the gimple dump, you can
see that the literal 268517138.f isn't being correctly truncated, but instead
the constant 2.68517138e+8 is being used instead:

main (int argc, char * * argv)
{
  double D.2549;
  const char * restrict D.2550;
  double D.2551;
  const char * restrict D.2552;
  int D.2553;
  float a;

  a = 2.68517152e+8;
  D.2549 = (double) a;
  D.2550 = (const char * restrict) &" 268517138.f = %f\t%f\n"[0];
  printf (D.2550, 2.68517138e+8, D.2549);

C++ FE does it correctly:

int main(int, char**) (int argc, char * * argv)
{
  double D.2226;
  double D.2227;
  double D.2228;
  double D.2229;
  int D.2230;

  {
    float a;

    a = 2.68517152e+8;
    D.2226 = (double) a;
    D.2227 = (double) 2.68517152e+8;
    printf (&" 268517138.f = %f\t%f\n"[0], D.2227, D.2226);


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

* [Bug c/50773] float values are printed with greater precision than the float data type has when given as an argument to printf()
  2011-10-18  9:34 [Bug c/50773] New: float values are printed with greater precision than the float data type has when given as an argument to printf() momchil at xaxo dot eu
  2011-10-18 10:06 ` [Bug c/50773] " rguenth at gcc dot gnu.org
  2011-10-18 10:21 ` aph at gcc dot gnu.org
@ 2011-10-18 10:43 ` rguenth at gcc dot gnu.org
  2011-10-18 14:50 ` joseph at codesourcery dot com
  2014-04-18 20:42 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-10-18 10:43 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
             Status|RESOLVED                    |REOPENED
   Last reconfirmed|                            |2011-10-18
                 CC|                            |jsm28 at gcc dot gnu.org
         Resolution|INVALID                     |
     Ever Confirmed|0                           |1
      Known to fail|                            |4.7.0

--- Comment #3 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-10-18 10:43:08 UTC ---
(In reply to comment #2)
> This is definitely a C front-end bug.  If you look at the gimple dump, you can
> see that the literal 268517138.f isn't being correctly truncated, but instead
> the constant 2.68517138e+8 is being used instead:
> 
> main (int argc, char * * argv)
> {
>   double D.2549;
>   const char * restrict D.2550;
>   double D.2551;
>   const char * restrict D.2552;
>   int D.2553;
>   float a;
> 
>   a = 2.68517152e+8;
>   D.2549 = (double) a;
>   D.2550 = (const char * restrict) &" 268517138.f = %f\t%f\n"[0];
>   printf (D.2550, 2.68517138e+8, D.2549);
> 
> C++ FE does it correctly:
> 
> int main(int, char**) (int argc, char * * argv)
> {
>   double D.2226;
>   double D.2227;
>   double D.2228;
>   double D.2229;
>   int D.2230;
> 
>   {
>     float a;
> 
>     a = 2.68517152e+8;
>     D.2226 = (double) a;
>     D.2227 = (double) 2.68517152e+8;
>     printf (&" 268517138.f = %f\t%f\n"[0], D.2227, D.2226);

Needs -fexcess-precision=standard -m32 to trigger.  libcpp does the
parsing of FP constants IIRC, and the C++ frontend does not implement
-fexcess-precision.

CCing Joseph.

Testcase that fails with -fexcess-precision=standard -m32:

extern void abort (void);
int
main()
{
      float a = 268517138.f;
      if (a != 268517138.f)
        abort ();
      return 0;
}


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

* [Bug c/50773] float values are printed with greater precision than the float data type has when given as an argument to printf()
  2011-10-18  9:34 [Bug c/50773] New: float values are printed with greater precision than the float data type has when given as an argument to printf() momchil at xaxo dot eu
                   ` (2 preceding siblings ...)
  2011-10-18 10:43 ` rguenth at gcc dot gnu.org
@ 2011-10-18 14:50 ` joseph at codesourcery dot com
  2014-04-18 20:42 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: joseph at codesourcery dot com @ 2011-10-18 14:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from joseph at codesourcery dot com <joseph at codesourcery dot com> 2011-10-18 14:49:28 UTC ---
On Tue, 18 Oct 2011, rguenth at gcc dot gnu.org wrote:

> Needs -fexcess-precision=standard -m32 to trigger.  libcpp does the
> parsing of FP constants IIRC, and the C++ frontend does not implement
> -fexcess-precision.
> 
> CCing Joseph.
> 
> Testcase that fails with -fexcess-precision=standard -m32:
> 
> extern void abort (void);
> int
> main()
> {
>       float a = 268517138.f;
>       if (a != 268517138.f)
>         abort ();
>       return 0;
> }

I don't see any bug there.  With -fexcess-precision=standard, 
FLT_EVAL_METHOD is 2, "evaluate all operations and constants to the range 
and precision of the long double type".  Thus 268517138.f has type float 
but a value represented to the precision of long double - and the 
conversion to the precision of float takes place for the initialization 
but not for the comparison.  (Without -fexcess-precision=standard - 
outside any conformance mode - FLT_EVAL_METHOD is still 2, but exactly 
what happens is unpredictable.)


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

* [Bug c/50773] float values are printed with greater precision than the float data type has when given as an argument to printf()
  2011-10-18  9:34 [Bug c/50773] New: float values are printed with greater precision than the float data type has when given as an argument to printf() momchil at xaxo dot eu
                   ` (3 preceding siblings ...)
  2011-10-18 14:50 ` joseph at codesourcery dot com
@ 2014-04-18 20:42 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2014-04-18 20:42 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |mpolacek at gcc dot gnu.org
         Resolution|---                         |INVALID

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Closing as not a bug then.


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

end of thread, other threads:[~2014-04-18 20:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-18  9:34 [Bug c/50773] New: float values are printed with greater precision than the float data type has when given as an argument to printf() momchil at xaxo dot eu
2011-10-18 10:06 ` [Bug c/50773] " rguenth at gcc dot gnu.org
2011-10-18 10:21 ` aph at gcc dot gnu.org
2011-10-18 10:43 ` rguenth at gcc dot gnu.org
2011-10-18 14:50 ` joseph at codesourcery dot com
2014-04-18 20:42 ` mpolacek 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).