public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/50695] New: double comparison broken after computation
@ 2011-10-11  2:24 gpib at rickyrockrat dot net
  2011-10-11  8:54 ` [Bug c/50695] " rguenth at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: gpib at rickyrockrat dot net @ 2011-10-11  2:24 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 50695
           Summary: double comparison broken after computation
    Classification: Unclassified
           Product: gcc
           Version: 4.4.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: gpib@rickyrockrat.net


1e-6*1e6 != 1

The following minimal code segment fails (compile with gcc -o x x.c):

#include <string.h>
int main(int argc, char *argv){ 
double x,y;
x=strtof(",+1.00000E-06,",NULL);
y=1000;
y*=1000;
if(x*y!=1)
printf("Bug!!\n");
else printf("Nobug\n");
}
gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5'
--with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--enable-multiarch --enable-linker-build-id --with-system-zlib
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls
--enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc
--disable-werror --with-arch-32=i486 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)

Also fails with:

Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5'
--with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--enable-multiarch --enable-linker-build-id --with-system-zlib
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls
--enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc
--disable-werror --with-arch-32=i486 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
@ 2011-10-11  8:54 ` rguenth at gcc dot gnu.org
  2011-10-11  8:59 ` rguenth at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-10-11  8:54 UTC (permalink / raw)
  To: gcc-bugs

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

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-11 08:54:18 UTC ---
Use strtod.


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
  2011-10-11  8:54 ` [Bug c/50695] " rguenth at gcc dot gnu.org
@ 2011-10-11  8:59 ` rguenth at gcc dot gnu.org
  2011-10-11  9:11 ` schwab@linux-m68k.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-10-11  8:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-10-11 08:58:45 UTC ---
And provide a prototype of strtof/strtod that does not return int.


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
  2011-10-11  8:54 ` [Bug c/50695] " rguenth at gcc dot gnu.org
  2011-10-11  8:59 ` rguenth at gcc dot gnu.org
@ 2011-10-11  9:11 ` schwab@linux-m68k.org
  2011-10-11 12:24 ` marc.glisse at normalesup dot org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: schwab@linux-m68k.org @ 2011-10-11  9:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andreas Schwab <schwab@linux-m68k.org> 2011-10-11 09:11:11 UTC ---
That won't help anyway since ",+1.00000E-06," does not start with a valid
floating point number and will always be parsed as 0.


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
                   ` (2 preceding siblings ...)
  2011-10-11  9:11 ` schwab@linux-m68k.org
@ 2011-10-11 12:24 ` marc.glisse at normalesup dot org
  2011-10-11 19:05 ` gpib at rickyrockrat dot net
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: marc.glisse at normalesup dot org @ 2011-10-11 12:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Marc Glisse <marc.glisse at normalesup dot org> 2011-10-11 12:24:18 UTC ---
And anyway 10^-6 is not representable exactly as a double.


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
                   ` (3 preceding siblings ...)
  2011-10-11 12:24 ` marc.glisse at normalesup dot org
@ 2011-10-11 19:05 ` gpib at rickyrockrat dot net
  2011-10-11 19:06 ` gpib at rickyrockrat dot net
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: gpib at rickyrockrat dot net @ 2011-10-11 19:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from rickyrockrat <gpib at rickyrockrat dot net> 2011-10-11 19:05:28 UTC ---
>+1.00000E-06," does not start with a valid
>floating point number and will always be parsed as 0. 

I don't know what 'always will be', nor who exactly is doing the parsing, but
strtof, at least, seems to parse it correctly, because a print of said variable
prints as 1.00000E-06....  Believe me, I checked.

Ok, here's a new test case, still broken:
#include <string.h>
int main(int argc, char *argv){ 
double x,y;
x=strtod(",+1.00000E-06,",NULL);
y=1000;
y*=1000;
if(x*y!=1)
printf("Bug!!%E\n",x);
else printf("Nobug\n");}

And the output is:
Bug!!4.400000E+01

How is it resolved?


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
                   ` (4 preceding siblings ...)
  2011-10-11 19:05 ` gpib at rickyrockrat dot net
@ 2011-10-11 19:06 ` gpib at rickyrockrat dot net
  2011-10-11 19:25 ` gpib at rickyrockrat dot net
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: gpib at rickyrockrat dot net @ 2011-10-11 19:06 UTC (permalink / raw)
  To: gcc-bugs

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

rickyrockrat <gpib at rickyrockrat dot net> changed:

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

--- Comment #6 from rickyrockrat <gpib at rickyrockrat dot net> 2011-10-11 19:06:23 UTC ---
Using strtod has the same problem.


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
                   ` (5 preceding siblings ...)
  2011-10-11 19:06 ` gpib at rickyrockrat dot net
@ 2011-10-11 19:25 ` gpib at rickyrockrat dot net
  2011-10-11 19:34 ` gpib at rickyrockrat dot net
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: gpib at rickyrockrat dot net @ 2011-10-11 19:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from rickyrockrat <gpib at rickyrockrat dot net> 2011-10-11 19:25:10 UTC ---
I removed the ','at the beginning of the string (which was not there in the
original test case), and I now get

Bug!!4.074850E+05

In any case, it should return 0, if +1.xxE-6 is an invalid number.

