computing limit of basic data types ?

S

sandSpiderX

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
 
S

Suman

sandSpiderX said:
Hi,

I have to develop a program to compute range of all data types at run
time...
Homework, eh? Give us a better description, please.
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)....

Question: How do you know the limit is -128 or not?
sizeof(char) == 1 byte == CHAR_BITS bits (which is not necessarily = 8
bits)
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..
And an overdose of ellipsis does not reflect your thinking prowess.
You could have done without them.
 
S

sandSpiderX

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
 
S

sandSpiderX

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
 
S

Suman

sandSpiderX said:
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....
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?
for this I am trying to compute data type range at runtime....

Are you sure you mean data type?
 
S

Suman

sandSpiderX said:
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...
Are you sure only women will read your posts ;) ? I, for one, am not.
 
S

sandSpiderX

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
 
P

Prateek

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
-- --
 
V

verec

Well, I say int x on platform XYZ,

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

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 :)
 
V

verec

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 :)

Alternatively:

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

verec

Alternatively:

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

An even shorter form:

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

Suman

verec said:
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 :)

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?
 
O

Old Wolf

sandSpiderX said:
Well, I say int x on platform XYZ,
can you write a program and tell me what the range of x is ?

#include <iostream>
#include <climits>

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

Suman

Old said:
#include <iostream>
#include <climits>

int main()
{
std::cout << "Range of int is " << INT_MIN
<< " to " << INT_MAX << std::endl;
return 0;
}
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 :)
 
S

Steven T. Hatton

Old said:
#include <iostream>
#include <climits>

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

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

sandSpiderX

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|
 
S

Steven T. Hatton

sandSpiderX said:
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|

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.
 
S

Steven T. Hatton

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top