numeric_limits 類別

類別範本描述內建數數值型別的算術屬性。

語法

template <class Type>
    class numeric_limits

參數

類型
正在測試、查詢或設定其屬性的基本項目資料類型。 類型 也可以宣告 const 為 、 volatileconst volatile

備註

標頭會定義類型 、、、 charunsigned intlongsigned charunsigned charintunsigned shortshortchar16_tlong doubleunsigned longunsigned long longfloatlong longdoublechar32_t 的明確特製化。 boolwchar_t 針對這些明確特製化,成員 numeric_limits::is_specializedtrue,而且所有相關成員都包含有意義的值。 這個程式可提供額外的明確特製化。 類別的大部分成員函式都在描述或測試 float的可能實作。

針對任意特製化,沒有任何成員包含有意義的值。 未包含有意義的值的成員物件會儲存零 (或 false),而未傳回有意義的值的成員函式會傳回 Type(0)

靜態函式和常數

名稱 描述
denorm_min 傳回未標準化的最小非零值。
digits 傳回基數數字的數目,其中該類型可以在不減少有效位數的情況下表示。
digits10 傳回十進位數字的數目,其中該類型可以在不減少有效位數的情況下表示。
epsilon 傳回資料類型可以表示之介於 1 到大於 1 的最小值之間的差異。
has_denorm 測試類型是否允許未標準化的值。
has_denorm_loss 測試偵測到的精確度遺失是否為未標準化遺失,而非不精確的結果。
has_infinity 測試類型是否有正無限大的表示。
has_quiet_NaN 測試類型是否具有無訊息的數位標記法,而不是數位(NAN),這是非訊號。
has_signaling_NaN 測試類型是否有不是數字 (NAN) 的訊號表示。
infinity 類型的正無限大表示 (如果有的話)。
is_bounded 測試類型可能代表的一組值是否為有限值。
is_exact 測試執行計算的類型是否沒有進位誤差。
is_iec559 測試類型是否符合 IEC 559 標準。
is_integer 測試類型是否有整數表示。
is_modulo 測試類型是否有模數表示。
is_signed 測試類型是否有正負號表示。
is_specialized 測試類型是否有在類別範本 numeric_limits 中定義的明確特製化。
lowest 傳回最大負數的有限值。
max 傳回類型的最大有限值。
max_digits10 傳回十進位數字的數目,需要有這個數值,才能確保類型的兩個不同值有不同的十進位表示法。
max_exponent 傳回當基數的基底自乘至該乘冪時,浮點類型可以有限值表示的最大正整數指數。
max_exponent10 傳回當 10 的基底自乘至該乘冪時,浮點類型可以有限值表示的最大正整數指數。
min 傳回類型的最小標準化數值。
min_exponent 傳回當基數的基底自乘至該乘冪時,浮點類型可以有限值表示的最大負整數指數。
min_exponent10 傳回當 10 的基底自乘至該乘冪時,浮點類型可以有限值表示的最大負整數指數。
quiet_NaN 傳回類型非數字 (NAN) 的無訊息表示。
基數 傳回用來表示類型的整數基底 (稱為基數)。
round_error 傳回類型的最大進位誤差。
round_style 傳回一個值,該值描述實作可選擇用來將浮點值捨入為整數值的各種方法。
signaling_NaN 傳回類型非數字 (NAN) 的訊號表示。
tinyness_before 測試類型是否可以判斷某個值太小,而無法在捨入前以標準化數值表示。
traps 測試要報告算術例外狀況的設限是否會針對類型實作。

denorm_min

傳回未標準化的最小非零值。

static constexpr Type denorm_min() throw();

傳回值

反正規化的最小非零值。

備註

long doubledouble C++ 編譯器相同。

函式會傳回型別的最小值,如果 has_denorm不等於 denorm_present ,則與 min 相同。

範例

