![]() |
double problem
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 |
Re: double problem
Le 26/08/2004 à 08:59:39, Nomak <no.email@invalid.domain.fr> 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. |
Re: double problem
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 |
Re: double problem
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 |
| All times are GMT. The time now is 06:44 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.