Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the real lower limit for (signed) long long?

Tags:

c++

g++

Checking the limits of the type long long using

std::cout << std::numeric_limits<long long>::min();

I get -9223372036854775808

However when compiling the following code:

int main() {
   long long l;
   l=-9223372036854775808LL;
}

I get the warnings:

test.cpp:3:7: warning: integer constant is so large that it is unsigned.
test.cpp:3: warning: this decimal constant is unsigned only in ISO C90

What am I missing? Many thanks in advance for your help.

Giorgio

like image 265
Giorgio A Avatar asked May 21 '11 12:05

Giorgio A


2 Answers

This 9223372036854775808LL is a positive number. So you need to take

std::cout << std::numeric_limits<long long>::max();

into consideration. Negating it immediately afterwards doesn't make the operand of the - operator itself negative.

like image 123
Johannes Schaub - litb Avatar answered Oct 13 '22 12:10

Johannes Schaub - litb


It works fine with std::numeric and boost::numeric; it gives no warnings.

#include <iostream>
#include <boost/numeric/conversion/bounds.hpp>
#include <boost/limits.hpp>

int main(int argc, char* argv[]) {

  std::cout << "The minimum value for long long:\n";
  std::cout << boost::numeric::bounds<long long>::lowest() << std::endl;
  std::cout << std::numeric_limits<long long>::min() << std::endl << std::endl;

  std::cout << "The maximum value for long long:\n";
  std::cout << boost::numeric::bounds<long long>::highest() << std::endl;
  std::cout << std::numeric_limits<long long>::max() << std::endl << std::endl;

  std::cout << "The smallest positive value for long long:\n";
  std::cout << boost::numeric::bounds<long long>::smallest() << std::endl << std::endl;


  long long l;
  l = boost::numeric::bounds<long long>::lowest();
  std::cout << l << std::endl;
  l = std::numeric_limits<long long>::min();
  std::cout << l << std::endl;


  return 0;
}
like image 28
Arlen Avatar answered Oct 13 '22 14:10

Arlen