C++ std::numeric_limits::max,min及lowest
std::numeric_limits<T>::max(),min()及lowest()
根據std::numeric_limits,如果T
屬於浮點數,則:
std::numeric_limits<T>::max()
回傳該型別的最大值std::numeric_limits<T>::min()
回傳該型別的"最小正數"std::numeric_limits<T>::lowest()
回傳該型別的最小值(為負數或0)。
如果T
屬於整數,則min
與lowest
會回傳一樣的值,皆為該型別的最小值(為負數或0)。
筆者寫了一段代碼: cpp-code-snippets/numeric_limits.cpp。 當中輸出了 int
, unsigned int
, float
, double
, long double
等5種型別的最大值,最小正數值及最小值,其運行結果為:
max, min, lowest for 5 different data types:
int:
2147483647
-2147483648
-2147483648
unsigned int:
4294967295
0
0
float:
3.40282e+38
1.17549e-38
-3.40282e+38
double:
1.79769e+308
2.22507e-308
-1.79769e+308
long double:
1.18973e+4932
3.3621e-4932
-1.18973e+4932
在TensorRT/parsers/caffe/caffeWeightFactory/caffeWeightFactory.cpp
的convertInternal
中:
template <typename INPUT, typename OUTPUT>
void* convertInternal(void** ptr, int64_t count, bool* mOK)
{
//...
if (/**/ > std::numeric_limits<OUTPUT>::max()
|| /**/ < std::numeric_limits<OUTPUT>::lowest())
}
便用std::numeric_limits
所提供的函數來判斷一個值是否落在OUTPUT
型別的最小值和最大值之間。要注意的是,此處使用的是lowest
而非min
,來獲取OUTPUT
型別的最小值。