// numeric_limits_denorm_min.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The smallest nonzero denormalized value" << endl
        << "for float objects is: "
        << numeric_limits<float>::denorm_min( ) << endl;
   cout << "The smallest nonzero denormalized value" << endl
        << "for double objects is: "
        << numeric_limits<double>::denorm_min( ) << endl;
   cout << "The smallest nonzero denormalized value" << endl
        << "for long double objects is: "
        << numeric_limits<long double>::denorm_min( ) << endl;

   // A smaller value will round to zero
   cout << numeric_limits<float>::denorm_min( )/2 <<endl;
   cout << numeric_limits<double>::denorm_min( )/2 <<endl;
   cout << numeric_limits<long double>::denorm_min( )/2 <<endl;
}
The smallest nonzero denormalized value
for float objects is: 1.4013e-045
The smallest nonzero denormalized value
for double objects is: 4.94066e-324
The smallest nonzero denormalized value
for long double objects is: 4.94066e-324
0
0
0

digits

傳回基數數字的數目,其中該類型可以在不減少有效位數的情況下表示。

static constexpr int digits = 0;

傳回值

該類型可在不減少有效位數的情況下代表的基數數字數目。

備註

成員會儲存該類型在不變更的情況下可代表的基數數字數目,這是預先定義之整數類型的位元數 (所有正負號位元除外),或是預先定義之浮點數類型的尾數數字數目。

範例

// numeric_limits_digits_min.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << numeric_limits<float>::digits <<endl;
   cout << numeric_limits<double>::digits <<endl;
   cout << numeric_limits<long double>::digits <<endl;
   cout << numeric_limits<int>::digits <<endl;
   cout << numeric_limits<__int64>::digits <<endl;
}
24
53
53
31
63

digits10

傳回十進位數字的數目,其中該類型可以在不減少有效位數的情況下表示。

static constexpr int digits10 = 0;

傳回值

該類型可在不減少有效位數的情況下代表的十進位數字數目。

範例

// numeric_limits_digits10.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << numeric_limits<float>::digits10 <<endl;
   cout << numeric_limits<double>::digits10 <<endl;
   cout << numeric_limits<long double>::digits10 <<endl;
   cout << numeric_limits<int>::digits10 <<endl;
   cout << numeric_limits<__int64>::digits10 <<endl;
   float f = (float)99999999;
   cout.precision ( 10 );
   cout << "The float is; " << f << endl;
}
6
15
15
9
18
The float is; 100000000

epsilon

函式會傳回可針對資料類型代表的 1 到大於 1 的最小值之間的差異。

static constexpr Type epsilon() throw();

傳回值

可針對資料類型代表的 1 到大於 1 的最小值之間的差異。

備註

型別 的值FLT_EPSILON float 。 一個類型的 epsilon 是最小的正浮點數 N,因此可顯示為 N + epsilon + N

範例

// numeric_limits_epsilon.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The difference between 1 and the smallest "
        << "value greater than 1" << endl
        << "for float objects is: "
        << numeric_limits<float>::epsilon( ) << endl;
   cout << "The difference between 1 and the smallest "
        << "value greater than 1" << endl
        << "for double objects is: "
        << numeric_limits<double>::epsilon( ) << endl;
   cout << "The difference between 1 and the smallest "
        << "value greater than 1" << endl
        << "for long double objects is: "
        << numeric_limits<long double>::epsilon( ) << endl;
}
The difference between 1 and the smallest value greater than 1
for float objects is: 1.19209e-007
The difference between 1 and the smallest value greater than 1
for double objects is: 2.22045e-016
The difference between 1 and the smallest value greater than 1
for long double objects is: 2.22045e-016

has_denorm

測試類型是否允許未標準化的值。

static constexpr float_denorm_style has_denorm = denorm_absent;

傳回值

const float_denorm_style 別 的列舉值,指出型別是否允許反正規化值。

備註

成員會儲存 denorm_present 具有反正規化值的浮點類型,實際上為指數位的可變數目。

範例

// numeric_limits_has_denorm.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects allow denormalized values: "
        << numeric_limits<float>::has_denorm
        << endl;
   cout << "Whether double objects allow denormalized values: "
        << numeric_limits<double>::has_denorm
        << endl;
   cout << "Whether long int objects allow denormalized values: "
        << numeric_limits<long int>::has_denorm
        << endl;
}
Whether float objects allow denormalized values: 1
Whether double objects allow denormalized values: 1
Whether long int objects allow denormalized values: 0

