1

I recently noticed numeric_limits::max() and numeric_limits::min() don't seem to work for uint8_t and int8_t. Is there a reason for this or could it be a bug? I tried on my own computer using gcc compiler:

#include <iostream>
#include <limits>

using namespace std;

int main()
{

    std::cout << "numeric_limits<uint8_t>::max() = " << numeric_limits<uint8_t>::max() << std::endl; 
    std::cout << "numeric_limits<int8_t>::max() = " << numeric_limits<int8_t>::max() << std::endl; 
    std::cout << "numeric_limits<int8_t>::min() = " << numeric_limits<int8_t>::min() << std::endl;

    std::cout << "numeric_limits<uint16_t>::max() = " << numeric_limits<uint16_t>::max() << std::endl; 
    std::cout << "numeric_limits<int16_t>::max() = " << numeric_limits<int16_t>::max() << std::endl; 
    std::cout << "numeric_limits<int16_t>::min() = " << numeric_limits<int16_t>::min() << std::endl; 

    std::cout << "numeric_limits<uint32_t>::max() = " << numeric_limits<uint32_t>::max() << std::endl; 
    std::cout << "numeric_limits<int32_t>::max() = " << numeric_limits<int32_t>::max() << std::endl; 
    std::cout << "numeric_limits<int32_t>::min() = " << numeric_limits<int32_t>::min() << std::endl; 

    std::cout << "numeric_limits<uint64_t>::max() = " << numeric_limits<uint64_t>::max() << std::endl; 
    std::cout << "numeric_limits<int64_t>::max() = " << numeric_limits<int64_t>::max() << std::endl; 
    std::cout << "numeric_limits<int64_t>::min() = " << numeric_limits<int64_t>::min() << std::endl; 

    return 0;
}

gives output:

numeric_limits<uint8_t>::max() = �
numeric_limits<int8_t>::max() = 
numeric_limits<int8_t>::min() = �
numeric_limits<uint16_t>::max() = 65535
numeric_limits<int16_t>::max() = 32767
numeric_limits<int16_t>::min() = -32768
numeric_limits<uint32_t>::max() = 4294967295
numeric_limits<int32_t>::max() = 2147483647
numeric_limits<int32_t>::min() = -2147483648
numeric_limits<uint64_t>::max() = 18446744073709551615
numeric_limits<int64_t>::max() = 9223372036854775807
numeric_limits<int64_t>::min() = -9223372036854775808
Paradox
  • 1,935
  • 1
  • 18
  • 31
  • On my system I have this `typedef unsigned char uint8_t;`. What happens when you print a unsigned char, .e.g `unsigned char c = 'a'; std::cout << c;`? – Thomas Sablik Jun 16 '20 at 09:32
  • @ThomasSablik I get the output "a%". From other comments it seems like it's printing a char instead of an int? That could answer my question, but I don't know why it has this behavior. – Paradox Jun 16 '20 at 09:35

3 Answers3

6

It does work. The output is interpreted as ASCII characters though. If you cast to int before you print, you will see the correct values:

std::cout << "numeric_limits<uint8_t>::max() = " << static_cast<int>(numeric_limits<uint8_t>::max()) << std::endl; 
std::cout << "numeric_limits<int8_t>::max() = " << static_cast<int>(numeric_limits<int8_t>::max()) << std::endl; 
std::cout << "numeric_limits<int8_t>::min() = " << static_cast<int>(numeric_limits<int8_t>::min()) << std::endl;
Mikael H
  • 1,323
  • 7
  • 12
  • That worked, thanks. I feel slightly mislead by the name uint8 being a char and not an integer though. I wonder why they did it this way. – Paradox Jun 16 '20 at 09:38
  • @Paradox A char is simply a 8 bit number. It's not a character. The output functions (cout/printf/...) interpret this number as character and print it like this. The name _"char"_ is a bit misleading. – Thomas Sablik Jun 16 '20 at 09:39
  • @Paradox re: `and not an integer` a char **is** an integer. Character streams just treat integer types that are character types in a special way. – eerorika Jun 16 '20 at 09:51
3
std::cout << "numeric_limits<uint8_t>::max() = " << std::to_string(numeric_limits<uint8_t>::max()) << std::endl; 
std::cout << "numeric_limits<int8_t>::max() = " << std::to_string(numeric_limits<int8_t>::max()) << std::endl; 
std::cout << "numeric_limits<int8_t>::min() = " << std::to_string(numeric_limits<int8_t>::min()) << std::endl;

try to convert them to string, before inserting them into cout.

crsn
  • 609
  • 4
  • 11
2

int8 types are probably defined as chars, so don't print the values as charbut as ints:

int main() {

    std::cout << "numeric_limits<uint8_t>::max() = " << (int)numeric_limits<uint8_t>::max() << std::endl; 
    std::cout << "numeric_limits<int8_t>::max() = " << (int)numeric_limits<int8_t>::max() << std::endl; 
    std::cout << "numeric_limits<int8_t>::min() = " << (int)numeric_limits<int8_t>::min() << std::endl;
}
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69