Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

How to Determine the Kind of a Number From std::numeric_limits

Based on the information above, one can see that different kinds of numbers can be differentiated based on the information stored in std::numeric_limits. This is in addition to the traits class number_category provided by this library.

Integer Types

For an integer type T, all of the following conditions hold:

std::numeric_limits<T>::is_specialized == true
std::numeric_limits<T>::is_integer == true
std::numeric_limits<T>::is_exact == true
std::numeric_limits<T>::min_exponent == 0
std::numeric_limits<T>::max_exponent == 0
std::numeric_limits<T>::min_exponent10 == 0
std::numeric_limits<T>::max_exponent10 == 0

In addition the type is signed if:

std::numeric_limits<T>::is_signed == true

If the type is arbitrary precision then:

std::numeric_limits<T>::is_bounded == false

Otherwise the type is bounded, and returns a non zero value from:

std::numeric_limits<T>::max()

and has:

std::numeric_limits<T>::is_modulo == true

if the type implements modulo arithmetic on overflow.

Rational Types

Rational types are just like integers except that:

std::numeric_limits<T>::is_integer == false
Fixed Precision Types

There appears to be no way to tell these apart from rational types, unless they set:

std::numeric_limits<T>::is_exact == false

This is because these types are in essence a rational type with a fixed denominator.

floating-point Types

For a floating-point type T, all of the following conditions hold:

std::numeric_limits<T>::is_specialized == true
std::numeric_limits<T>::is_integer == false
std::numeric_limits<T>::is_exact == false
std::numeric_limits<T>::min_exponent != 0
std::numeric_limits<T>::max_exponent != 0
std::numeric_limits<T>::min_exponent10 != 0
std::numeric_limits<T>::max_exponent10 != 0

In addition the type is signed if:

std::numeric_limits<T>::is_signed == true

And the type may be decimal or binary depending on the value of:

std::numeric_limits<T>::radix

In general, there are no arbitrary precision floating-point types, and so:

std::numeric_limits<T>::is_bounded == false
Exact floating-point Types

Exact floating-point types are a field composed of an arbitrary precision integer scaled by an exponent. Such types have no division operator and are the same as floating-point types except:

std::numeric_limits<T>::is_exact == true
Complex Numbers

For historical reasons, complex numbers do not specialize std::numeric_limits, instead you must inspect std::numeric_limits<T::value_type>.


PrevUpHomeNext