[CLN-list] integer conversion functions weirdness

Bruno Haible bruno at clisp.org
Thu Oct 19 20:03:31 CEST 2006


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


More information about the CLN-list mailing list