Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why numeric_limits<int>::min() is differently defined?

Tags:

c++

stl

To retrieve the smallest value i have to use numeric_limits<int>::min()

I suppose the smallest int is -2147483648, and tests on my machine showed this result. But some C++ references like Open Group Base Specifications and cplusplus.com define it with the value -2147483647.

I ask this question because in my implementation of the negaMax Framework (Game Tree Search) the value minimal integer * (-1) has to be well defined. Yes, with minimal int = (numeric_limits::min() + 2) i am on the safe side in any case, thus my question is more theoretically but i think nevertheless quite interesting.

like image 601
Christian Ammer Avatar asked Feb 28 '23 12:02

Christian Ammer


1 Answers

If a value is represented as sign-and-magnitude instead of two's complement, the sign bit being one with all other bits as zero is equivalent to -0. In sign-and-magnitude the maximum positive integer and negative integer are the same magnitude. Two's complement is able to represent one more negative value because it doesn't have the same symmetry.

like image 113
user261840 Avatar answered Mar 12 '23 01:03

user261840