F
Fred L. Kleinschmidt
I need to know the largets value representable in a variable. However, I
do not know the variable's true type - only that it is some kind of int.
It may be any of the following:
#typedef Newtype short;
#typedef Newtype unsigned short;
#typedef Newtype int;
#typedef Newtype unsigned int;
#typedef Newtype long;
....etc.
The actual typedef used depends on the platform; this definition is made
in a third-party header over which I have no control.
Here is my code that appears to work on the platforms I am using. Have I
missed anything here? Any gotchas?
Newtype GetMaximum Newtype(void)
{
static Newtype x = ~0; /* Fill with 1's */
/*
* If x<0, then Newtype was a signed entity, so we need to negate
it.
* Note the compiler may complain if Newtype is unsigned.
*/
return ( (x < 0) ? -x : x );
}
do not know the variable's true type - only that it is some kind of int.
It may be any of the following:
#typedef Newtype short;
#typedef Newtype unsigned short;
#typedef Newtype int;
#typedef Newtype unsigned int;
#typedef Newtype long;
....etc.
The actual typedef used depends on the platform; this definition is made
in a third-party header over which I have no control.
Here is my code that appears to work on the platforms I am using. Have I
missed anything here? Any gotchas?
Newtype GetMaximum Newtype(void)
{
static Newtype x = ~0; /* Fill with 1's */
/*
* If x<0, then Newtype was a signed entity, so we need to negate
it.
* Note the compiler may complain if Newtype is unsigned.
*/
return ( (x < 0) ? -x : x );
}