double problem

N

Nomak

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
 
N

Nomak

Le 26/08/2004 à 08:59:39 said:
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
 
K

Kai-Uwe Bux

Nomak said:
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
 
M

Matthew Hall

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top