Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > double problem

Reply
Thread Tools

double problem

 
 
Nomak
Guest
Posts: n/a
 
      08-26-2004
Hello,

i've lost about hours (at least) to find a problem. I use the
following utility function:

13 template <typename T>
14 inline
15 T
16 diffabs(T a, T b)
17 {
18 T ret;
19
20 if (a > b)
21 ret = a - b;
22 else
23 ret = b - a;
24
25 if (ret < 0)
26 cerr << "ERROR: diffabs(" << a << ", "
27 << b << ") = " << ret << endl;
28
29 assert(ret >= 0);
30 return ret;
31 }

The verification part have been added after founding the bug.

At runtime, i have the assertion wich fails, but no error message, so
i run gdb:

#7 0x08050747 in diffabs<double> (a=-nan(0x8000000000000), b=-1)
29 assert(ret >= 0);
(gdb) print ret
$1 = -nan(0x8000000000000)

I don't even know wich path is taken. But the error message test fails
(<0) while the assert test is ok (>=0).

Please tell me it's not the way it should be and it's a compiler bug.

If it's the standard way, do you know why? And how can i detect if a
double if negativ or -nan ?

TIA

--
Nomak
 
Reply With Quote
 
 
 
 
Nomak
Guest
Posts: n/a
 
      08-26-2004
Le 26/08/2004 à 08:59:39, Nomak <(E-Mail Removed)> a écrit:

> Hello,
>
> i've lost about hours (at least) to find a problem. I use the
> following utility function:
>
> 13 template <typename T>
> 14 inline
> 15 T
> 16 diffabs(T a, T b)
> 17 {
> 18 T ret;
> 19
> 20 if (a > b)
> 21 ret = a - b;
> 22 else
> 23 ret = b - a;
> 24
> 25 if (ret < 0)
> 26 cerr << "ERROR: diffabs(" << a << ", "
> 27 << b << ") = " << ret << endl;
> 28
> 29 assert(ret >= 0);
> 30 return ret;
> 31 }
>
> The verification part have been added after founding the bug.
>
> At runtime, i have the assertion wich fails, but no error message, so
> i run gdb:
>
> #7 0x08050747 in diffabs<double> (a=-nan(0x8000000000000), b=-1)
> 29 assert(ret >= 0);
> (gdb) print ret
> $1 = -nan(0x8000000000000)
>
> I don't even know wich path is taken. But the error message test fails
> (<0) while the assert test is ok (>=0).
>
> Please tell me it's not the way it should be and it's a compiler bug.
>
> If it's the standard way, do you know why? And how can i detect if a
> double if negativ or -nan ?
>
> TIA


ok, i've seen the faq, too bad

--
Nomak
Even if a samurai's head were to be suddenly cut off, he
should still be able to perform one more action with certainty.
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-26-2004
Nomak wrote:

> Hello,
>
> i've lost about hours (at least) to find a problem. I use the
> following utility function:
>
> 13 template <typename T>
> 14 inline
> 15 T
> 16 diffabs(T a, T b)
> 17 {
> 18 T ret;
> 19
> 20 if (a > b)
> 21 ret = a - b;
> 22 else
> 23 ret = b - a;
> 24
> 25 if (ret < 0)
> 26 cerr << "ERROR: diffabs(" << a << ", "
> 27 << b << ") = " << ret << endl;
> 28
> 29 assert(ret >= 0);
> 30 return ret;
> 31 }
>
> The verification part have been added after founding the bug.
>
> At runtime, i have the assertion wich fails, but no error message, so
> i run gdb:
>
> #7 0x08050747 in diffabs<double> (a=-nan(0x8000000000000), b=-1)
> 29 assert(ret >= 0);
> (gdb) print ret
> $1 = -nan(0x8000000000000)
>


The assertion fails and you do not see the error message. The reason is that
the value of ret is "not_a_number". This particular value does not compare
the usual way:

#include <limits>
#include <iostream>

int main ( void ) {
double nan = std::numeric_limits<double>::quiet_NaN();
std::cout << ( nan < 0 ) << "\n"
<< ( nan > 0 ) << "\n"
<< ( nan <= 0 ) << "\n"
<< ( nan >= 0 ) << "\n"
<< ( nan >= 0 ) << "\n"
<< ( nan < nan ) << "\n"
<< ( nan > nan ) << "\n"
<< ( nan == nan ) << "\n"
<< ( nan <= nan ) << "\n"
<< ( nan >= nan ) << "\n";
}

output:

0
0
0
0
0
0
0
0
0
0

And this output is expected.

> I don't even know wich path is taken. But the error message test fails
> (<0) while the assert test is ok (>=0).
>
> Please tell me it's not the way it should be and it's a compiler bug.
>
> If it's the standard way, do you know why? And how can i detect if a
> double if negativ or -nan ?
>
> TIA
>


One way to check for nan is to use the fact that !( nan == nan ) is true.


Best

Kai-Uwe Bux
 
Reply With Quote
 
Matthew Hall
Guest
Posts: n/a
 
      08-26-2004
Kai-Uwe Bux wrote:
.....
>
> One way to check for nan is to use the fact that !( nan == nan ) is true.
>
>
> Best
>
> Kai-Uwe Bux


Just a note - there are compilers/platforms where (x==x) returns true
even if x is nan - the mipspro compiler (at least the 7.3.2.1m version
installed here) is one such example, even with all optimizations
disabled. If you care about portability to such machines, you may wish
to use 'isnan(x)' as defined in cmath (or math.h on Irix machines). I
have no idea of the relative speed of isnan to (x==x), but I do know
isnan can be slow (somewhat slower than floating point division on my
intel boxes w/linux).

-matt
 
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
from List <double> to double[] Web learner ASP .Net 3 04-26-2006 05:26 PM
Should I be concerned about buying DVD18s (Double-sided, double-layered) The Man With No Name DVD Video 3 02-04-2006 01:45 AM
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
structs of double as double[] sb C++ 4 02-19-2004 07:39 PM



Advertisments