c++ 数值极限

  一般来说,数值型别的极限是一个与平台相关的特性。c++标准程序库通过template numeric_limits提供这些极值,取代c语言所采用的预处理常数。你仍然可以使用后者,其中整数常数定义于<climits>和<limits.h>,浮点常数定义于<cfloat>和<float.h> 。新的极值概念有两个优点,第一提供了更好的型别安全性,第二是程序员可借此写出一些template以核定这些极值。

    1.内建型别的最小长度

  C++Standard 规定了各种型别必须保证的最小精度:见下:

    型别           最小长度

    char           1 byte

    short int     2bytes

    int              2bytes

    long int      4bytes

    float          4bytes

    double      8bytes

     long  double     8bytes

下面是C++11对numeric_limits的定义:(来自http://www.cplusplus.com/reference/limits/numeric_limits/)

c++ 数值极限
 1 template <class T> class numeric_limits {
 2 public:
 3   static constexpr bool is_specialized = false;
 4   static constexpr T min() noexcept { return T(); }
 5   static constexpr T max() noexcept { return T(); }
 6   static constexpr T lowest() noexcept { return T(); }
 7   static constexpr int  digits = 0;
 8   static constexpr int  digits10 = 0;
 9   static constexpr bool is_signed = false;
10   static constexpr bool is_integer = false;
11   static constexpr bool is_exact = false;
12   static constexpr int radix = 0;
13   static constexpr T epsilon() noexcept { return T(); }
14   static constexpr T round_error() noexcept { return T(); }
15 
16   static constexpr int  min_exponent = 0;
17   static constexpr int  min_exponent10 = 0;
18   static constexpr int  max_exponent = 0;
19   static constexpr int  max_exponent10 = 0;
20 
21   static constexpr bool has_infinity = false;
22   static constexpr bool has_quiet_NaN = false;
23   static constexpr bool has_signaling_NaN = false;
24   static constexpr float_denorm_style has_denorm = denorm_absent;
25   static constexpr bool has_denorm_loss = false;
26   static constexpr T infinity() noexcept { return T(); }
27   static constexpr T quiet_NaN() noexcept { return T(); }
28   static constexpr T signaling_NaN() noexcept { return T(); }
29   static constexpr T denorm_min() noexcept { return T(); }
30 
31   static constexpr bool is_iec559 = false;
32   static constexpr bool is_bounded = false;
33   static constexpr bool is_modulo = false;
34 
35   static constexpr bool traps = false;
36   static constexpr bool tinyness_before = false;
37   static constexpr float_round_style round_style = round_toward_zero;
38 };
c++ 数值极限

 

         numeric_limits<>的成员:

digits10

返回目标类型在十进制下可以表示的最大位数

epsilon

返回目标数据类型能表示的最逼近1的正数和1的差的绝对值

has_denorm

测试目标类型是不是可以非规范化表示示

has_denorm_loss

测试所有类型是不是能测出因为非规范化而造成的精度损失(不是因为结果本身的不精确)

has_infinity

测试目标类型是不是能表示无限(比如被0除,或者其他一些情况)

has_quiet_NaN

检查目标类型是不是支持安静类型的NaN

has_signaling_NaN

检查目标类型是不是支持信号类型的NaN

infinity

检查目标类型的无限类型(如果支持无限表示)

is_bounded

检查目标类型的取值是否有限

is_exact

测试目标类型的计算结果是不是不会造成舍入误差(比如float是0)

is_iec559

测试目标类型是不是符合IEC559标准

is_integer

测试目标类型是不是可以用整型来表示(比如char是1,float是0)

is_modulo

Tests if a type has a modulo representation.

is_signed

测试目标类型是否是带符号的

is_specialized

测试目标类型是不是在numeric_limits .模板类中有特殊定义

max

返回可取的有限最大值

max_exponent

Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power.

max_exponent10

Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power.

min

返回可取的最小值(规范化)

min_exponent

Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power.

min_exponent10

Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power.

quiet_NaN

返回目标类型的安静NAN的表示

radix

Returns the integral base, referred to as radix, used for the representation of a type.

round_error

返回目标类型的最大可能的舍入误差

round_style

Returns a value that describes the various methods that an implementation can choose for rounding a floating-point value to an integer value.

signaling_NaN

返回目标类型关于信号NAN的表示

tinyness_before

测试目标类型是不是能测定出微小的舍入误差

traps

Tests whether trapping that reports on arithmetic exceptions is implemented for a type.

 例子:

#include<iostream>
#include <limits>
#include <string>
using namespace std;

int main()
{
  cout << boolalpha;

  cout << "max(short): "<< numeric_limits<short>::max() <<endl;
  cout << "max(int): " << numeric_limits<int>::max() <<endl;
  cout << "max(long): " << numeric_limits<long>::max() << endl;
  cout <<endl;

  cout << "max(float): " << numeric_limits<float>::max() <<endl;
  cout << "max(double): "<< numeric_limits<double>::max() <<endl;
  cout << "max(long double): " << numeric_limits<long double>::max() <<endl;
  cout << endl;

  cout << "is_signed(char): " << numeric_limits<char>::is_signed <<endl;
  cout << "is_specialized(string): " <<numeric_limits<string>::is_specialized<<endl;
  return 0;
}

在我台电脑上的运行结果为:

max(short): 32767
max(int): 2147483647
max(long): 2147483647

max(float): 3.40282e+38
max(double): 1.79769e+308
max(long double): 1.18973e+4932

is_signed(char): true
is_specialized(string): false

 

 

 

    

c++ 数值极限,布布扣,bubuko.com

c++ 数值极限

上一篇:js中reduce函数


下一篇:深入分析利用宏代码传播NetwiredRC和Quasar RAT的恶意RTF文档