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

To get the smallest value, I have to use numeric_limits<int>::min()

I assume the smallest int is -2147483648, and tests on my machine showed this result. But some C ++ links, such as Open Group Base Specifications and cplusplus.com define it with a value of -2147483647.

I ask this question because in my implementation of the Framework NegaMax (search for the game tree) the minimum integer * (-1) must be defined correctly. Yes, with a minimum value of int = (numeric_limits :: min () + 2) In any case, I am safe, so my question is more theoretical, but I think it's still quite interesting.

+4
source share
5 answers

If the value is represented as a sign and a value instead of two additions, the sign bit is equal to one with all other bits as zero, equal to -0. In sign and magnitude, the maximum positive integer and negative integer are the same magnitude. Two additions may represent another negative value, since it does not have the same symmetry.

+4
source

The value of numeric_limits<int>::min() is determined by the implementation. That is why it could be otherwise. You should not adhere to any specific minimum value.

+3
source

At cplusplus.com you forgot to read the qualifier

min value *

  • This is not necessarily the actual value of a constant in any particular compiler or system; it can be equal to or larger in magnitude than this.
+1
source

From the cplusplus.com link you posted (highlighted by me):

The next panel shows the different constants and their guaranteed minimum values (positive numbers may be larger in value, and negative numbers may be less significant). Any particular compiler implementation can define integral types with larger values ​​than those shown here.

Numerical restrictions are always determined by the system and the compiler, try starting with a 64-bit compiler and the system, you can see completely different numbers.

0
source

C ++ uses two compliments for signed integers. Thus, the smallest signed integer is determined to be 100..00 (usually 32 bits).

A simple move 1<<(sizeof(int)*8-1) should give you the smallest signed integer.

Obviously, for unsigned integers, the smallest value is 0.

edit: you can read here
edit2: apparently C ++ doesn't necessarily use a compliment of two sides, my mistake

0
source

Source: https://habr.com/ru/post/1302081/


All Articles