From:http://www.cplusplus.com/reference/std/limits/numeric_limits/
numeric_limits
浮点数据精度判断
float x=0.0;
float y=0.0f;
if ( abs(x -y) <= numeric_limits<float>::epsilon() )
{
...
}
<limits>
Numeric limits type
This class is specialized for each of the fundamental types, with its members returning or set to the different values that define the properties that type has in the specific platform in which it compiles.
For all the other types (non-fundamental types) a specialization of this class should not exist.
The non-specialized class is defined as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
A specialization exists for each of the fundamental types: bool, char, signed char, unsigned char, wchar_t, short, unsigned short, int, unsigned int, long, unsigned long, float, double and long double. These specializations define the specific values for the different static const members, and all have is_specialized defined as true.
Members
member | type | property |
---|---|---|
is_specialized | bool | true on all specializations of the type, false in the non-specialized version. |
min() | T | Minimum finite value. For floating types with denormalization (variable number of exponent bits): minimum positive normalized value. Equivalent to CHAR_MIN, SCHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, FLT_MIN, DBL_MIN, LDBL_MIN or 0, depending on type. |
max() | T | Maximum finite value. Equivalent to CHAR_MAX, SCHAR_MAX, UCHAR_MAX, SHRT_MAX, USHRT_MAX, INT_MAX, UINT_MAX, LONG_MAX, ULONG_MAX, FLT_MAX, DBL_MAX or LDBL_MAX, depending on type. |
digits | int | 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 (equivalent to FLT_MANT_DIG, DBL_MANT_DIG or LDBL_MANT_DIG). |
digits10 | int | Number of digits (in decimal base) that can be represented without change. Equivalent to FLT_DIG, DBL_DIG or LDBL_DIG for floating types. |
is_signed | bool | true if type is signed. |
is_integer | bool | true if type is integer. |
is_exact | bool | true if type uses exact representations. |
radix | int | For integer types: base of the representation. For floating types: base of the exponent of the representation (equivalent to FLT_RADIX). |
epsilon() | 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. |
round_error() | T | Measure of the maximum rounding error. |
min_exponent | int | Minimum negative integer value for the exponent that generates a normalized floating-point number. Equivalent to FLT_MIN_EXP, DBL_MIN_EXP or LDBL_MIN_EXP for floating types. |
min_exponent10 | int | 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. |
max_exponent | int | Maximum integer value for the exponent that generates a normalized floating-point number. Equivalent to FLT_MAX_EXP, DBL_MAX_EXP or LDBL_MAX_EXP for floating types. |
max_exponent10 | int | 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. |
has_infinity | bool | true if the type has a representation for positive infinity. |
has_quiet_NaN | bool | true if the type has a representation for a quiet (non-signaling) "Not-a-Number". |
has_signaling_NaN | bool | true if the type has a representation for a signaling "Not-a-Number". |
has_denorm | float_denorm_style | Denormalized values (representations with a variable number of exponent bits). A type may have any of the following enum values: denorm_absent, if it does not allow denormalized values. denorm_present, if it allows denormalized values. denorm_indeterminate, if indeterminate at compile time. |
has_denorm_loss | bool | true if a loss of accuracy is detected as a denormalization loss, rather than an inexact result. |
infinity() | T | Representation of positive infinity, if available. |
quiet_NaN() | T | Representation of quiet (non-signaling) "Not-a-Number", if available. |
signaling_NaN() | T | Representation of signaling "Not-a-Number", if available. |
denorm_min() | T | Minimum positive denormalized value. For types not allowing denormalized values: same as min(). |
is_iec559() | T | 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. |
is_bounded | bool | true if the set of values represented by the type is finite. |
is_modulo | bool | 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. |
traps | bool | true if trapping is implemented for the type. |
tinyness_before | bool | true if tinyness is detected before rounding. |
round_style | float_round_style | Rounding style. A type may have any of the following enum values: round_toward_zero, if it rounds toward zero. round_to_nearest, if it rounds to the nearest representable value. round_toward_infinity, if it rounds toward infinity. round_toward_neg_infinity, if it rounds toward negative infinity. round_indeterminate, if the rounding style is indeterminable at compile time. |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Possible output:
Minimum value for int: -2147483648 Maximum value for int: 2147483647 int is signed: true Non-sign bits in int: 31 int has infinity: false |
See also
climits (limits.h) | Sizes of integral types (header) |
cfloat (float.h) | Characteristics of floating-point types (header) |
Test:
#include <iostream> #include <cstdlib> #include <limits> #include <string> using namespace std; int main() { //判断各类型有无极值 cout << boolalpha; cout << "specialized(char): " << numeric_limits<char>::is_specialized << endl; cout << "specialized(wchar_t): " << numeric_limits<wchar_t>::is_specialized << endl; cout << "specialized(string): " << numeric_limits<string>::is_specialized << endl; cout << noboolalpha << endl; //各个类型的最大值 cout << "max(int): " << numeric_limits<int>::max() << endl; cout << "max(short): " << numeric_limits<short>::max() << endl; cout << "max(unsigned int): " << numeric_limits<unsigned int>::max() << endl; cout << "max(unsigned short): " << numeric_limits<unsigned short>::max() << endl; cout << "max(unsigned long): " << numeric_limits<unsigned long>::max() << endl; cout << "max(long): " << numeric_limits<long>::max() << endl; cout << "max(long long): " << numeric_limits<long long>::max() << endl; cout << "max(float): " << numeric_limits<float>::max() << endl; cout << "max(double): " << numeric_limits<double>::max() << endl; cout << "max(long double): " << numeric_limits<long double>::max() << endl; cout << endl; //各个类型的最小值 cout << "min(int): " << numeric_limits<int>::min() << endl; cout << "min(short): " << numeric_limits<short>::min() << endl; cout << "min(unsigned int): " << numeric_limits<unsigned int>::min() << endl; cout << "min(unsigned short): " << numeric_limits<unsigned short>::min() << endl; cout << "min(unsigned long): " << numeric_limits<unsigned long>::min() << endl; cout << "min(long): " << numeric_limits<long>::min() << endl; cout << "min(long long): " << numeric_limits<long long>::min() << endl; cout << "min(float): " << numeric_limits<float>::min() << endl; cout << "min(double): " << numeric_limits<double>::min() << endl; cout << "min(long double): " << numeric_limits<long double>::min() << endl; system("pause"); return 0; } |