has_denorm_loss

測試偵測到的精確度遺失是否為未標準化遺失,而非不精確的結果。

static constexpr bool has_denorm_loss = false;

傳回值

true 如果偵測到精確度遺失為反正規化損失,則為 ; false 如果不是,則為 。

備註

成員會針對判斷一個值是否損失精確度的類型儲存 true,損失精確度的原因包括以反正規化結果 (太小以致於無法以正規化的值來表示) 形式傳遞該值,或是該值不精確 (與不受限於指數範圍和有效位數限制的結果不同),這是使用 IEC 559 浮點數表示法時的一個選項,而此選項可能影響某些結果。

範例

// numeric_limits_has_denorm_loss.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects can detect denormalized loss: "
        << numeric_limits<float>::has_denorm_loss
        << endl;
   cout << "Whether double objects can detect denormalized loss: "
        << numeric_limits<double>::has_denorm_loss
        << endl;
   cout << "Whether long int objects can detect denormalized loss: "
        << numeric_limits<long int>::has_denorm_loss
        << endl;
}
Whether float objects can detect denormalized loss: 1
Whether double objects can detect denormalized loss: 1
Whether long int objects can detect denormalized loss: 0

has_infinity

測試類型是否有正無限大的表示。

static constexpr bool has_infinity = false;

傳回值

true 如果類型具有正無限的標記法,則為 ; false 如果不是,則為 。

備註

如果 is_iec559 true ,則成員會傳 true 回 。

範例

// numeric_limits_has_infinity.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects have infinity: "
        << numeric_limits<float>::has_infinity
        << endl;
   cout << "Whether double objects have infinity: "
        << numeric_limits<double>::has_infinity
        << endl;
   cout << "Whether long int objects have infinity: "
        << numeric_limits<long int>::has_infinity
        << endl;
}
Whether float objects have infinity: 1
Whether double objects have infinity: 1
Whether long int objects have infinity: 0

has_quiet_NaN

測試類型是否有不是數字 (NAN) 的無訊息 (非訊號) 表示。

static constexpr bool has_quiet_NaN = false;

傳回值

true如果類型 具有無訊息 NAN 的表示,則為 , false 否則為 。

備註

無訊息 NAN 是代表「不是數字」(not a number) 的編碼,不會在運算式中發出其存在訊號。 如果 is_iec559 為 true,則傳回值 true 為 。

範例

// numeric_limits_has_quiet_nan.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects have quiet_NaN: "
        << numeric_limits<float>::has_quiet_NaN
        << endl;
   cout << "Whether double objects have quiet_NaN: "
        << numeric_limits<double>::has_quiet_NaN
        << endl;
   cout << "Whether long int objects have quiet_NaN: "
        << numeric_limits<long int>::has_quiet_NaN
        << endl;
}
Whether float objects have quiet_NaN: 1
Whether double objects have quiet_NaN: 1
Whether long int objects have quiet_NaN: 0

has_signaling_NaN

測試類型是否有不是數字 (NAN) 的訊號表示。

static constexpr bool has_signaling_NaN = false;

傳回值

true 如果型別具有向南訊號標記法則為 ; false 如果不是,則為 。

備註

帶訊號 NAN 是代表「不是數字」的編碼,會在運算式中發出其存在訊號。 如果 is_iec559 為 true,則傳回值 true 為 。

範例

// numeric_limits_has_signaling_nan.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects have a signaling_NaN: "
        << numeric_limits<float>::has_signaling_NaN
        << endl;
   cout << "Whether double objects have a signaling_NaN: "
        << numeric_limits<double>::has_signaling_NaN
        << endl;
   cout << "Whether long int objects have a signaling_NaN: "
        << numeric_limits<long int>::has_signaling_NaN
        << endl;
}
Whether float objects have a signaling_NaN: 1
Whether double objects have a signaling_NaN: 1
Whether long int objects have a signaling_NaN: 0

