C++ numerical limits
Date: 2023-02-20Last modified: 2024-11-02
Table of contents
Weird integral types
☢
Warning
Integral types less than 4 bytes in size don’t support arithmetic operations.
-
char
: 1 byte -
short int
: 2 bytes
short int var1{10}; // 2 bytes
short int var2{20};
char var3{40}; // 1 byte
char var4{50};
size_t size1{10};
ssize_t size2{-1};
auto result1 = var1 + var2; // 🔴 Implicit conversion to int
auto result2 = var3 + var4; // 🔴 Implicit conversion to int
auto result3 = var1 >> 1;
auto result4 = var3 << 1;
#define printSizeOf(var) \
fmt::print("sizeof({:^10}) = {} | value = {}\n", #var, sizeof(var), var);
printSizeOf(var1);
printSizeOf(var2);
printSizeOf(var3); // ascii 40 -> (
printSizeOf(var4); // ascii 50 -> 2
printSizeOf(result1);
printSizeOf(result2);
printSizeOf(result3);
printSizeOf(result4);
printSizeOf(size1);
printSizeOf(size2);
Type limits
// max: maximum positive value
// min: minimum positive value
// lowest: minimum negative value (if not integral)
fmt::print("\n\n{:^14}|{:2}|{:^27}|{:^27}|{:>27}\n", "TYPE", "SZ", "LOWEST",
"MIN", "MAX");
#define print_range(T) \
if (std::numeric_limits<T>::is_integer) { \
fmt::print("{:14}|{:2}|INT{:24}|{:27}|{:27}\n", #T, sizeof(T), \
std::numeric_limits<T>::lowest(), \
std::numeric_limits<T>::min(), std::numeric_limits<T>::max()); \
} else { \
fmt::print("{:14}|{:2}|{:27}|{:27}|{:27}\n", #T, sizeof(T), \
std::numeric_limits<T>::lowest(), \
std::numeric_limits<T>::min(), std::numeric_limits<T>::max()); \
}
print_range(short);
print_range(unsigned short);
print_range(int);
print_range(unsigned int);
print_range(long);
print_range(unsigned long);
print_range(float);
print_range(double);
print_range(long double);
print_range(size_t);
print_range(ssize_t);
fmt::print("\n\n{:14}|{:^20}|{:^10}\n", "TYPE", "PROPERTY", "VALUE");
#define print_prop(T, P) \
fmt::print("{:14}|{:^20}|{:^10}\n", #T, #P, std::numeric_limits<T>::P);
print_prop(int, is_signed);
print_prop(int, digits);
print_prop(int, radix);
print_prop(int, digits10);
// print_prop(int, round_style);
print_prop(double, is_signed);
print_prop(double, digits);
print_prop(double, radix);
print_prop(double, digits10);
// print_prop(double, round_style);
Possible output
sizeof( var1 ) = 2 | value = 10
sizeof( var2 ) = 2 | value = 20
sizeof( var3 ) = 1 | value = (
sizeof( var4 ) = 1 | value = 2
sizeof( result1 ) = 4 | value = 30
sizeof( result2 ) = 4 | value = 90
sizeof( result3 ) = 4 | value = 5
sizeof( result4 ) = 4 | value = 80
sizeof( size1 ) = 8 | value = 10
sizeof( size2 ) = 8 | value = -1
TYPE |SZ| LOWEST | MIN | MAX
short | 2|INT -32768| -32768| 32767
unsigned short| 2|INT 0| 0| 65535
int | 4|INT -2147483648| -2147483648| 2147483647
unsigned int | 4|INT 0| 0| 4294967295
long | 8|INT -9223372036854775808| -9223372036854775808| 9223372036854775807
unsigned long | 8|INT 0| 0| 18446744073709551615
float | 4| -3.4028235e+38| 1.1754944e-38| 3.4028235e+38
double | 8| -1.7976931348623157e+308| 2.2250738585072014e-308| 1.7976931348623157e+308
long double |16|-1.189731495357231765e+4932|3.3621031431120935063e-4932| 1.189731495357231765e+4932
size_t | 8|INT 0| 0| 18446744073709551615
ssize_t | 8|INT -9223372036854775808| -9223372036854775808| 9223372036854775807
TYPE | PROPERTY | VALUE
int | is_signed | true
int | digits | 31
int | radix | 2
int | digits10 | 9
double | is_signed | true
double | digits | 53
double | radix | 2
double | digits10 | 15