Connecting Tech Pros Worldwide Forums | Help | Site Map

computing limit of basic data types ?

sandSpiderX
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi,

I have to develop a program to compute range of all data types at run
time...

meaning say char c;

now I have to see how many characters char can represent,,

one logic i think is to start incrementing till the loop goes back to 0
(for unsigned) and/or -128 (for signed)....

However this cycling is unifrom across all platforms or not...

What other way can this be done...without using header defined limit
constants....

Help me..with this..

sandSpiderX


Suman
Guest
 
Posts: n/a
#2: Jul 23 '05

re: computing limit of basic data types ?




sandSpiderX wrote:[color=blue]
> Hi,
>
> I have to develop a program to compute range of all data types at run
> time...
>[/color]
Homework, eh? Give us a better description, please.[color=blue]
> meaning say char c;
>
> now I have to see how many characters char can represent,,
>
> one logic i think is to start incrementing till the loop goes back to 0
> (for unsigned) and/or -128 (for signed)....[/color]

Question: How do you know the limit is -128 or not?
sizeof(char) == 1 byte == CHAR_BITS bits (which is not necessarily = 8
bits)
[color=blue]
>
> However this cycling is unifrom across all platforms or not...
>
> What other way can this be done...without using header defined limit
> constants....
> Help me..with this..
>[/color]
And an overdose of ellipsis does not reflect your thinking prowess.
You could have done without them.

sandSpiderX
Guest
 
Posts: n/a
#3: Jul 23 '05

re: computing limit of basic data types ?


Hi Suman,

No homework....just working on some application part....
Its like the application is going to run on mutiple platforms and is
going to produce full range of charactersets and range for other data
types....

for this I am trying to compute data type range at runtime....

Am I doing wrong ....
Whats the best way....

The module is going to be compiled and run on Solaris,HP/AUX,Intel, Mac
and the like....

sandSpiderX

sandSpiderX
Guest
 
Posts: n/a
#4: Jul 23 '05

re: computing limit of basic data types ?


Suman ::

Just forgot...its not overdose of ellipsis,

Its the best way to give the reader some time for breaking up and
analysing more carefully what she is reading...

sandSpiderX

Suman
Guest
 
Posts: n/a
#5: Jul 23 '05

re: computing limit of basic data types ?




sandSpiderX wrote:[color=blue]
> Hi Suman,
>
> No homework....just working on some application part....
> Its like the application is going to run on mutiple platforms and is
> going to produce full range of charactersets and range for other data
> types....[/color]
What is this full range? Your application will give you a range for
a particular data type which will in general vary from system to
system.
Are you trying to standardize this range for a given data type for all
the
systems? Normally, this would mean you have to take the intersection of
the set
of ranges provided by each system your application is going to run on,
for
a particular data type.

For example, are you trying to give your application the ability to
treat integers as 32 bit quantities across all platforms?
[color=blue]
> for this I am trying to compute data type range at runtime....
>[/color]

Are you sure you mean data type?[color=blue]
> Am I doing wrong ....
> Whats the best way....
>
> The module is going to be compiled and run on Solaris,HP/AUX,Intel, Mac
> and the like....
>
> sandSpiderX[/color]

Suman
Guest
 
Posts: n/a
#6: Jul 23 '05

re: computing limit of basic data types ?




sandSpiderX wrote:[color=blue]
> Suman ::
>
> Just forgot...its not overdose of ellipsis,
>
> Its the best way to give the reader some time for breaking up and
> analysing more carefully what she is reading...[/color]
Are you sure only women will read your posts ;) ? I, for one, am not.

sandSpiderX
Guest
 
Posts: n/a
#7: Jul 23 '05

re: computing limit of basic data types ?


Suman...

I apologise for the she part...

Well, I say int x on platform XYZ,

can you write a program and tell me what the range of x is ?

sand.Spider.X

Prateek
Guest
 
Posts: n/a
#8: Jul 23 '05

re: computing limit of basic data types ?


Use the numeric_limits class template from the header <limits>. For
example, the maximum value a char can hold is
numeric_limits<char>::max(). numeric_limits has not only ranges, but
also other useful information about numeric types, specially floating
point types.

Prateek

-- --
To iterate is human, to recurse divine.
-L. Peter Deutsch
-- --

verec
Guest
 
Posts: n/a
#9: Jul 23 '05

re: computing limit of basic data types ?


On 2005-06-25 13:15:22 +0100, "sandSpiderX" <m74.piscean@gmail.com> said:
[color=blue]
> Well, I say int x on platform XYZ,
>
> can you write a program and tell me what the range of x is ?[/color]

Well, if _that_ is the problem ...

int
howManyBitsInAnInt() {
int i = 1 ;
int count = 0 ;
while(i) {
++count ;
i <<= 1 ;
}
return count ;
}

doing the same for a float ... is left as a very interesting exercise :)
--
JFB

verec
Guest
 
Posts: n/a
#10: Jul 23 '05

re: computing limit of basic data types ?