infinity

類型的正無限大表示 (如果有的話)。

static constexpr Type infinity() throw();

傳回值

類型的正無限大表示 (如果有的話)。

備註

只有在 has_infinity true ,傳回值才有意義。

範例

// numeric_limits_infinity.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << numeric_limits<float>::has_infinity <<endl;
   cout << numeric_limits<double>::has_infinity<<endl;
   cout << numeric_limits<long double>::has_infinity <<endl;
   cout << numeric_limits<int>::has_infinity <<endl;
   cout << numeric_limits<__int64>::has_infinity <<endl;

   cout << "The representation of infinity for type float is: "
        << numeric_limits<float>::infinity( ) <<endl;
   cout << "The representation of infinity for type double is: "
        << numeric_limits<double>::infinity( ) <<endl;
   cout << "The representation of infinity for type long double is: "
        << numeric_limits<long double>::infinity( ) <<endl;
}
1
1
1
0
0
The representation of infinity for type float is: inf
The representation of infinity for type double is: inf
The representation of infinity for type long double is: inf

is_bounded

測試類型可能代表的一組值是否為有限值。

static constexpr bool is_bounded = false;

傳回值

true 如果類型有一組限定的可表示值,則為 ; false 如果不是,則為 。

備註

所有預先定義的型別都有一組限定的可表示值,並傳回 true

範例

// numeric_limits_is_bounded.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects have bounded set "
        << "of representable values: "
        << numeric_limits<float>::is_bounded
        << endl;
   cout << "Whether double objects have bounded set "
        << "of representable values: "
        << numeric_limits<double>::is_bounded
        << endl;
   cout << "Whether long int objects have bounded set "
        << "of representable values: "
        << numeric_limits<long int>::is_bounded
        << endl;
   cout << "Whether unsigned char objects have bounded set "
        << "of representable values: "
        << numeric_limits<unsigned char>::is_bounded
        << endl;
}
Whether float objects have bounded set of representable values: 1
Whether double objects have bounded set of representable values: 1
Whether long int objects have bounded set of representable values: 1
Whether unsigned char objects have bounded set of representable values: 1

is_exact

測試執行計算的類型是否沒有進位誤差。

static constexpr bool is_exact = false;

傳回值

true 如果計算沒有四捨五入錯誤,則為 ; false 如果不是,則為 。

備註

所有預先定義的整數類型都有其值的確切標記法,並傳回 false 。 固定點或有理數表示也視為精確,但浮點數表示則不視為精確。

範例

// numeric_limits_is_exact.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects have calculations "
        << "free of rounding errors: "
        << numeric_limits<float>::is_exact
        << endl;
   cout << "Whether double objects have calculations "
        << "free of rounding errors: "
        << numeric_limits<double>::is_exact
        << endl;
   cout << "Whether long int objects have calculations "
        << "free of rounding errors: "
        << numeric_limits<long int>::is_exact
        << endl;
   cout << "Whether unsigned char objects have calculations "
        << "free of rounding errors: "
        << numeric_limits<unsigned char>::is_exact
        << endl;
}
Whether float objects have calculations free of rounding errors: 0
Whether double objects have calculations free of rounding errors: 0
Whether long int objects have calculations free of rounding errors: 1
Whether unsigned char objects have calculations free of rounding errors: 1

is_iec559

測試類型是否符合 IEC 559 標準。

static constexpr bool is_iec559 = false;

傳回值

true 如果類型符合 IEC 559 標準,則為 ; false 如果不是,則為 。

備註

IEC 559 是一個表示浮點值的國際標準,在美國也稱為 IEEE 754。

範例

// numeric_limits_is_iec559.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects conform to iec559 standards: "
        << numeric_limits<float>::is_iec559
        << endl;
   cout << "Whether double objects conform to iec559 standards: "
        << numeric_limits<double>::is_iec559
        << endl;
   cout << "Whether int objects conform to iec559 standards: "
        << numeric_limits<int>::is_iec559
        << endl;
   cout << "Whether unsigned char objects conform to iec559 standards: "
        << numeric_limits<unsigned char>::is_iec559
        << endl;
}
Whether float objects conform to iec559 standards: 1
Whether double objects conform to iec559 standards: 1
Whether int objects conform to iec559 standards: 0
Whether unsigned char objects conform to iec559 standards: 0

