446,413 Members | 1,023 Online Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

# double problem

 P: n/a Hello, i've lost about hours (at least) to find a problem. I use the following utility function: 13 template 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 (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 Jul 22 '05 #1
3 Replies

 P: n/a Le 26/08/2004 à 08:59:39, Nomak a écrit: Hello, i've lost about hours (at least) to find a problem. I use the following utility function: 13 template 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 (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. Jul 22 '05 #2

 P: n/a Nomak wrote: Hello, i've lost about hours (at least) to find a problem. I use the following utility function: 13 template 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 (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 #include int main ( void ) { double nan = std::numeric_limits::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 Jul 22 '05 #3

 P: n/a 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 Jul 22 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 