473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Maximum value for typedef'd type

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 );
}

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 13 '05 #1
8 4662
Fred L. Kleinschmidt wrote:
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 );
}

Yes, there are gotchas including, if I am not mistaken, undefined
behaviour in some cases, as evidenced by the thread entitled "Many happy
returns" that has been running this week.

--
Bertrand Mollinier Toublet
int main(){char*strchr();int j=1234;char t[]=":@abcdefghij-lmnopqrstuv"
"wxyz.\n",*i="iqgbgxmbbla.llsvoaz:zdxylaxejivnidhd @ttopnjeftuh-i";while
(*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);}return 0;}

Nov 13 '05 #2
Fred L. Kleinschmidt <fr*****************@boeing.com> scribbled the following:
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.


This is the entirely wrong syntax for typedef. # marks a preprocessor
directive, while typedef is a feature of the actual C language.
Presumably you mean:
typedef short Newtype;
typedef unsigned short Newtype;
and so on.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
Nov 13 '05 #3
In message <3F***************@boeing.com>
"Fred L. Kleinschmidt" <fr*****************@boeing.com> wrote:
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.

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 */
To reliably fill it with 1's, that has to be "~(Newtype) 0".
/*
* 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 );
}


That assumes sign-and-magnitude representation for signed integers, as far as
I can see. That's not common, but it may match your platform. For 2's
complement you end up returning 1, and for 1's complement you either return 0
or invoke undefined behaviour.

A better start would be

static Newtype x = -1;

That will set x to -1 if it is signed, and to NEWTYPE_MAX if it is unsigned,
on any system. Thus we can next say

if (x > 0) return x;

Signed types are much harder to probe without invoking undefined behaviour or
relying on outside knowledge. Let's try though. C guarantees one of 3
representations for signed integers - sign-and-magnitude, 2's complement or
1's complement.

I think you can safely determine which representation is in use safely by
saying

x = ~(Newtype) 1;

x now has all its value bits set except the "1" bit, and the sign bit is set.
What exactly that means depends on the representation.

if (x == -1)
/* representation is 1's complement */;
else if (x == -2)
/* representation is 2's complement */;
else
/* representation is sign-and-magnitude */;

Note that this test (indeed probably the whole function) can be evaluated at
compile time.

Sign-and-magnitude is straightforward. For that one we just

return (-x) + 1;

For the other two, we've got a problem. We need to set all the value bits,
and clear the sign bit, but how to achieve this without invoking undefined
behaviour? Integer overflow is undefined, left-shift of positive values
outside the range is undefined, and whether you can have the sign bit set and
all value bits clear is implementation-defined.

I can't see any way of doing it without making some sort of assumption. The
safest may be to assume that left shift overflow throws away the excess bits
or moves them through the sign bit, and do something like:

x = 0xFF; // avoids trap representations at end of shift
while ((x << 1) > x)
x <<= 1;
return x | (x >> 8);

If there is a nice, portable answer to this, I can't see it :(

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #4
In <3F***************@boeing.com> "Fred L. Kleinschmidt" <fr*****************@boeing.com> writes:
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.
You may want to learn C before worrying about such issues. typedef is
not a preprocessor directive and it has a completely different syntax than
the #define directive.
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)
This is definitely not a valid function definition.
{
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 );
}


This is all wrong. If Newtype is signed, the most likely result is 1.

The trick is to use the widest unsigned type (unsigned long in C89) for
this purpose:

unsigned long max = -1;
while ((Newtype)max < 0 || (Newtype)max != max) max >>= 1;
return max;

The code exploits the fact that the maximum value is one less than a
power of two. It is theoretically unsafe on C99 implementations, where
(Newtype)max is allowed to raise an implementation-defined signal if
the value of max cannot be represented by Newtype.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5


Joona I Palaste wrote:

Fred L. Kleinschmidt <fr*****************@boeing.com> scribbled the following:
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.
This is the entirely wrong syntax for typedef. # marks a preprocessor
directive, while typedef is a feature of the actual C language.
Presumably you mean:
typedef short Newtype;
typedef unsigned short Newtype;
and so on.


Yeah, I did a cut/paste from a print that uses "#" as a linestart
indicator, and forgot to remove it to show the real source text. --
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates


--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 13 '05 #6


Kevin Bracey wrote:

In message <3F***************@boeing.com>
"Fred L. Kleinschmidt" <fr*****************@boeing.com> wrote:
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.

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 */