is_integer

測試類型是否有整數表示。

static constexpr bool is_integer = false;

傳回值

true 如果類型具有整數表示,則為 ; false 如果不是,則為 。

備註

所有預先定義的整數類型都有整數表示。

範例

// numeric_limits_is_integer.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects have an integral representation: "
        << numeric_limits<float>::is_integer
        << endl;
   cout << "Whether double objects have an integral representation: "
        << numeric_limits<double>::is_integer
        << endl;
   cout << "Whether int objects have an integral representation: "
        << numeric_limits<int>::is_integer
        << endl;
   cout << "Whether unsigned char objects have an integral representation: "
        << numeric_limits<unsigned char>::is_integer
        << endl;
}
Whether float objects have an integral representation: 0
Whether double objects have an integral representation: 0
Whether int objects have an integral representation: 1
Whether unsigned char objects have an integral representation: 1

is_modulo

測試「類型」是否有模數表示。

static constexpr bool is_modulo = false;

傳回值

true 如果類型具有模數標記法,則為 ; false 如果不是,則為 。

備註

模數表示是一種所有結果都是某個值之簡化模數的表示。 所有預先定義的不帶正負號整數類型都有模數表示。

範例

// numeric_limits_is_modulo.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects have a modulo representation: "
        << numeric_limits<float>::is_modulo
        << endl;
   cout << "Whether double objects have a modulo representation: "
        << numeric_limits<double>::is_modulo
        << endl;
   cout << "Whether signed char objects have a modulo representation: "
        << numeric_limits<signed char>::is_modulo
        << endl;
   cout << "Whether unsigned char objects have a modulo representation: "
        << numeric_limits<unsigned char>::is_modulo
        << endl;
}
Whether float objects have a modulo representation: 0
Whether double objects have a modulo representation: 0
Whether signed char objects have a modulo representation: 1
Whether unsigned char objects have a modulo representation: 1

is_signed

測試類型是否有正負號表示。

static constexpr bool is_signed = false;

傳回值

true 如果類型具有帶正負號標記法,則為 ; false 如果不是,則為 。

備註

成員會針對有正負號表示的類型儲存 true,這適用於所有預先定義的浮點數和帶正負號的整數類型。

範例

// numeric_limits_is_signaled.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects have a signed representation: "
        << numeric_limits<float>::is_signed
        << endl;
   cout << "Whether double objects have a signed representation: "
        << numeric_limits<double>::is_signed
        << endl;
   cout << "Whether signed char objects have a signed representation: "
        << numeric_limits<signed char>::is_signed
        << endl;
   cout << "Whether unsigned char objects have a signed representation: "
        << numeric_limits<unsigned char>::is_signed
        << endl;
}
Whether float objects have a signed representation: 1
Whether double objects have a signed representation: 1
Whether signed char objects have a signed representation: 1
Whether unsigned char objects have a signed representation: 0

is_specialized

測試類型是否有在類別範本 numeric_limits 中定義的明確特製化。

static constexpr bool is_specialized = false;

傳回值

true 如果類型在類別範本中定義明確特製化,則為 ; false 如果不是,則為 。

備註

指標以外的所有純量類型都有針對類別範本 numeric_limits 定義的明確特製化。

範例

// numeric_limits_is_specialized.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float objects have an explicit "
        << "specialization in the class: "
        << numeric_limits<float>::is_specialized
        << endl;
   cout << "Whether float* objects have an explicit "
        << "specialization in the class: "
        << numeric_limits<float*>::is_specialized
        << endl;
   cout << "Whether int objects have an explicit "
        << "specialization in the class: "
        << numeric_limits<int>::is_specialized
        << endl;
   cout << "Whether int* objects have an explicit "
        << "specialization in the class: "
        << numeric_limits<int*>::is_specialized
        << endl;
}
Whether float objects have an explicit specialization in the class: 1
Whether float* objects have an explicit specialization in the class: 0
Whether int objects have an explicit specialization in the class: 1
Whether int* objects have an explicit specialization in the class: 0

