STL:Numberic_limit()
(1):與MFC的兼容性
MFC程序過程中使用STL一些類編譯出錯,開始我認為是我寫錯了,放到Console Application里一切正常。
比如:
void CMyDialog::OnBnClickedButton1()
{
? ? double min=std::numeric_limits<double>::max();
? ? void *p=:perator new(count);
}
若在Console中根本沒問題。但在MFC中numeric_limits錯誤提示:
Error 2 error C2589: '(' : illegal token on right side of '::'?
operator new錯誤提示:
Error 1 error C2665: 'operator new' : none of the 5 overloads could convert all the argument types?
解決方案:
跟Windows中定義的宏想混淆了
可以用括號改變語句的順序,強制轉換為stl中的函數名
double min=(std::numeric_limits<double>::max)();
(2): 小例展示numeric_limits的基本用法:
[cpp]?view plaincopy
我機器上的運行結果:
[c-sharp]?view plaincopy
關于為什么float的最小值竟然是正的?我也存在疑問,從結果中,我們看出,min返回的是float型別可以表示的最小的正值,
而不是最小的float數。從這個例子中,我們差不多了解到numeric_limits的基本用法。
?
3. 基本成員函數
我以float類型來展示:
[c-sharp]?view plaincopy
運行結果:
[cpp]?view plaincopy
(3):關于STL:Numberic_limit()
原文鏈接:http://www.cplusplus.com/reference/limits/numeric_limits/
class template <limits>std::numeric_limits
template <class T> numeric_limits; Numeric limits type Provides information about the properties of? arithmetic types?(either integral or floating-point) in the specific platform for which the library compiles.This class template is specialized for every? fundamental arithmetic type, with its members describing the properties of type? T. This template shall not be specialized for any other type.
Template parameters
TIf this is a?fundamental arithmetic type, the members of the class describe its properties.
Template instantiations
- C++98
- C++11
| integral types | bool |
| char | |
| wchar_t | |
| signed char | |
| short int | |
| int | |
| long int | |
| unsigned char | |
| unsigned short int | |
| unsigned int | |
| unsigned long int | |
| floating point types | float |
| double | |
| long double |
Members that produce a value of type? T?are member functions, while members of specific types are static member constants:
Members
| is_specialized | bool | true?for all?arithmetic types?(i.e., those for which?numeric_limits?is specialized). false?for all other types. |
| 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,?LLONG_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,?LLONG_MAX,?ULLONG_MAX,?UINT_LEAST16_MAX,?UINT_LEAST32_MAX,?FLT_MAX,DBL_MAX?or?LDBL_MAX, depending on type. |
| lowest() | T | Minimum finite value. (since C++11) For integral types: the same as?min(). For floating-point types: implementation-dependent; generally, the negative ofmax(). |
| 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 toFLT_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 toFLT_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 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. |
| 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 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. |
| 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 | bool | 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. |
下面是參數的解釋
| digits10 | 返回目標類型在十進制下可以表示的最大位數 |
| epsilon | 返回目標數據類型能表示的最逼近1的正數和1的差的絕對值 |
| has_denorm | 測試目標類型是不是可以非規范化表示示 |
| has_denorm_loss | 測試所有類型是不是能測出因為非規范化而造成的精度損失(不是因為結果本身的不精確) |
| has_infinity | 測試目標類型是不是能表示無限(比如被0除,或者其他一些情況) |
| has_quiet_NaN | 檢查目標類型是不是支持安靜類型的NaN |
| has_signaling_NaN | 檢查目標類型是不是支持信號類型的NaN |
| infinity | 檢查目標類型的無限類型(如果支持無限表示) |
| is_bounded | 檢查目標類型的取值是否有限 |
| is_exact | 測試目標類型的計算結果是不是不會造成舍入誤差(比如float是0) |
| is_iec559 | 測試目標類型是不是符合IEC559標準 |
| is_integer | 測試目標類型是不是可以用整型來表示(比如char是1,float是0) |
| is_modulo | Tests if a type has a modulo representation. |
| is_signed | 測試目標類型是否是帶符號的 |
| is_specialized | 測試目標類型是不是在numeric_limits?.模板類中有特殊定義 |
| max | 返回可取的有限最大值 |
| max_exponent | Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power. |
| max_exponent10 | Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power. |
| min | 返回可取的最小值(規范化) |
| min_exponent | Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power. |
| min_exponent10 | Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power. |
| quiet_NaN | 返回目標類型的安靜NAN的表示 |
| radix | Returns the integral base, referred to as radix, used for the representation of a type. |
| round_error | 返回目標類型的最大可能的舍入誤差 |
| round_style | Returns a value that describes the various methods that an implementation can choose for rounding a floating-point value to an integer value. |
| signaling_NaN | 返回目標類型關于信號NAN的表示 |
| tinyness_before | 測試目標類型是不是能測定出微小的舍入誤差 |
| traps | Tests whether trapping that reports on arithmetic exceptions is implemented for a type. |
For all types that are not? fundamental arithmetic types, the default template definition is used:
- C++98
- C++11
| 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 | template <class T> class numeric_limits { public:static const bool is_specialized = false;static T min() throw();static T max() throw();static const int digits = 0;static const int digits10 = 0;static const bool is_signed = false;static const bool is_integer = false;static const bool is_exact = false;static const int radix = 0;static T epsilon() throw();static T round_error() throw();static const int min_exponent = 0;static const int min_exponent10 = 0;static const int max_exponent = 0;static const int max_exponent10 = 0;static const bool has_infinity = false;static const bool has_quiet_NaN = false;static const bool has_signaling_NaN = false;static const float_denorm_style has_denorm = denorm_absent;static const bool has_denorm_loss = false;static T infinity() throw();static T quiet_NaN() throw();static T signaling_NaN() throw();static T denorm_min() throw();static const bool is_iec559 = false;static const bool is_bounded = false;static const bool is_modulo = false;static const bool traps = false;static const bool tinyness_before = false;static const float_round_style round_style = round_toward_zero; }; |
Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | // numeric_limits example #include <iostream> // std::cout #include <limits> // std::numeric_limitsint main () {std::cout << std::boolalpha;std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';return 0; } |
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 |
總結
以上是生活随笔為你收集整理的STL:Numberic_limit()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 寓言故事100篇阅读
- 下一篇: 中班教案《马术表演》