38

Is there a C++ cross-platform library that provides me with a portable maximum integer number?

I want to declare:

const int MAX_NUM = /* call some library here */;

I use MSVC 2008 unmanaged.

1
  • 4
    you don't need a lib, you need to learn C++; what you are asking for is standard c++ (std::numeric_limits)
    – KeatsPeeks
    Nov 13, 2009 at 21:30

5 Answers 5

96

In the C++ standard library header <limits>, you will find:

std::numeric_limits<int>::max()

Which will tell you the maximum value that can be stored in a variable of type int. numeric_limits is a class template, and you can pass it any of the numeric types to get the maximum value that they can hold.

The numeric_limits class template has a lot of other information about numeric types as well.

5
  • 1
    Actually it's called a "function template", not the other way around. But otheriwse that's very good advice. +1
    – sbi
    Nov 13, 2009 at 21:47
  • 1
    @sbi: Thank you for the correction. The C++ standard does indeed call them "function templates" (14.8.1), but also uses the phrase "nontemplate function," which makes me think that the phrase "template function" might also be acceptable; even highly reputable C++ references (parashift.com/c++-faq-lite/templates.html#faq-35.13) use the phrase "template function." In any case, I've updated the answer with the unambiguously correct terminology. :-) Nov 13, 2009 at 21:57
  • 1
    Comeau has a faq entry on that: comeaucomputing.com/techtalk/templates/#terms Nov 13, 2009 at 22:03
  • 1
    @gf: Thanks; I haven't seen that FAQ before (I've not used Comeau), but there is a lot of interesting stuff there. I have had to correct my answer again, though, since there aren't any template functions or function templates in it. numeric_limits is a class template. Nov 13, 2009 at 22:28
  • In a similar vein, talking about word-order, this "portably provides a maximum integer", it doesn't "provide a portable maximum integer". There does not exist an integer which is a portable maximum integer, for precisely the reason numeric_limits exists, which is that it's different on different implementations ;-) Nov 14, 2009 at 3:54
10

See limits.h (C) or climits (C++). In this case you would want the INT_MAX constant.

6

I know it's an old question but maybe someone can use this solution:

int size = 0; // Fill all bits with zero (0)
size = ~size; // Negate all bits, thus all bits are set to one (1)

So far we have -1 as result 'till size is a signed int.

size = (unsigned int)size >> 1; // Shift the bits of size one position to the right.

As Standard says, bits that are shifted in are 1 if variable is signed and negative and 0 if variable would be unsigned or signed and positive.

As size is signed and negativ we would shift in sign bit which is 1, which is not helping much, so we cast to unsigned int, forcing to shift in 0 instead, setting the sign bit to 0 while letting all other bits remain 1.

cout << size << endl; // Prints out size which is now set to maximum positive value.

We could also use a mask and xor but then we had to know the exact bitsize of the variable. With shifting in bits front, we don't have to know at any time how many bits the int has on machine or compiler nor need we include extra libraries.

3

I know the answer has been given but I just want to know from my old days, I used to do

int max = (unsigned int)-1

Will it give the same as

std::numeric_limits<int>::max()

?

6
  • 1
    This trick only works for unsigned max values. Nov 13, 2009 at 22:01
  • It was a question about the question, why the -2!? Nov 13, 2009 at 22:18
  • 6
    For future reference, StackOverflow works best as a 1 Question -> Many Answer type of thing. If you have a question (and the one you bring up seems reasonable), feel free to post a brand new question. Here, it looks like you're trying to offer advice that others perseive as incorrect.
    – Tim Frey
    Nov 13, 2009 at 22:25
  • 1
    To expand on what Andrey says: no, it won't give the same. (int)(unsigned int)-1 is -1 (well, actually it's implementation-defined, but it's usually -1), whereas std::numeric_limits<int>::max() is a large positive int value. However, (unsigned int)-1 is equal to std::numeric_limits<unsigned int>::max(), always. Nov 14, 2009 at 4:00
  • @SteveJessop Only true on a two's compliment platform. C/C++ makes no guarantee of ones/twos compliment.
    – Tyzoid
    Oct 3, 2015 at 1:43
1

On Hp UX with aCC compiler:

#include <iostream>
#include <limits>
using namespace std;

int main () {
  if (sizeof(int)==sizeof(long)){
    cout<<"sizeof int == sizeof long"<<endl;
  } else {
    cout<<"sizeof int != sizeof long"<<endl;
  }

  if (numeric_limits<int>::max()==numeric_limits<long>::max()){
    cout<<"INT_MAX == lONG_MAX"<<endl;
  } else {
    cout<<"INT_MAX != LONG_MAX"<<endl;
  }

  cout << "Maximum value for int: " << numeric_limits<int>::max() << endl;
  cout << "Maximum value for long: " << numeric_limits<long>::max() << endl;
  return 0;
}

It prints:

sizeof int == sizeof long

INT_MAX != LONG_MAX

I checked both int and long types are 4bytes. manpage limits(5) says that INT_MAX and LONG_MAX are both 2147483647

http://nixdoc.net/man-pages/HP-UX/man5/limits.5.html

So, conclusion std::numeric_limits< type >:: is not portable.

3
  • I would conclude that there's a bug, either in the implementation or in your program. I don't see a problem with your program, but it would be helpful if you'd copy-and-paste the exact output, including the last two lines. Apr 17, 2014 at 16:16
  • It's a bug marc.info/?l=apache-stdcxx-issues&m=120683278410901
    – yet
    Apr 17, 2014 at 17:34
  • The link points to a discussion about floating-point limits, not integer limits. Are you sure it's relevant? Apr 17, 2014 at 17:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.