最低

傳回最大負數的有限值。

static constexpr Type lowest() throw();

傳回值

傳回最大負數的有限值。

備註

傳回類型的最大負數有限值 (如果是整數類型,通常是 min(),如果是浮點數類型則是 -max())。 如果 is_boundedtrue,則傳回值有意義。

max

傳回類型的最大有限值。

static constexpr Type max() throw();

傳回值

類型的最大有限值。

備註

型別的有限值上限為 INT_MAX int ,類型為 float FLT_MAX。 如果 is_bounded 為 ,則傳回值有意義 true

範例

// numeric_limits_max.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main() {
   cout << "The maximum value for type float is:  "
        << numeric_limits<float>::max( )
        << endl;
   cout << "The maximum value for type double is:  "
        << numeric_limits<double>::max( )
        << endl;
   cout << "The maximum value for type int is:  "
        << numeric_limits<int>::max( )
        << endl;
   cout << "The maximum value for type short int is:  "
        << numeric_limits<short int>::max( )
        << endl;
}

max_digits10

傳回十進位數字的數值,需要有這個數值,才能確保類型的兩個不同值有不同的十進位表示法。

static constexpr int max_digits10 = 0;

傳回值

傳回十進位數字的數值,需要有這個數值,才能確保類型的兩個不同值有不同的十進位表示法。

備註

此成員儲存十進位數字的數值,需要有這個數值,才能確保類型的兩個不同值有不同的十進位表示法。

max_exponent

傳回當基數的基底自乘至該乘冪時,浮點類型可以有限值表示的最大正整數指數。

static constexpr int max_exponent = 0;

傳回值

類型可代表的最大整數基數型指數。

備註

此成員函式傳回值僅對浮點數類型有意義。 max_exponent是 型 float 別 的值FLT_MAX_EXP。

範例

// numeric_limits_max_exponent.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The maximum radix-based exponent for type float is:  "
        << numeric_limits<float>::max_exponent
        << endl;
   cout << "The maximum radix-based exponent for type double is:  "
        << numeric_limits<double>::max_exponent
        << endl;
   cout << "The maximum radix-based exponent for type long double is:  "
        << numeric_limits<long double>::max_exponent
        << endl;
}
The maximum radix-based exponent for type float is:  128
The maximum radix-based exponent for type double is:  1024
The maximum radix-based exponent for type long double is:  1024

max_exponent10

傳回當 10 的基底自乘至該乘冪時,浮點類型可以有限值表示的最大正整數指數。

static constexpr int max_exponent10 = 0;

傳回值

類型可代表之以 10 為底數的最大整數指數。

備註

此成員函式傳回值僅對浮點數類型有意義。 max_exponent是 型 float 別 的值FLT_MAX_10。

範例

// numeric_limits_max_exponent10.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The maximum base 10 exponent for type float is:  "
           << numeric_limits<float>::max_exponent10
           << endl;
   cout << "The maximum base 10 exponent for type double is:  "
           << numeric_limits<double>::max_exponent10
           << endl;
   cout << "The maximum base 10 exponent for type long double is:  "
           << numeric_limits<long double>::max_exponent10
           << endl;
}
The maximum base 10 exponent for type float is:  38
The maximum base 10 exponent for type double is:  308
The maximum base 10 exponent for type long double is:  308

分鐘

傳回類型的最小標準化數值。

static constexpr Type min() throw();

傳回值

類型的最小正規化值。

備註

int 類型的最小正規化值是 INT_MIN,float 類型的最大正規化值則是 FLT_MIN。 如果 is_boundedtrue,或如果 is_signedfalse,傳回值便有意義。

範例

