在c/c 11中,std::numeric_limits为模板类,在库编译平台提供基础算术类型的极值等属性信息,取代传统c语言,所采用的预处理常数。比较常用的使用是对于给定的基础类型用来判断在当前系统上的最大值、最小值。若使用此类,需包含
min、max与c库宏常量的关系如下:
测试代码如下:
#include "numeric_limits.hpp"
#include
#include
//
/* reference:
http://www.cplusplus.com/reference/limits/numeric_limits/
https://msdn.microsoft.com/en-us/library/c707ct0t.aspx
*/
int test_numeric_limits_1()
{
std::cout << std::boolalpha;
std::cout << "minimum value for int: " << std::numeric_limits::min() << std::endl;
std::cout << "maximum value for int: " << std::numeric_limits::max() << std::endl;
std::cout << "int is signed: " << std::numeric_limits::is_signed << std::endl;
std::cout << "non-sign bits in int: " << std::numeric_limits::digits << std::endl;
std::cout << "int has infinity: " << std::numeric_limits::has_infinity << std::endl;
std::cout << "minimum value for float: " << std::numeric_limits::min() << std::endl; // min returns the smallest positive value the type can encode, not the lowest
std::cout << "lowest value for float: " << std::numeric_limits::lowest() << std::endl; // the lowest value
std::cout << "maximum value for float: " << std::numeric_limits::max() << std::endl;
std::cout << "float is signed: " << std::numeric_limits::is_signed << std::endl;
std::cout << "non-sign bits in float: " << std::numeric_limits::digits << std::endl;
std::cout << "float has infinity: " << std::numeric_limits::has_infinity << std::endl;
std::cout << "minimum value for unsigned short: " << std::numeric_limits::min() << std::endl;
std::cout << "maximum value for unsigned short: " << std::numeric_limits::max() << std::endl;
std::cout << "is_specialized(float): " << std::numeric_limits::is_specialized << std::endl;
std::cout << "is_integer(float): " << std::numeric_limits::is_integer << std::endl;
std::cout << "is_exact(float): " << std::numeric_limits::is_exact << std::endl;
std::cout << "is_bounded(float): " << std::numeric_limits::is_bounded << std::endl;
std::cout << "is_modulo(float): " << std::numeric_limits::is_modulo << std::endl;
std::cout << "is_iec559(float): " << std::numeric_limits::is_iec559 << std::endl;
std::cout << "digits10(float): " << std::numeric_limits::digits10 << std::endl;
std::cout << "radix(float): " << std::numeric_limits::radix << std::endl;
std::cout << "min_exponent(float): " << std::numeric_limits::min_exponent << std::endl;
std::cout << "max_exponent(float): " << std::numeric_limits::max_exponent << std::endl;
std::cout << "min_exponent10(float): " << std::numeric_limits::min_exponent10 << std::endl;
std::cout << "max_exponent10(float): " << std::numeric_limits::max_exponent10 << std::endl;
std::cout << "epsilon(float): " << std::numeric_limits::epsilon() << std::endl;
std::cout << "round_style(float): " << std::numeric_limits::round_style << std::endl;
std::cout << "the smallest nonzero denormalized value for float: "
<< std::numeric_limits::denorm_min()<< std::endl;
std::cout << "the difference between 1 and the smallest value greater than 1 for float: "
<< std::numeric_limits::epsilon()<< std::endl;
std::cout << "whether float objects allow denormalized values: "
<< std::numeric_limits::has_denorm << std::endl;
std::cout << "whether float objects can detect denormalized loss: "
<< std::numeric_limits::has_denorm_loss << std::endl;
std::cout << "whether float objects have quiet_nan: "
<< std::numeric_limits::has_quiet_nan << std::endl;
std::cout << "whether float objects have a signaling_nan: "
<< std::numeric_limits::has_signaling_nan << std::endl;
std::cout << "the base for type float is: "
<< std::numeric_limits::radix << std::endl;
std::cout << "the maximum rounding error for type float is: "
<< std::numeric_limits::round_error() << std::endl;
std::cout << "the rounding style for a double type is: "
<< std::numeric_limits::round_style << std::endl;
std::cout << "the signaling nan for type float is: "
<< std::numeric_limits::signaling_nan() << std::endl;
std::cout << "whether float types can detect tinyness before rounding: "
<< std::numeric_limits::tinyness_before << std::endl;
std::cout << "whether float types have implemented trapping: "
<< std::numeric_limits::traps << std::endl;
return 0;
}
在windows10 64位上执行结果如下: