std::numeric_limits<T>::digits
From cppreference.com
static const int digits;
|
(until C++11) | |
static constexpr int digits;
|
(since C++11) | |
The value of std::numeric_limits<T>::digits is the number of digits in base-radix that can be represented by the type T without change. For integer types, this is the number of bits not counting the sign bit and the padding bits (if any). For floating-point types, this is the digits of the mantissa (for IEC 559/IEEE 754 implementations, this is the number of digits stored for the mantissa plus one, because the mantissa has an implicit leading 1 and binary point).
Standard specializations
T
|
value of std::numeric_limits<T>::digits(assuming no padding bits) |
/* non-specialized */
|
0
|
bool
|
1
|
char
|
CHAR_BIT - std::numeric_limits<char>::is_signed
|
signed char
|
CHAR_BIT - 1
|
unsigned char
|
CHAR_BIT
|
wchar_t
|
CHAR_BIT * sizeof(wchar_t)
- std::numeric_limits<wchar_t>::is_signed
|
char8_t (since C++20)
|
CHAR_BIT
|
char16_t (since C++11)
|
CHAR_BIT * sizeof(char16_t)
|
char32_t (since C++11)
|
CHAR_BIT * sizeof(char32_t)
|
short
|
CHAR_BIT * sizeof(short) - 1
|
unsigned short
|
CHAR_BIT * sizeof(short)
|
int
|
CHAR_BIT * sizeof(int) - 1
|
unsigned int
|
CHAR_BIT * sizeof(int)
|
long
|
CHAR_BIT * sizeof(long) - 1
|
unsigned long
|
CHAR_BIT * sizeof(long)
|
long long (since C++11)
|
CHAR_BIT * sizeof(long long) - 1
|
unsigned long long (since C++11)
|
CHAR_BIT * sizeof(long long)
|
float
|
FLT_MANT_DIG
|
double
|
DBL_MANT_DIG
|
long double
|
LDBL_MANT_DIG
|
Example
Run this code
#include <concepts>
#include <iomanip>
#include <iostream>
#include <limits>
#include <meta>
#include <string_view>
#include <type_traits>
using namespace std::literals;
template<typename T>
requires std::integral<T> or std::floating_point<T>
constexpr void digit()
{
constexpr int w{(int)"unsigned long long"sv.size()};
std::cout << std::right << std::setw(w)
<< std::meta::display_string_of(^^T) << " : "
<< std::numeric_limits<T>::digits << '\n';
}
template<typename... T>
constexpr void digits()
{
(digit<T>(), ...);
}
int main()
{
digits<
bool, char, signed char, unsigned char, wchar_t, char8_t, char16_t,
char32_t, short, unsigned short, int, unsigned int, long, unsigned long,
long long, unsigned long long, float, double, long double
>();
}
Possible output:
bool : 1
char : 7
signed char : 7
unsigned char : 8
wchar_t : 31
char8_t : 8
char16_t : 16
char32_t : 32
short : 15
unsigned short : 16
int : 31
unsigned int : 32
long : 63
unsigned long : 64
long long : 63
unsigned long long : 64
float : 24
double : 53
long double : 64
See also
[static] |
the radix or integer base used by the representation of the given type (public static member constant) |
[static] |
one more than the smallest negative power of the radix that is a valid normalized floating-point value (public static member constant) |
[static] |
one more than the largest integer power of the radix that is a valid finite floating-point value (public static member constant) |