// numeric_limits_min.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The minimum value for type float is:  "
        << numeric_limits<float>::min( )
        << endl;
   cout << "The minimum value for type double is:  "
        << numeric_limits<double>::min( )
        << endl;
   cout << "The minimum value for type int is:  "
        << numeric_limits<int>::min( )
        << endl;
   cout << "The minimum value for type short int is:  "
        << numeric_limits<short int>::min( )
        << endl;
}
The minimum value for type float is:  1.17549e-038
The minimum value for type double is:  2.22507e-308
The minimum value for type int is:  -2147483648
The minimum value for type short int is:  -32768

min_exponent

傳回當基數的基底自乘至該乘冪時,浮點類型可以有限值表示的最大負整數指數。

static constexpr int min_exponent = 0;

傳回值

類型可代表的最小整數基數型指數。

備註

此成員函式僅對浮點數類型有意義。 min_exponent是 型 float 別 的值FLT_MIN_EXP。

範例

// numeric_limits_min_exponent.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The minimum radix-based exponent for type float is:  "
        << numeric_limits<float>::min_exponent
        << endl;
   cout << "The minimum radix-based exponent for type double is:  "
        << numeric_limits<double>::min_exponent
        << endl;
   cout << "The minimum radix-based exponent for type long double is:  "
         << numeric_limits<long double>::min_exponent
        << endl;
}
The minimum radix-based exponent for type float is:  -125
The minimum radix-based exponent for type double is:  -1021
The minimum radix-based exponent for type long double is:  -1021

min_exponent10

傳回當 10 的基底自乘至該乘冪時,浮點類型可以有限值表示的最大負整數指數。

static constexpr int min_exponent10 = 0;

傳回值

類型可代表之以 10 為底數的最小整數指數。

備註

此成員函式僅對浮點數類型有意義。 min_exponent10是 型 float 別 的值FLT_MIN_10_EXP。

範例

// numeric_limits_min_exponent10.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The minimum base 10 exponent for type float is:  "
        << numeric_limits<float>::min_exponent10
        << endl;
   cout << "The minimum base 10 exponent for type double is:  "
        << numeric_limits<double>::min_exponent10
        << endl;
   cout << "The minimum base 10 exponent for type long double is:  "
        << numeric_limits<long double>::min_exponent10
        << endl;
}
The minimum base 10 exponent for type float is:  -37
The minimum base 10 exponent for type double is:  -307
The minimum base 10 exponent for type long double is:  -307

quiet_NaN

傳回類型非數字 (NAN) 的無訊息表示。

static constexpr Type quiet_NaN() throw();

傳回值

該類型的無訊息 NAN 表示。

備註

只有在 has_quiet_NaN 為 時 ,傳回值才 true 有意義。

範例

// numeric_limits_quiet_nan.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The quiet NaN for type float is:  "
        << numeric_limits<float>::quiet_NaN( )
        << endl;
   cout << "The quiet NaN for type int is:  "
        << numeric_limits<int>::quiet_NaN( )
        << endl;
   cout << "The quiet NaN for type long double is:  "
        << numeric_limits<long double>::quiet_NaN( )
        << endl;
}
The quiet NaN for type float is:  1.#QNAN
The quiet NaN for type int is:  0
The quiet NaN for type long double is:  1.#QNAN

基數

傳回用來表示類型的整數基底 (稱為基數)。

static constexpr int radix = 0;

傳回值

用來表示類型的整數底數。

備註

就預先定義的整數類型而言,底數為 2,而就預先定義的浮點數類型而言,則為指數的底數 (或 FLT_RADIX)。

範例

// numeric_limits_radix.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The base for type float is:  "
        << numeric_limits<float>::radix
        << endl;
   cout << "The base for type int is:  "
        << numeric_limits<int>::radix
        << endl;
   cout << "The base for type long double is:  "
        << numeric_limits<long double>::radix
        << endl;
}
The base for type float is:  2
The base for type int is:  2
The base for type long double is:  2

round_error

傳回類型的最大進位誤差。

static constexpr Type round_error() throw();

傳回值

該類型的最大進位誤差。

範例