On 2005-06-25 17:26:36 +0100, verec <verec@mac.com> said:
[color=blue]
> On 2005-06-25 13:15:22 +0100, "sandSpiderX" <m74.piscean@gmail.com> said:
>[color=green]
>> Well, I say int x on platform XYZ,
>>
>> can you write a program and tell me what the range of x is ?[/color]
>
> Well, if _that_ is the problem ...
>
> int
> howManyBitsInAnInt() {
> int i = 1 ;
> int count = 0 ;
> while(i) {
> ++count ;
> i <<= 1 ;
> }
> return count ;
> }
>
> doing the same for a float ... is left as a very interesting exercise :)[/color]

Alternatively:

int
postiveRangeOfInt() {
unsigned int i = (unsigned int) -1 ;
return i >>= 1 ;
}

--
JFB

verec
Guest
 
Posts: n/a
#11: Jul 23 '05

re: computing limit of basic data types ?


On 2005-06-25 17:33:53 +0100, verec <verec@mac.com> said:
[color=blue]
> Alternatively:
>
> int
> postiveRangeOfInt() {
> unsigned int i = (unsigned int) -1 ;
> return i >>= 1 ;
> }[/color]

An even shorter form:

int positiveRangeOfInt() {
return ~0 >> 1 ;
}

Clark S. Cox III
Guest
 
Posts: n/a
#12: Jul 23 '05

re: computing limit of basic data types ?


On 2005-06-25 20:59:20 -0400, verec <verec@mac.com> said:
[color=blue]
> On 2005-06-25 17:33:53 +0100, verec <verec@mac.com> said:
>[color=green]
>> Alternatively:
>>
>> int
>> postiveRangeOfInt() {
>> unsigned int i = (unsigned int) -1 ;
>> return i >>= 1 ;
>> }[/color]
>
> An even shorter form:
>
> int positiveRangeOfInt() {
> return ~0 >> 1 ;
> }[/color]

I think you mean:

int positiveRangeOfInt() {
return ~0u >> 1;
}


--
Clark S. Cox, III
clarkcox3@gmail.com

verec
Guest
 
Posts: n/a
#13: Jul 23 '05

re: computing limit of basic data types ?


On 2005-06-26 15:41:22 +0100, Clark S. Cox III <clarkcox3@gmail.com> said:
[color=blue][color=green]
>> An even shorter form:
>>
>> int positiveRangeOfInt() {
>> return ~0 >> 1 ;
>> }[/color]
>
> I think you mean:
>
> int positiveRangeOfInt() {
> return ~0u >> 1;
> }[/color]

Well spotted!
--
JFB

Suman
Guest
 
Posts: n/a
#14: Jul 23 '05

re: computing limit of basic data types ?




verec wrote:[color=blue]
> On 2005-06-25 13:15:22 +0100, "sandSpiderX" <m74.piscean@gmail.com> said:
>[color=green]
> > Well, I say int x on platform XYZ,
> >
> > can you write a program and tell me what the range of x is ?[/color]
>
> Well, if _that_ is the problem ...
>
> int
> howManyBitsInAnInt() {
> int i = 1 ;
> int count = 0 ;
> while(i) {
> ++count ;
> i <<= 1 ;
> }
> return count ;
> }
>
> doing the same for a float ... is left as a very interesting exercise :)[/color]

AFAIK, bitwise mumbo-jumbo on floating point data is not allowed.
What I _really_ dont understand is, if the problem is _that_ simple,
why can't
we do a (sizeof(type) * CHAR_BITS ) to get the number of bits?

Old Wolf
Guest
 
Posts: n/a
#15: Jul 23 '05

re: computing limit of basic data types ?


sandSpiderX wrote:[color=blue]
>
> Well, I say int x on platform XYZ,
> can you write a program and tell me what the range of x is ?
>[/color]

#include <iostream>
#include <climits>

int main()
{
std::cout << "Range of int is " << INT_MIN
<< " to " << INT_MAX << std::endl;
return 0;
}

Suman
Guest
 
Posts: n/a
#16: Jul 23 '05

re: computing limit of basic data types ?




Old Wolf wrote:[color=blue]
> sandSpiderX wrote:[color=green]
> >
> > Well, I say int x on platform XYZ,
> > can you write a program and tell me what the range of x is ?
> >[/color]
>
> #include <iostream>
> #include <climits>
>
> int main()
> {
> std::cout << "Range of int is " << INT_MIN
> << " to " << INT_MAX << std::endl;
> return 0;
> }[/color]
This is what the OP had posted originally:
"What other way can this be done...without using header defined limit
constants.... ", so much for the trouble :)

Steven T. Hatton
Guest
 
Posts: n/a
#17: Jul 23 '05

re: computing limit of basic data types ?


Old Wolf wrote:
[color=blue]
> sandSpiderX wrote:[color=green]
>>
>> Well, I say int x on platform XYZ,
>> can you write a program and tell me what the range of x is ?
>>[/color]
>
> #include <iostream>
> #include <climits>
>
> int main()
> {
> std::cout << "Range of int is " << INT_MIN
> << " to " << INT_MAX << std::endl;
> return 0;
> }[/color]

Or the C++ way:

