Overview
Maximum and minimum values of integral types such as int, short, long have been defined in <climits>.
Similarly, Maximum and minimum values for float, double are defined in <cfloat>. However these are defined as macros. For example, INT_MAX, INT_MIN, FLT_MIN,FLT_MAX etc.
Details
numeric_limits is a template class that defines functions such as min(), max() along with host of other functions that are related to arithmetic types.
Specialized classes from numeric_limits are defined for fundamental arithmetic types such as int, double. These methods in these classes can be used instead of of macros.
For example, numeric_limits<int>::max() can be used in place INT_MAX. These classes can be queried for minimum, maximum values and other properties such as number of digits, signed etc.
The specialized classes are defined as below where xxx is a data types listed below.
template<> class numeric_limits<xxx>;
Each specialized class will have is_specialized value set to true otherwise it's false.
Integral types
bool | char | signed char |
unsigned char | wchar_t | char16_t |
char32_t | short int | unsigned short int |
int | unsigned int | long int |
unsigned long int | long long int | unsigned long long int |
Floating point types
float | double | long double |
The following lists structure of the template class
template <class T> class numeric_limits { public:/* Properties *///true for all the types specialized from numeric_limits class. static constexpr bool is_specialized = false; //true if the type is signed. static constexpr bool is_signed = false; //true if the type is an integer. static constexpr bool is_integer = false; //true if the type uses exact representations. static constexpr bool is_exact = false; //true if the type has a representation for positive infinity. static constexpr bool has_infinity = false; //true if the type has a representation for a quiet (non-signaling) "Not-a-Number". static constexpr bool has_quiet_NaN = false; //true if the type has a representation for a signaling "Not-a-Number". static constexpr bool has_signaling_NaN = false; /* Denormalized values (representations with a variable number of exponent bits). A type may have any of the following enum values: 1. denorm_absent, if it does not allow denormalized values. 2. denorm_present, if it allows denormalized values. 3. denorm_indeterminate, if indeterminate at compile time. */ static constexpr float_denorm_style has_denorm = denorm_absent; //true if a loss of accuracy is detected as a denormalization loss, rather than an inexact result. static constexpr bool has_denorm_loss = false; /* Rounding style. A type may have any of the following enum values: 1. round_toward_zero, if it rounds toward zero. 2. round_to_nearest, if it rounds to the nearest representable value. 3. round_toward_infinity, if it rounds toward infinity. 4. round_toward_neg_infinity, if it rounds toward negative infinity. 5. round_indeterminate, if the rounding style is indeterminable at compile time. */ static constexpr float_round_style round_style = round_toward_zero; /* true if the type adheres to IEC-559 / IEEE-754 standard. An IEC-559 type always has has_infinity, has_quiet_NaN and has_signaling_NaN set to true; And infinity, quiet_NaN and signaling_NaN return some non-zero value. */ static constexpr bool is_iec559 = false; //true if the set of values represented by the type is finite. static constexpr bool is_bounded = false; /* true if the type is modulo. A type is modulo if it is possible to add two positive numbers and have a result that wraps around to a third number that is less. */ static constexpr bool is_modulo = false; /* For integer types: number of non-sign bits (radix base digits) in the representation. For floating types: number of digits (in radix base) in the mantissa. For example, same as FLT_MANT_DIG for float. */ static constexpr int digits = 0; /* Number of digits (in decimal base) that can be represented without change. For example, same as FLT_DIG for float. */ static constexpr int digits10 = 0; //Number of digits (in decimal base) required to ensure that values that differ are always differentiated. static constexpr int max_digits10 = 0; /* For integer types: base of the representation. For floating types: base of the exponent of the representation (equivalent to FLT_RADIX). */ static constexpr int radix = 0; /* Minimum negative integer value such that radix raised to (min_exponent-1) generates a normalized floating-point number. Equivalent to FLT_MIN_EXP, DBL_MIN_EXP or LDBL_MIN_EXP for floating types. */ static constexpr int min_exponent = 0; /* Minimum negative integer value such that 10 raised to that power generates a normalized floating-point number. Equivalent to FLT_MIN_10_EXP, DBL_MIN_10_EXP or LDBL_MIN_10_EXP for floating types. */ static constexpr int min_exponent10 = 0; /* Maximum integer value such that radix raised to (max_exponent-1) generates a representable finite floating-point number. Equivalent to FLT_MAX_EXP, DBL_MAX_EXP or LDBL_MAX_EXP for floating types. */ static constexpr int max_exponent = 0; /* Maximum integer value such that 10 raised to that power generates a normalized finite floating-point number. Equivalent to FLT_MAX_10_EXP, DBL_MAX_10_EXP or LDBL_MAX_10_EXP for floating types. */ static constexpr int max_exponent10 = 0; //true if trapping is implemented for the type during arithmetic operations. static constexpr bool traps = false; //true if tinyness is detected before rounding. static constexpr bool tinyness_before = false;
/* Methods *///Minimum finite value based on the type. For example, same as INT_MIN for int. static constexpr T min() noexcept { return T(); } /* Minimum finite value. For integral types: the same as min(). For floating-point types: implementation-dependent; generally, the negative of max(). */ static constexpr T lowest() noexcept { return T(); } //Maximum finite value based on the type. For example, same as INT_MAX for int. static constexpr T max() noexcept { return T(); } /* Machine epsilon (the difference between 1 and the least value greater than 1 that is representable). Equivalent to FLT_EPSILON, DBL_EPSILON or LDBL_EPSILON for floating types. */ static constexpr T epsilon() noexcept { return T(); } //Rqeturns the maximum rounding error of the given floating-point type static constexpr T round_error() noexcept { return T(); } //Returns the positive infinity value of the given floating-point type static constexpr T infinity() noexcept { return T(); } //Returns a quiet NaN value of the given floating-point type static constexpr T quiet_NaN() noexcept { return T(); } //Returns a signaling NaN value of the given floating-point type static constexpr T signaling_NaN() noexcept { return T(); } /* Returns the smallest positive subnormal value of the given floating-point type For types not allowing denormalized values: same as min(). */ static constexpr T denorm_min() noexcept { return T(); } }
Example
/* prints Minimum value for int: -2147483648 Maximum value for int: 2147483647 int is signed: true Non-sign bits in int: 31 int has infinity: false */ cout << boolalpha; cout << "Minimum value for int: " << numeric_limits<int>::min() << endl; cout << "Maximum value for int: " << numeric_limits<int>::max() << endl; cout << "int is signed: " << numeric_limits<int>::is_signed << endl; cout << "Non-sign bits in int: " << numeric_limits<int>::digits << endl; cout << "int has infinity: " << numeric_limits<int>::has_infinity << endl;This is expected in this example.
Included in Header file: <limits>
No comments:
Post a Comment