When I include these includes:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

my print looks like this:
Bug!!1.000000E-06

Which is what perplexed me in the first place. It seems if I include stdlib.h,
I get 
Bug!!4.074850E+05
If I don't include it, I get:
Bug!!1.000000E-06, which is what prompted this bug report in the first place.


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
                   ` (6 preceding siblings ...)
  2011-10-11 19:25 ` gpib at rickyrockrat dot net
@ 2011-10-11 19:34 ` gpib at rickyrockrat dot net
  2011-10-11 19:39 ` gpib at rickyrockrat dot net
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: gpib at rickyrockrat dot net @ 2011-10-11 19:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from rickyrockrat <gpib at rickyrockrat dot net> 2011-10-11 19:33:47 UTC ---
Created attachment 25469
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=25469
stdlib.h

Tar for string.h, stdlib.h, and stdio.h on the system.


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
                   ` (7 preceding siblings ...)
  2011-10-11 19:34 ` gpib at rickyrockrat dot net
@ 2011-10-11 19:39 ` gpib at rickyrockrat dot net
  2011-10-12 11:35 ` rguenth at gcc dot gnu.org
  2011-10-12 17:40 ` gpib at rickyrockrat dot net
  10 siblings, 0 replies; 12+ messages in thread
From: gpib at rickyrockrat dot net @ 2011-10-11 19:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from rickyrockrat <gpib at rickyrockrat dot net> 2011-10-11 19:38:51 UTC ---
One further note, with stdio.h, string.h and using strtod, I get the correct
answer suggested by Andreas Schwab:
Bug!!0.000000E+00

If I put stdio.h, string.h, and stdlib.h, I get
Nobug

Something doesn't make sense.


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
                   ` (8 preceding siblings ...)
  2011-10-11 19:39 ` gpib at rickyrockrat dot net
@ 2011-10-12 11:35 ` rguenth at gcc dot gnu.org
  2011-10-12 17:40 ` gpib at rickyrockrat dot net
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-10-12 11:35 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #10 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-10-12 11:35:19 UTC ---
(In reply to comment #9)
> One further note, with stdio.h, string.h and using strtod, I get the correct
> answer suggested by Andreas Schwab:
> Bug!!0.000000E+00
> 
> If I put stdio.h, string.h, and stdlib.h, I get
> Nobug
> 
> Something doesn't make sense.

It makes perfect sense.  Without a prototype for strtof (which is provided
by stdlib.h) strtof get's implicitely declares as

int strtof();

which means the return value is expected to be an integer returned in %eax
which then gets converted to floating-point.  But in reality strtof is

float strtof(const char *nptr, char **endptr);

and the return value is returned in %xmm0 and only a widening from float
to double would be necessary.

Thus, your program is invalid and it will just use garbage that happens
to be present in %eax.

GCC tells you this when you append -Wall:

> gcc-4.4 -S t.c -Wall
t.c:2: warning: second argument of ‘main’ should be ‘char **’
t.c: In function ‘main’:
t.c:4: warning: implicit declaration of function ‘strtof’
t.c:8: warning: implicit declaration of function ‘printf’
t.c:8: warning: incompatible implicit declaration of built-in function ‘printf’
t.c:10: warning: control reaches end of non-void function


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

* [Bug c/50695] double comparison broken after computation
  2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
                   ` (9 preceding siblings ...)
  2011-10-12 11:35 ` rguenth at gcc dot gnu.org
@ 2011-10-12 17:40 ` gpib at rickyrockrat dot net
  10 siblings, 0 replies; 12+ messages in thread
From: gpib at rickyrockrat dot net @ 2011-10-12 17:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from rickyrockrat <gpib at rickyrockrat dot net> 2011-10-12 17:40:25 UTC ---
Richard,
The original issue involved strtof, with -Wall enabled in the Makefile, and
stdlib.h included. That is the case that brought all this up, and that is the
case where it prints Bug!!1.000000E-06.  I still don't understand why widening
the result from float to double would cause the problem.

I apologize for my pathetic test cases that not only made me look like an
idiot, but threw confusion into the mix as well.  I suppose I shouldn't submit
a bug report while I do 10 other things.

It doesn't seem to me it's resolved, but everyone here seems to think it
invalid, and I've got a work-around that works for me, so I give up.
Thanks.


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

end of thread, other threads:[~2011-10-12 17:40 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-11  2:24 [Bug c/50695] New: double comparison broken after computation gpib at rickyrockrat dot net
2011-10-11  8:54 ` [Bug c/50695] " rguenth at gcc dot gnu.org
2011-10-11  8:59 ` rguenth at gcc dot gnu.org
2011-10-11  9:11 ` schwab@linux-m68k.org
2011-10-11 12:24 ` marc.glisse at normalesup dot org
2011-10-11 19:05 ` gpib at rickyrockrat dot net
2011-10-11 19:06 ` gpib at rickyrockrat dot net
2011-10-11 19:25 ` gpib at rickyrockrat dot net
2011-10-11 19:34 ` gpib at rickyrockrat dot net
2011-10-11 19:39 ` gpib at rickyrockrat dot net
2011-10-12 11:35 ` rguenth at gcc dot gnu.org
2011-10-12 17:40 ` gpib at rickyrockrat dot net

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).