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.

sivabudh
  • 31,807
  • 63
  • 162
  • 228
  • 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 '09 at 21:30

5 Answers5

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 1
    Actually it's called a "function template", not the other way around. But otheriwse that's very good advice. +1 – sbi Nov 13 '09 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 (http://www.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. :-) – James McNellis Nov 13 '09 at 21:57
  • 1
    Comeau has a faq entry on that: http://www.comeaucomputing.com/techtalk/templates/#terms – Georg Fritzsche Nov 13 '09 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. – James McNellis Nov 13 '09 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 ;-) – Steve Jessop Nov 14 '09 at 03:54
10

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

Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126
7

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.

MJeB
  • 71
  • 1
  • 1
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()

?

  • 1
    This trick only works for unsigned max values. – AnT stands with Russia Nov 13 '09 at 22:01
  • It was a question about the question, why the -2!? – Mike Gleason jr Couturier Nov 13 '09 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 '09 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::max()` is a large positive `int` value. However, `(unsigned int)-1` is equal to `std::numeric_limits::max()`, always. – Steve Jessop Nov 14 '09 at 04:00
  • @SteveJessop Only true on a two's compliment platform. C/C++ makes no guarantee of ones/twos compliment. – Tyzoid Oct 03 '15 at 01:43
  • @Tyzoid: you're correct that C and C++ both allow either 1s' or 2's complement integer representation (or sign-magnitude). No part of what I said is defined by the standard to be true only on 2's complement implementations. The result of `(int)(unsigned int)-1` is implementation-defined (implementations usually choose `-1` since implementations usually are 2's complement so that's easy to implement, but they don't have to choose `-1` even if they are 2's complement), and `std::numeric_limits::max() == (unsigned int)-1` regardless of the signed integer representation. – Steve Jessop Oct 03 '15 at 10:32
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.

yet
  • 773
  • 11
  • 19
  • 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. – Keith Thompson Apr 17 '14 at 16:16
  • It's a bug http://marc.info/?l=apache-stdcxx-issues&m=120683278410901 – yet Apr 17 '14 at 17:34
  • The link points to a discussion about floating-point limits, not integer limits. Are you sure it's relevant? – Keith Thompson Apr 17 '14 at 17:48