bug in numeric_cast<unsigned long>, <unsigned char>, and <unsigne d int> in gcc 3.3.2
Hello, I am seeing a bug that the regression tests miss for numeric_cast<unsigned long> with gcc 3.3.2. Specifically, the following code is throwing an exception: double small_value = 1.25; unsigned long l=0; l = numeric_cast<unsigned long>(small_value); // throws exception! unsinged char c=0; c = numeric_cast<unsigned char>(small_value); // throws exception! I am under the impression that numeric_cast is supposed to accept losses of precision (by truncating the fractional values), and to throw exceptions only if there is a range or sign error. Is this so? This error happens only when I try to cast a value with a non-zero fraction; i.e. 1.25 breaks but 1.0 is fine. It only occurs for unsigned types -- signed int, char, and long appear to work correctly. I have found the same incorrect behavior with gcc 3.3.2 on both solaris and linux. Using sunpro on solaris, the same code runs correctly. The regression tests don't catch this, since they only test numeric_cast with integral values 1 and LONG_MAX. Any ideas? It looks like a compiler problem to me. Davis King william.d.king@bankofamerica.com
"King, William D"
Hello,
I am seeing a bug that the regression tests miss for numeric_cast<unsigned long> with gcc 3.3.2. Specifically, the following code is throwing an exception:
double small_value = 1.25; unsigned long l=0; l = numeric_cast<unsigned long>(small_value); // throws exception! unsinged char c=0; c = numeric_cast<unsigned char>(small_value); // throws exception!
I am under the impression that numeric_cast is supposed to accept losses
of
precision (by truncating the fractional values), and to throw exceptions only if there is a range or sign error. Is this so?
FWIW, the new Numeric Conversions library has recently been accepted into boost. It works exactly as you expected, and has a replacement for numeric_cast<>. Check it out from: http://personales.ciudad.com.ar/fernando_cacciola/numeric_conversions.zip HTH Fernando Cacciola SciSoft
participants (2)
-
Fernando Cacciola
-
King, William D