std::numeric_limits::is_modulo

From Cppreference

Jump to: navigation, search
static const bool is_modulo
(pre-C++11 version)
static constexpr bool is_modulo
(C++11 version)

The value of std::numeric_limits<T>::is_modulo is true for all arithmetic types T that handle overflows with modulo arithmetic, that is, if the result of addition, subtraction, multiplication, or division of this type would fall outside the range [min(), max()], the value returned by such operation differs from the expected value by a multiple of max()-min()+1.

[edit] Standard specializations

T value of std::numeric_limits<T>::is_modulo
/* non-specialized */ false
bool false
char true
signed char true
unsigned char true
wchar_t true
char16_t true
char32_t true
short true
unsigned short true
int true
unsigned int true
long true
unsigned long true
long long true
unsigned long long true
float false
double false
long double false

[edit] Example

Demonstrates the behavior of modulo types

#include <iostream>
#include <type_traits>
#include <limits>
 
template<typename T>
typename std::enable_if<std::numeric_limits<T>::is_modulo>::type
check_overflow()
{
    std::cout << "\nmax value is " << std::numeric_limits<T>::max() << '\n'
              << "min value is " << std::numeric_limits<T>::min() << '\n'
              << "max value + 1 is " << std::numeric_limits<T>::max()+1 << '\n';
}
 
int main()
{
    check_overflow<int>();
    check_overflow<unsigned long>();
    // check_overflow<float>(); // compile-time error, not a modulo type
}

Output:

max value is 2147483647
min value is -2147483648
max value + 1 is -2147483648
 
max value is 18446744073709551615
min value is 0
max value + 1 is 0

[edit] See also

is_integer
identifies integer types
(public static member constant)
is_iec559
identifies the IEC 559/IEEE 754 floating-point types
(public static member constant)
is_exact
identifies exact types
(public static member constant)