九、 通用工具 ----数值的极限(Numeric Limit)

来源:互联网 发布:爱奇艺万能播放器mac版 编辑:程序博客网 时间:2024/05/22 12:57

3 数值的极限(Numeric Limit)

  1. 数值类型一般有与平台相互依靠的极值;
  2. c++使用template numeric_limits提供,定义于头文件
  3. c语言采用的是预处理常量:整数定义在<climits>和<limits.h>,浮点数定义在<cfloat>和<float.h>

c++定义的各类型的最小精度:
内建类型的最小精度

1 class numeric_limits<>

1.1 numeric_limits<>的所有成员

数值的极值1

数值的极值2

数值的极值3
数值的极值4

例子:

#include <limits>#include <iostream>int main() {    std::cout << "type\tlowest\thighest\n";    std::cout << "int\t"              << std::numeric_limits<int>::lowest() << '\t'              << std::numeric_limits<int>::max() << '\n';    std::cout << "float\t"              << std::numeric_limits<float>::lowest() << '\t'              << std::numeric_limits<float>::max() << '\n';    std::cout << "double\t"              << std::numeric_limits<double>::lowest() << '\t'              << std::numeric_limits<double>::max() << '\n';}

结果为:

type    lowest         highestint     -2147483648    2147483647float   -3.40282e+38   3.40282e+38double  -1.79769e+308  1.79769e+308