public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/12639] long double comparison is broken
       [not found] <bug-12639-0@http.gcc.gnu.org/bugzilla/>
@ 2005-10-16  1:24 ` gcc-bugzilla at gcc dot gnu dot org
  2005-10-16  9:07 ` falk at debian dot org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: gcc-bugzilla at gcc dot gnu dot org @ 2005-10-16  1:24 UTC (permalink / raw)
  To: gcc-bugs


The comparison between 0 and the largest power of 2 that fits in a long double
gives a non-zero result!!!

Environment:
System: OSF1 spe171.testdrive.hp.com V5.1 2650 alpha
Architecture: alphaev56-dec-osf5.1
host: alphaev56-dec-osf5.1
build: alphaev56-dec-osf5.1
target: alphaev56-dec-osf5.1
configured with: ../configure

How-To-Repeat:

spe171.testdrive.hp.com> cat bug.c
#include <stdio.h>
#include <stdlib.h>
#include <float.h>

int
main ()
{
  long double d;
  d = 1.0; while (d < LDBL_MAX / 2.0) d += d;
  printf ("d=%1.30Le\n", d);
  if (d == (long double) 0.0)
    printf ("d equals 0.0\n");
}

spe171.testdrive.hp.com> gcc -O1 bug.c
spe171.testdrive.hp.com> ./a.out
d=5.948657476786158825428796633140e+4931
d equals 0.0


------- Comment #1 from ehrhardt at mathematik dot uni-ulm dot de  2003-10-16 14:04 -------
Please read http://gcc.gnu.org/bugs.html. Also have a look at
bug 323 and its duplicates.


-- 

ehrhardt at mathematik dot uni-ulm dot de changed:

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


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


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

* [Bug c/12639] long double comparison is broken
       [not found] <bug-12639-0@http.gcc.gnu.org/bugzilla/>
  2005-10-16  1:24 ` [Bug c/12639] long double comparison is broken gcc-bugzilla at gcc dot gnu dot org
@ 2005-10-16  9:07 ` falk at debian dot org
  2005-10-27  9:11 ` [Bug target/12639] " christopher dot eltschka at physik dot uni-regensburg dot de
  2005-10-27 10:38 ` christopher dot eltschka at physik dot uni-regensburg dot de
  3 siblings, 0 replies; 5+ messages in thread
From: falk at debian dot org @ 2005-10-16  9:07 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from falk at debian dot org  2005-10-16 09:07 -------
(In reply to comment #1)
> Please read http://gcc.gnu.org/bugs.html. Also have a look at
> bug 323 and its duplicates.

Well, brushing off equality of 5*10^4931 and 0 as rounding error is maybe 
stretching it a bit ;-) This sure seems like a bug.


-- 

falk at debian dot org changed:

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


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


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

* [Bug target/12639] long double comparison is broken
       [not found] <bug-12639-0@http.gcc.gnu.org/bugzilla/>
  2005-10-16  1:24 ` [Bug c/12639] long double comparison is broken gcc-bugzilla at gcc dot gnu dot org
  2005-10-16  9:07 ` falk at debian dot org
@ 2005-10-27  9:11 ` christopher dot eltschka at physik dot uni-regensburg dot de
  2005-10-27 10:38 ` christopher dot eltschka at physik dot uni-regensburg dot de
  3 siblings, 0 replies; 5+ messages in thread
From: christopher dot eltschka at physik dot uni-regensburg dot de @ 2005-10-27  9:11 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from christopher dot eltschka at physik dot uni-regensburg dot de  2005-10-27 09:11 -------
I just stumbled on the same problem, but with much more conventional numbers.
The following C++ test program demonstrates it:

#include <iostream>

// this just serves to calculate a positive number which the optimizer
// will not figure out at compile time
long double f()
{
  long double value = 1.0;
  for (int i = 0; i < 10; ++i)
    value += (long double)i;
  return value;
}

int main()
{
  long double a = f();
  std::cout << a;
  if (a > 0)
    std::cout << " > 0\n";
  else if (a < 0)
    std::cout << " < 0\n";
  else if (a == 0)
    std::cout << " == 0\n";
  else
    std::cout << "oops!\n";
}

Compiling and running gives:
> g++ longdouble.cc -O3 && a.out
46 == 0

which is clearly wrong. The same result happens with -O2 and -O1. With -O0 I
correctly get the output 46 > 0.

The compiler is:

> g++ -v
Reading specs from /usr/local/lib/gcc-lib/alphaev67-dec-osf5.1/3.3/specs
Configured with: ../gcc-3.3/configure --with-libiconv-prefix=/usr/local
--with-included-gettext
Thread model: single
gcc version 3.3

As workaround for == 0, !! seems to work. Unfortunately I don't know a
workaround for > 0 and < 0.


-- 

christopher dot eltschka at physik dot uni-regensburg dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |christopher dot eltschka at
                   |                            |physik dot uni-regensburg
                   |                            |dot de


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


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

* [Bug target/12639] long double comparison is broken
       [not found] <bug-12639-0@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2005-10-27  9:11 ` [Bug target/12639] " christopher dot eltschka at physik dot uni-regensburg dot de
@ 2005-10-27 10:38 ` christopher dot eltschka at physik dot uni-regensburg dot de
  3 siblings, 0 replies; 5+ messages in thread
From: christopher dot eltschka at physik dot uni-regensburg dot de @ 2005-10-27 10:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from christopher dot eltschka at physik dot uni-regensburg dot de  2005-10-27 10:38 -------
(In reply to comment #3)
> As workaround for == 0, !! seems to work. Unfortunately I don't know a
> workaround for > 0 and < 0.
> 

Sorry for self-reply, but now I've found a simple workaround for all cases:
Instead of comparing with 0, just add 1 and compare with 1.
For some reason this works even if the number to compare with happens to be -1
(i.e. if after adding 1 you get 0).


-- 


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


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

* [Bug target/12639] long double comparison is broken
       [not found] <bug-12639-4@http.gcc.gnu.org/bugzilla/>
@ 2011-03-05 18:38 ` belyshev at depni dot sinp.msu.ru
  0 siblings, 0 replies; 5+ messages in thread
From: belyshev at depni dot sinp.msu.ru @ 2011-03-05 18:38 UTC (permalink / raw)
  To: gcc-bugs

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

Serge Belyshev <belyshev at depni dot sinp.msu.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
             Status|REOPENED                    |RESOLVED
                 CC|                            |belyshev at depni dot
                   |                            |sinp.msu.ru
      Known to work|                            |4.2.4, 4.3.5, 4.4.5, 4.5.2
         Resolution|                            |FIXED
           Severity|critical                    |normal

--- Comment #5 from Serge Belyshev <belyshev at depni dot sinp.msu.ru> 2011-03-05 18:37:52 UTC ---
Works on ev56 and ev67 linux-gnu with gcc 4.2 .. 4.5, assuming fixed.


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

end of thread, other threads:[~2011-03-05 18:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-12639-0@http.gcc.gnu.org/bugzilla/>
2005-10-16  1:24 ` [Bug c/12639] long double comparison is broken gcc-bugzilla at gcc dot gnu dot org
2005-10-16  9:07 ` falk at debian dot org
2005-10-27  9:11 ` [Bug target/12639] " christopher dot eltschka at physik dot uni-regensburg dot de
2005-10-27 10:38 ` christopher dot eltschka at physik dot uni-regensburg dot de
     [not found] <bug-12639-4@http.gcc.gnu.org/bugzilla/>
2011-03-05 18:38 ` belyshev at depni dot sinp.msu.ru

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