// numeric_limits_round_error.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The maximum rounding error for type float is:  "
        << numeric_limits<float>::round_error( )
        << endl;
   cout << "The maximum rounding error for type int is:  "
        << numeric_limits<int>::round_error( )
        << endl;
   cout << "The maximum rounding error for type long double is:  "
        << numeric_limits<long double>::round_error( )
        << endl;
}
The maximum rounding error for type float is:  0.5
The maximum rounding error for type int is:  0
The maximum rounding error for type long double is:  0.5

round_style

傳回一個值,該值描述實作可選擇用來將浮點值捨入為整數值的各種方法。

static constexpr float_round_style round_style = round_toward_zero;

傳回值

一個來自 float_round_style 列舉的值,描述捨入樣式。

備註

成員會儲存一個值,此值描述實作在將浮點數值捨入成整數值時,可選擇的各種方法。

捨入樣式在此實作中是採用硬式編碼,因此,即使是以不同的捨入模式啟動程式,該值也不會改變。

範例

// numeric_limits_round_style.cpp
// compile with: /EHsc
#include <iostream>
#include <float.h>
#include <limits>

using namespace std;

int main( )
{
   cout << "The rounding style for a double type is: "
        << numeric_limits<double>::round_style << endl;
   _controlfp_s(NULL,_RC_DOWN,_MCW_RC );
   cout << "The rounding style for a double type is now: "
        << numeric_limits<double>::round_style << endl;
   cout << "The rounding style for an int type is: "
        << numeric_limits<int>::round_style << endl;
}
The rounding style for a double type is: 1
The rounding style for a double type is now: 1
The rounding style for an int type is: 0

signaling_NaN

傳回類型非數字 (NAN) 的訊號表示。

static constexpr Type signaling_NaN() throw();

傳回值

該類型的帶訊號 NAN 表示。

備註

只有在 has_signaling_NaN 為 時 傳回值才 true 有意義。

範例

// numeric_limits_signaling_nan.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The signaling NaN for type float is:  "
        << numeric_limits<float>::signaling_NaN( )
        << endl;
   cout << "The signaling NaN for type int is:  "
        << numeric_limits<int>::signaling_NaN( )
        << endl;
   cout << "The signaling NaN for type long double is:  "
        << numeric_limits<long double>::signaling_NaN( )
        << endl;
}

tinyness_before

測試類型是否可以判斷某個值太小,而無法在捨入前以標準化數值表示。

static constexpr bool tinyness_before = false;

傳回值

如果該類型可以偵測到捨入前的微小值,便會傳回 true;否則會傳回 false

備註

可偵測微小值的類型已包含在使用 IEC 559 浮點數表示法的選項中,而其實作可能影響某些結果。

範例

// numeric_limits_tinyness_before.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float types can detect tinyness before rounding: "
        << numeric_limits<float>::tinyness_before
        << endl;
   cout << "Whether double types can detect tinyness before rounding: "
        << numeric_limits<double>::tinyness_before
        << endl;
   cout << "Whether long int types can detect tinyness before rounding: "
        << numeric_limits<long int>::tinyness_before
        << endl;
   cout << "Whether unsigned char types can detect tinyness before rounding: "
        << numeric_limits<unsigned char>::tinyness_before
        << endl;
}
Whether float types can detect tinyness before rounding: 1
Whether double types can detect tinyness before rounding: 1
Whether long int types can detect tinyness before rounding: 0
Whether unsigned char types can detect tinyness before rounding: 0

traps

測試要報告算術例外狀況的設限是否會針對類型實作。

static constexpr bool traps = false;

傳回值

true 如果已針對類型實作截獲,則為 ; false 如果不是,則為 。

範例

// numeric_limits_traps.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "Whether float types have implemented trapping: "
        << numeric_limits<float>::traps
        << endl;
   cout << "Whether double types have implemented trapping: "
        << numeric_limits<double>::traps
        << endl;
   cout << "Whether long int types have implemented trapping: "
        << numeric_limits<long int>::traps
        << endl;
   cout << "Whether unsigned char types have implemented trapping: "
        << numeric_limits<unsigned char>::traps
        << endl;
}
Whether float types have implemented trapping: 1
Whether double types have implemented trapping: 1
Whether long int types have implemented trapping: 0
Whether unsigned char types have implemented trapping: 0