Wrong values for limits.h constants

The constant values displayed at the limits.h page (https://www.cplusplus.com/reference/climits/) are wrong for the S*_MIN macros.
So, for example:

1
2
#define SCHAR_MIN (-128) // Not (-127)
#define SHRT_MIN (-32768) // Not (-32767) 


and so on.
For example, 0b10000000 in 2-complement notation represents the minimum negative value on a 8-bit value, which is -128. While -127 is represented as 0b10000001.
You could even use printf() to check this.
A fix in the mentioned page is highly recommended.
Last edited on
SCHAR_MIN    -127 (-27+1) or less*

* the actual value depends on the particular system and library implementation, but shall reflect the limits of these types in the target platform.


Did you actually READ this?
Last edited on
lastchance is correct (*at least, for pre-C++20), but just to give an example: Another possible representation is "sign and magnitude", where a minimum of 7 bits would be for the magnitude, and 1 bit would be for the sign. So under this representation, the range could in fact be [-127, 127]. I have never used a computer with this as the implementation, and I imagine it's more of a historic thing.

Although... with C++20, I'm pretty sure signed integer types now explicitly follow 2's complement.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r4.html
They did a survey:
In short, the only machine the author could find using non-two’s complement are made by Unisys, and no counter-example was brought by any member of the C++ standards committee. Nowadays Unisys emulates their old architecture using x86 CPUs with attached FPGAs for customers who have legacy applications which they’ve been unable to migrate. These applications are unlikely to be well served by modern C++, signed integers are the least of their problem. Post-modern C++ should focus on serving its existing users well, and incoming users should be blissfully unaware of integer esoterica.


*So, yes, in combination with the fact that CHAR_BIT must be >= 8, and that integers must use 2's complement as of C++20, I believe this, together, means that SCHAR_MIN is -128 or less, and not -127 or less. I couldn't find a source to directly state this as a fact, but it can extrapolated based on the two requirements.

But this was only a recent guarantee, and the site itself is not updated for C++17 or beyond. To see the latest documentation, I suggest cppreference.com
Last edited on
Topic archived. No new replies allowed.