http://www.josuttis.com/
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
{
// use textual representation for bool
cout << boolalpha;

// print maximum of integral types
cout << "max(short): " << numeric_limits<short>::max() << endl;
cout << "max(int): " << numeric_limits<int>::max() << endl;
cout << "max(long): " << numeric_limits<long>::max() << endl;
cout << endl;

// print maximum of floating-point types
cout << "max(float): "
<< numeric_limits<float>::max() << endl;
cout << "max(double): "
<< numeric_limits<double>::max() << endl;
cout << "max(long double): "
<< numeric_limits<long double>::max() << endl;
cout << endl;

// print whether char is signed
cout << "is_signed(char): "
<< numeric_limits<char>::is_signed << endl;
cout << endl;

// print whether numeric limits for type string exist
cout << "is_specialized(string): "
<< numeric_limits<string>::is_specialized << endl;
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
sandSpiderX
Guest
 
Posts: n/a
#18: Jul 23 '05

re: computing limit of basic data types ?


Hey all,,

I stil feel too deep inside the problem,
the soultions presented are elegant but not professional.

Infact most usage of header file global constants makes me feel sick
with C++.
Are we just bound by header constants.
What about using runtime code usage to generate the range.

Here is better worded problem,
I give you an integer,
write a program,
to generate range of integer <signed and unsigned> on any platform on
which the program is run.

Consider cases like int is 4 bytes on Visual C++ 6.0 Windows and 2
bytes on DOS windows.

So all computation needs to be done at run time.
What I thought was like once the range of a particular data type is
crossed it cycles back to its minimum range and then starts again,
like for signed char we have -128 to +127, so when we say 127+1, its
-128...

Something like the above needs to work out...

Hey all , lets clear this problem...

sandSpider|x|

Steven T. Hatton
Guest
 
Posts: n/a
#19: Jul 23 '05

re: computing limit of basic data types ?


sandSpiderX wrote:
[color=blue]
> Hey all,,
>
> I stil feel too deep inside the problem,
> the soultions presented are elegant but not professional.
>
> Infact most usage of header file global constants makes me feel sick
> with C++.
> Are we just bound by header constants.
> What about using runtime code usage to generate the range.
>
> Here is better worded problem,
> I give you an integer,
> write a program,
> to generate range of integer <signed and unsigned> on any platform on
> which the program is run.
>
> Consider cases like int is 4 bytes on Visual C++ 6.0 Windows and 2
> bytes on DOS windows.
>
> So all computation needs to be done at run time.
> What I thought was like once the range of a particular data type is
> crossed it cycles back to its minimum range and then starts again,
> like for signed char we have -128 to +127, so when we say 127+1, its
> -128...
>
> Something like the above needs to work out...
>
> Hey all , lets clear this problem...
>
> sandSpider|x|[/color]

Are you sure this is not your homework? I suggest you take a closer look at
the code I presented, and the template I used. I did not use any global
constants, and I showed you the preferred method of finding the kind of
information you are asking for.

Try http://www.dinkumware.com/manuals/reader.aspx?lib=cpp you don't need to
pay to use the site, but they will put up pages suggesting you buy their
reference. You have to click through these pages to read the document.
And, of course, consider buying it.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Steven T. Hatton
Guest
 
Posts: n/a
#20: Jul 23 '05

re: computing limit of basic data types ?


Steven T. Hatton wrote:
[color=blue][color=green]
>> So all computation needs to be done at run time.
>> What I thought was like once the range of a particular data type is
>> crossed it cycles back to its minimum range and then starts again,
>> like for signed char we have -128 to +127, so when we say 127+1, its
>> -128...
>>
>> Something like the above needs to work out...[/color][/color]


I just realized, perhaps you are asking a math question not a computer
programming question? Be aware that the characteristics of data types are
determined by the C++ implementation. They may be influenced by the
hardware, but do not need to be. Consult your textbook, or language
reference to find out how to determine the information upon which to base
your calculation.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Lionel B
Guest
 
Posts: n/a
#21: Jul 23 '05

re: computing limit of basic data types ?


sandSpiderX wrote:[color=blue]
> Hey all,,
>
> I stil feel too deep inside the problem,
> the soultions presented are elegant but not professional.
>
> Infact most usage of header file global constants makes me feel sick
> with C++.
> Are we just bound by header constants.[/color]

What exactly is the problem? The constants defined in <limits> are standard, portable and undoubtedly far more reliable
than any hack you are likely to come up with.
[color=blue]
> What about using runtime code usage to generate the range.[/color]

Now *that's* what I'd call unprofessional. Personally I would sack on the spot a programmer who insisted on implementing
their own routines for calculating these constants ;-)

So by all means go ahead, but be warned that there may be many artchitecture/OS/compiler-dependent gotchas along that
route. There is a danger of making all kinds of unwarranted assumptions about storage/representation of types. Luckily,
however, the designers of the language and compiler writers have most thoughtfully solved all those problems for you and
placed the results in a standard header called <limits> so there is really no need to roll your own.
[color=blue]
> /.../[/color]

--
Lionel B

Closed Thread