Solved

Maximum/minimum values of int, double, long, etc....

Posted on 2003-10-31
12
338 Views
Last Modified: 2010-04-01
I am often writing optimization routines that either start or are toggled off of extremum values of various datatypes. Are there any globally-defined max/min values for the various types (int, long...). For example, coming from Java, there exist things like Double.MAX_VALUE that start you off at the upper limit of double.

I realize of course I could look through a book, remember how many bits are in a long, and do the binary arithmetic, but the Java approach or something like it seems preferable.
0
Comment
Question by:antoniogm
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 168 total points
ID: 9659283
yes... it's in <limits.h>

{CHAR_BIT}
Number of bits in a type char.
[CX]  Value: 8  
{CHAR_MAX}
Maximum value of type char.
Value: {UCHAR_MAX} or {SCHAR_MAX}
{CHAR_MIN}
Minimum value of type char.
Value: {SCHAR_MIN} or 0
{INT_MAX}
Maximum value of an int.
Minimum Acceptable Value: 2 147 483 647
{LONG_BIT}
[XSI]
Number of bits in a long.
Minimum Acceptable Value: 32  
{LONG_MAX}
Maximum value of a long.
Minimum Acceptable Value: +2 147 483 647
{MB_LEN_MAX}
Maximum number of bytes in a character, for any supported locale.
Minimum Acceptable Value: 1
{SCHAR_MAX}
Maximum value of type signed char.
[CX]  Value: +127  
{SHRT_MAX}
Maximum value of type short.
Minimum Acceptable Value: +32 767
{SSIZE_MAX}
Maximum value of an object of type ssize_t.
Minimum Acceptable Value: {_POSIX_SSIZE_MAX}
{UCHAR_MAX}
Maximum value of type unsigned char.
[CX]  Value: 255  
{UINT_MAX}
Maximum value of type unsigned.
Minimum Acceptable Value: 4 294 967 295
{ULONG_MAX}
Maximum value of type unsigned long.
Minimum Acceptable Value: 4 294 967 295
{USHRT_MAX}
Maximum value for a type unsigned short.
Minimum Acceptable Value: 65 535
{WORD_BIT}
[XSI]
Number of bits in a word or type int.
Minimum Acceptable Value: 16  
{INT_MIN}
Minimum value of type int.
Maximum Acceptable Value: -2 147 483 647
{LONG_MIN}
Minimum value of type long.
Maximum Acceptable Value: -2 147 483 647
{SCHAR_MIN}
Minimum value of type signed char.
[CX]  Value: -128  
{SHRT_MIN}
Minimum value of type short.
Maximum Acceptable Value: -32 767
{LLONG_MIN}
Minimum value of type long long.
Maximum Acceptable Value: -9223372036854775807
{LLONG_MAX}
Maximum value of type long long.
Minimum Acceptable Value: +9223372036854775807
{ULLONG_MAX}
Maximum value of type unsigned long long.
Minimum Acceptable Value: 18446744073709551615

for more: http://www.opengroup.org/onlinepubs/007904975/basedefs/limits.h.html
0
 

Author Comment

by:antoniogm
ID: 9659650
Thanks for the response.

However...
There seems to be no limit on doubles however. Wha'ts up with that?
0
 

Author Comment

by:antoniogm
ID: 9659793
Thanks for the response.

However...
There seems to be no limit on doubles however. Wha'ts up with that?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9659858
> There seems to be no limit on doubles however. Wha'ts up with that?

There is no limit on floats and doubles.  The IEEE 754 float and double encodings
can represent numbers in the range +/- Infinity (with limited precision).  
For more info, see the following:

http://www.psc.edu/general/software/packages/ieee/ieee.html

0
 

Author Comment

by:antoniogm
ID: 9660440
So that raises the corollary question: how do you express infinity?

Or is there no way to do so? Do doubles just wrap around when incremented beyond their max?
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9661127
If you had followed the hyperlink I gave, it shows exactly how
infinity is expressed.  You will also notice that it doesn't "wrap" as
you expect.

0
 

Author Comment

by:antoniogm
ID: 9661197
Idid follow the link, and it gives precisely the information that my question stated I did not want to deal with. Knowing how infinity is represented bitwise in some IEEE document does precious little good when coding in C++. In any case, I'll take the answer as 'no', there is no way to compactly represent the maximal value of a double in C.
0
 
LVL 15

Assisted Solution

by:efn
efn earned 166 total points
ID: 9661227
limits.h is inherited from C.  The corresponding header file for floating-point numbers is float.h.  C++ adds a different header file <limits>.  The numeric_limits template class in that header file gives you a lot of information about various types.  For example, the smallest float value would be numeric_limits<float>::min().

For details, see:

http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=limits2.html

The IEEE standard for floating point numbers does have special values for positive infinity, negative infinity, and "not a number," but the number of different values it can represent is certainly finite.

--efn
0
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 166 total points
ID: 9662220
> Knowing how infinity is represented bitwise in some IEEE document
> does precious little good when coding in C++. In any case, I'll take
> the answer as 'no', there is no way to compactly represent the
> maximal value of a double in C.

Actually, it has everything to do with coding in C++, since that is
how C/C++ represents floating point numbers.  I'm still not sure
what you are looking for.  I think what you want is in math.h.

To set a float to infinity:
      float f = INFINITY;

To test if float is infinity:
      if (isinf(f)) ...

To test if float is finite:
      if (isfinite(f)) ...

To test if float is NaN (Not a Number)
      if (isnan(f)) ...

To set a float to NaN:
      float f = NAN;
0
 
LVL 15

Expert Comment

by:efn
ID: 9662306
To answer the original question more specifically than I did before, yes, there are functions similar to the Java functions.  There is a function is_bounded in the numeric_limits template class that returns a bool that tells you whether the type has a bounded set of representable values.  It returns true for all predefined types.  If numeric_limits<x>::is_bounded() is true for a type x, then numeric_limits<x>::max() returns the largest finite value the type can store, and numeric_limits<x>::min() returns the lowest finite value for the type.  There is also another function is_signed(), and min() also works if both is_bounded() and is_signed() are false.

--efn
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546363
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: bobbit31 {http:#9659283} & efn {http:#9661227} & brettmjohnson {http:#9662220}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 62
How to fix the Ubuntu Linux 15.10 Files GUI manager , Nautilus , flickers on and off? 9 104
Least Squares Curve Fitting 4 99
Finding Divisors 5 17
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question