std::numeric_limits是C/C++11中的一个模板类,在库编译平台提供基础算术类型的极值等属性信息,取代传统C语言,所采用的预处理常数。比较常用的使用是对于给定的基础类型用来判断在当前系统上的最大值、最小值。下面通过一段程序看看std::numeric_limits是怎么使用的。

#include <iostream>
#include <cstdlib>
#include <limits>

int main()
{
std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << std::endl;
std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << std::endl;
std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << std::endl;
std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << std::endl;
std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << std::endl;

std::cout << "Minimum value for float: " << std::numeric_limits<float>::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<float>::lowest() << std::endl; // the lowest value
std::cout << "Maximum value for float: " << std::numeric_limits<float>::max() << std::endl;
std::cout << "float is signed: " << std::numeric_limits<float>::is_signed << std::endl;
std::cout << "Non-sign bits in float: " << std::numeric_limits<float>::digits << std::endl;
std::cout << "float has infinity: " << std::numeric_limits<float>::has_infinity << std::endl;

std::cout << "Minimum value for unsigned short: " << std::numeric_limits<unsigned short>::min() << std::endl;
std::cout << "Maximum value for unsigned short: " << std::numeric_limits<unsigned short>::max() << std::endl;
 
std::cout << "is_specialized(float): " << std::numeric_limits<float>::is_specialized << std::endl;
std::cout << "is_integer(float): " << std::numeric_limits<float>::is_integer << std::endl;
std::cout << "is_exact(float): " << std::numeric_limits<float>::is_exact << std::endl;
std::cout << "is_bounded(float): " << std::numeric_limits<float>::is_bounded << std::endl;
std::cout << "is_modulo(float): " << std::numeric_limits<float>::is_modulo << std::endl;
std::cout << "is_iec559(float): " << std::numeric_limits<float>::is_iec559 << std::endl;
std::cout << "digits10(float): " << std::numeric_limits<float>::digits10 << std::endl;
std::cout << "radix(float): " << std::numeric_limits<float>::radix << std::endl;
std::cout << "min_exponent(float): " << std::numeric_limits<float>::min_exponent << std::endl;
std::cout << "max_exponent(float): " << std::numeric_limits<float>::max_exponent << std::endl;
std::cout << "min_exponent10(float): " << std::numeric_limits<float>::min_exponent10 << std::endl;
std::cout << "max_exponent10(float): " << std::numeric_limits<float>::max_exponent10 << std::endl;
std::cout << "epsilon(float): " << std::numeric_limits<float>::epsilon() << std::endl;
std::cout << "round_style(float): " << std::numeric_limits<float>::round_style << std::endl;

std::cout << "The smallest nonzero denormalized value for float: "
	<< std::numeric_limits<float>::denorm_min()<< std::endl;
std::cout << "The difference between 1 and the smallest value greater than 1 for float: "
	<< std::numeric_limits<float>::epsilon()<< std::endl;
std::cout << "Whether float objects allow denormalized values: "
	<< std::numeric_limits<float>::has_denorm << std::endl;
std::cout << "Whether float objects can detect denormalized loss: "
	<< std::numeric_limits<float>::has_denorm_loss << std::endl;
std::cout << "Whether float objects have quiet_NaN: "
	<< std::numeric_limits<float>::has_quiet_NaN << std::endl;
std::cout << "Whether float objects have a signaling_NaN: "
	<< std::numeric_limits<float>::has_signaling_NaN << std::endl;
std::cout << "The base for type float is:  "
	<< std::numeric_limits<float>::radix << std::endl;
std::cout << "The maximum rounding error for type float is:  "
	<< std::numeric_limits<float>::round_error() << std::endl;
std::cout << "The rounding style for a double type is: "
	<< std::numeric_limits<double>::round_style << std::endl;
std::cout << "The signaling NaN for type float is:  "
	<< std::numeric_limits<float>::signaling_NaN() << std::endl;
std::cout << "Whether float types can detect tinyness before rounding: "
	<< std::numeric_limits<float>::tinyness_before << std::endl;
std::cout << "Whether float types have implemented trapping: "
	<< std::numeric_limits<float>::traps << std::endl;

return 0;
}

利用 g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.71.0/gcc-head/include -std=c++11 的执行结果如下: 

Minimum value for int: -2147483648
Maximum value for int: 2147483647
int is signed: true
Non-sign bits in int: 31
int has infinity: false
Minimum value for float: 1.17549e-38
Lowest value for float: -3.40282e+38
Maximum value for float: 3.40282e+38
float is signed: true
Non-sign bits in float: 24
float has infinity: true
Minimum value for unsigned short: 0
Maximum value for unsigned short: 65535
is_specialized(float): true
is_integer(float): false
is_exact(float): false
is_bounded(float): true
is_modulo(float): false
is_iec559(float): true
digits10(float): 6
radix(float): 2
min_exponent(float): -125
max_exponent(float): 128
min_exponent10(float): -37
max_exponent10(float): 38
epsilon(float): 1.19209e-07
round_style(float): 1
The smallest nonzero denormalized value for float: 1.4013e-45
The difference between 1 and the smallest value greater than 1 for float: 1.19209e-07
Whether float objects allow denormalized values: 1
Whether float objects can detect denormalized loss: false
Whether float objects have quiet_NaN: true
Whether float objects have a signaling_NaN: true
The base for type float is:  2
The maximum rounding error for type float is:  0.5
The rounding style for a double type is: 1
The signaling NaN for type float is:  nan
Whether float types can detect tinyness before rounding: false
Whether float types have implemented trapping: false