Hello! Functions for conversion from cl_I into built-in integer types are a little bit weird: if conversion fails, CLN just aborts the program. This weirdness is documented in the manual: \begin{quote} `int cl_I_to_int (const cl_I& x)' `unsigned int cl_I_to_uint (const cl_I& x)' `long cl_I_to_long (const cl_I& x)' `unsigned long cl_I_to_ulong (const cl_I& x)' Returns `x' as element of the C type CTYPE. If `x' is not representable in the range of CTYPE, a runtime error occurs. \end{quote} So my question is: how do I check if the variable of cl_I type can be converted to int (or any other built-in integer type[s])? Best regards, Alexei. -- All science is either physics or stamp collecting.
Sheplyakov Alexei writes:
So my question is: how do I check if the variable of cl_I type can be converted to int (or any other built-in integer type[s])?
My 2c: wrap the conversion in a try block, and catch the exception?. p. Dr. Pierangelo Masarati | voice: +39 02 2399 8309 Dip. Ing. Aerospaziale | fax: +39 02 2399 8334 Politecnico di Milano | mailto:pierangelo.masarati@polimi.it via La Masa 34, 20156 Milano, Italy | http://www.aero.polimi.it/~masarati Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html
Hello, On Thu, Oct 19, 2006 at 06:10:15PM +0200, Pierangelo Masarati wrote:
So my question is: how do I check if the variable of cl_I type can be converted to int (or any other built-in integer type[s])?
My 2c: wrap the conversion in a try block, and catch the exception?.
Have you tried it yourself? CLN does not use exceptions... Best regards, Alexei -- All science is either physics or stamp collecting.
Sheplyakov Alexei writes:
Have you tried it yourself? CLN does not use exceptions...
No, sorry. I'd guess any C++ software would use exceptions. I don't directly use CLN, I use GiNaC which is based on CLN and, as far as I know, GiNaC uses exceptions. But, I admit, this is by no means a good excuse for not checking... p. Dr. Pierangelo Masarati | voice: +39 02 2399 8309 Dip. Ing. Aerospaziale | fax: +39 02 2399 8334 Politecnico di Milano | mailto:pierangelo.masarati@polimi.it via La Masa 34, 20156 Milano, Italy | http://www.aero.polimi.it/~masarati Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html
Hello, Sheplyakov Alexei wrote:
how do I check if the variable of cl_I type can be converted to int (or any other built-in integer type[s])?
Did you try explicit bound checking? Something like this: #include <limits.h> static cl_I INT_MAX_I = (cl_I)(long)INT_MAX; static cl_I INT_MIN_I = (cl_I)(long)INT_MIN; static cl_I UINT_MAX_I = (cl_I)(unsigned long)UINT_MAX; static bool fits_in_int (const cl_I& x) { return x >= INT_MIN_I && x <= INT_MAX_I; } static bool fits_in_uint (const cl_I& x) { return !minusp(x) && x <= UINT_MAX_I; } The purpose of the casts to 'long' and 'unsigned long' above is explained in the manual, section "Conversions". Bruno
participants (3)
-
Bruno Haible
-
Pierangelo Masarati
-
varg@theor.jinr.ru