Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem comparing a double with 0

Reply
Thread Tools

Problem comparing a double with 0

 
 
=?iso-8859-1?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      07-02-2003
Hello.

This test:

#include <stdio.h>

int main ()
{
double a= -1.0e-120;
if (a < 0.0)
printf ("%g < 0\n", a);
if (a > 0.0)
printf ("%g > 0\n", a);
if (a == 0.0)
printf ("%g == 0\n", a);
}

Compiled with gcc 3.3 as a C program gives -1e-120 < 0, but compiled as
C++ gives me -1e-120 == 0.

I suspect that is a gcc problem, but is some workaround possible? How
can I reliably compute the sign of a?

Regards.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-02-2003
"Julián Albo" <> wrote...
> This test:
>
> #include <stdio.h>
>
> int main ()
> {
> double a= -1.0e-120;
> if (a < 0.0)
> printf ("%g < 0\n", a);
> if (a > 0.0)
> printf ("%g > 0\n", a);
> if (a == 0.0)
> printf ("%g == 0\n", a);
> }
>
> Compiled with gcc 3.3 as a C program gives -1e-120 < 0, but compiled as
> C++ gives me -1e-120 == 0.
>
> I suspect that is a gcc problem, but is some workaround possible? How
> can I reliably compute the sign of a?


As far as the sign is concerned, you're doing it right. If g++
can't generate right code for such a simple comparison or for such
a simple initialisation, it's a bug in the compiler. If you need
a work-around, you should probably ask in gnu.g++.help.

You _could_ test the high-order bit for the sign, and that should
always work for IEEE doubles. Of course, testing a bit works only
on unsigned integral types, so you'd have to extract the top byte.
And then the endianness comes into play... It's not portable, IOW.

Try looking at the assembly code it generates to make sure whether
it's a bug, and possibly to see what workaround you could apply.

Victor


 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      07-02-2003
Victor Bazarov escribió:

> > #include <stdio.h>
> >
> > int main ()
> > {
> > double a= -1.0e-120;
> > if (a < 0.0)
> > printf ("%g < 0\n", a);
> > if (a > 0.0)
> > printf ("%g > 0\n", a);
> > if (a == 0.0)
> > printf ("%g == 0\n", a);
> > }
> >
> > Compiled with gcc 3.3 as a C program gives -1e-120 < 0, but compiled as
> > C++ gives me -1e-120 == 0.
> >
> > I suspect that is a gcc problem, but is some workaround possible? How
> > can I reliably compute the sign of a?

>
> As far as the sign is concerned, you're doing it right. If g++
> can't generate right code for such a simple comparison or for such
> a simple initialisation, it's a bug in the compiler. If you need
> a work-around, you should probably ask in gnu.g++.help.
> You _could_ test the high-order bit for the sign, and that should
> always work for IEEE doubles. Of course, testing a bit works only
> on unsigned integral types, so you'd have to extract the top byte.
> And then the endianness comes into play... It's not portable, IOW.
>
> Try looking at the assembly code it generates to make sure whether
> it's a bug, and possibly to see what workaround you could apply.


Thak you for your suggestions. The workaround I found is using instead
of the literal 0.0 a variable with the value 0 and no const. If the
variable is const, the problem remains.

Regards.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing double in for loop eman.abu.samra@gmail.com C++ 19 04-11-2008 12:36 PM
from List <double> to double[] Web learner ASP .Net 3 04-26-2006 05:26 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Double double display display problem problem Tom Accuosti Firefox 3 09-27-2004 10:02 PM
Wrong results when comparing negative double variables in an if statement John C Programming 11 05-05-2004 04:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57