To reliably fill it with 1's, that has to be "~(Newtype) 0".
/*
* 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 );
}


That assumes sign-and-magnitude representation for signed integers, as far as
I can see. That's not common, but it may match your platform. For 2's
complement you end up returning 1, and for 1's complement you either return 0
or invoke undefined behaviour.

A better start would be

static Newtype x = -1;

That will set x to -1 if it is signed, and to NEWTYPE_MAX if it is unsigned,
on any system. Thus we can next say

if (x > 0) return x;

Signed types are much harder to probe without invoking undefined behaviour or
relying on outside knowledge. Let's try though. C guarantees one of 3
representations for signed integers - sign-and-magnitude, 2's complement or
1's complement.

I think you can safely determine which representation is in use safely by
saying

x = ~(Newtype) 1;

x now has all its value bits set except the "1" bit, and the sign bit is set.
What exactly that means depends on the representation.

if (x == -1)
/* representation is 1's complement */;
else if (x == -2)
/* representation is 2's complement */;
else
/* representation is sign-and-magnitude */;

Note that this test (indeed probably the whole function) can be evaluated at
compile time.

Sign-and-magnitude is straightforward. For that one we just

return (-x) + 1;

For the other two, we've got a problem. We need to set all the value bits,
and clear the sign bit, but how to achieve this without invoking undefined
behaviour? Integer overflow is undefined, left-shift of positive values
outside the range is undefined, and whether you can have the sign bit set and
all value bits clear is implementation-defined.

I can't see any way of doing it without making some sort of assumption. The
safest may be to assume that left shift overflow throws away the excess bits
or moves them through the sign bit, and do something like:

x = 0xFF; // avoids trap representations at end of shift
while ((x << 1) > x)
x <<= 1;
return x | (x >> 8);

If there is a nice, portable answer to this, I can't see it :(

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/


I know that NewType must be one of the six
(unsigned/signed)(short/int/long).
Thus here is what I am now trying:

Newtype GetMaximum Newtype(void)
{
static Newtype x = -1; /* negative if signed, MAXVAL if unsigned */
static int len = sizeof(Newtype);

if ( x > 0 ) {
return x; /* Newtype is an unsigned type */
}
/* Newtype is a signed type */
if (len == sizeof(short)) {
return (Newtype)USHRT_MAX;
}
else if (len == sizeof(long)) {
return (Newtype)ULONG_MAX;
}
return (Newtype)INT_MAX; /* as a default, if not short or long */
}
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 13 '05 #7
Fred L. Kleinschmidt wrote:
....
I know that NewType must be one of the six
(unsigned/signed)(short/int/long).
Thus here is what I am now trying:

Newtype GetMaximum Newtype(void)
{
static Newtype x = -1; /* negative if signed, MAXVAL if unsigned */
static int len = sizeof(Newtype);

if ( x > 0 ) {
return x; /* Newtype is an unsigned type */
}
/* Newtype is a signed type */
OK, so far.
if (len == sizeof(short)) {
return (Newtype)USHRT_MAX;


USHRT_MAX? For a signed variable?

Furthermore, the size of a type is not related to the range representable
by this type. Have a look at Dan Pop's reply for a possible solution.

Jirka

Nov 13 '05 #8
Fred L. Kleinschmidt <fr*****************@boeing.com> scribbled the following:
Joona I Palaste wrote:
Fred L. Kleinschmidt <fr*****************@boeing.com> scribbled the following:
> 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.
This is the entirely wrong syntax for typedef. # marks a preprocessor
directive, while typedef is a feature of the actual C language.
Presumably you mean:
typedef short Newtype;
typedef unsigned short Newtype;
and so on.

Yeah, I did a cut/paste from a print that uses "#" as a linestart
indicator, and forgot to remove it to show the real source text.


Looks like the print uses really strange syntax then, because the
syntax of "typedef" is that the new type (Newtype in this case) must
appear *after* the actual type, not before it.
In other words: the print reads:
typedef Newtype short;
when it should read:
typedef short Newtype;

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Till Crueger | last post by:
Hi, I have to implement some simple sorting algorithm. I am NOT asking for you to do my homework, but my question is rather on how to store the integers. I recall reading once in here that there...
2
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
5
by: homsan toft | last post by:
Hi, I'm (still) trying to return a pair<const Key, T> from iterator dereference. So I defined a proxy class in the obvious way: template<class KeyT, class DataT> struct ref_proxy { typedef...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
3
by: Madmartigan | last post by:
Hello I have the following task but am battling with the final output. How do I keep two different vectors in sync and how would I retrieve the index for the maximum value of one of the vectors??...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
6
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm using the VScrollBar and set it as follow: m_vScrollBar.Minimum = -19602; m_vScrollBar.Maximum = 0; m_vScrollBar.SmallChange = 1; m_vScrollBar.LargeChange = 1089; m_vScrollBar.Value =...
1
by: maurizio | last post by:
thank you for your answer actually i've to do some statistics (maximum,minimum,mean,standard deviation,....) of a file of data in which each column is a particular type of data. (the file is a tab...
7
by: laredotornado | last post by:
Hi, I'm using PHP 5 with MySql 5. I have a MySQL InnoDB table with a column of type INTEGER UNSIGNED. Is there a constant in PHP to insert the maximum value possible into the column? The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.