本文介绍了浮点和双精度(C ++)的实际最小/最大值是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读有关将 FLT_MIN和 FLT_MAX值用于浮点数的建议。每当我这样做时,代码块都会告诉我它的

I have read recommendations to use the "FLT_MIN" and "FLT_MAX" values for float. Whenever I do this, codeblocks tells me its

不知道这意味着我试图获取真实值并得到

Not knowing what this meant I tried to get real values and got

...但是这些并不清楚。

... but these don't clarify things.

这是我代码中的摘录

   char c;         // reserve: 1 byte, store 1 character (-128 to 127)
   int i;          // reserve: 4 bytes, store -2147483648 to 2147483657
   short int s;    // reserve: 2 bytes, store -32768 to 32767
   float f;        // reserve: 4 bytes, store ?? - ?? (? digits)
   double d;       // reserve: 8 bytes, store ?? - ?? (? digits)
   unsigned int u; //reserve: r bytes store 0 to 4294967295

   c = 'c';
   cout << c <<" lives at " << &c <<endl;

   i = 40000;
   cout << i <<" lives at " << &i <<endl;

   s = 100;
   cout << s <<" lives at " << &s <<endl;

   f = 10.1;
   cout << f <<" lives at " << &f <<endl;

   d = 10.102;
   cout << d <<" lives at " << &d <<endl;

   u = 1723;
   cout << u <<" lives at " << &u <<endl;

在代码段中,我们可以清楚地看到short int的min-max值,例如-32768 -32767。这些值是可以理解的正确值,但对于float和int而言,实际值尚不清楚。

In the snippet we can clearly see the min-max values of a short int for example at -32768 - 32767. These are proper understandable values, but for float and int, the real values are not clear.

推荐答案

<$ < limits> 标头中的c $ c> std :: numerics_limits 类提供有关数字类型特征的信息。

The std::numerics_limits class in the <limits> header provides information about the characteristics of numeric types.

对于浮点类型 T ,以下是该类型中可表示的最大值和最小值,从各种意义上来说,都是最大和最少。我还包括通用IEEE 754 64位二进制类型的值,此答案中称为 double 。这些按降序排列:

For a floating-point type T, here are the greatest and least values representable in the type, in various senses of "greatest" and "least." I also include the values for the common IEEE 754 64-bit binary type, which is called double in this answer. These are in decreasing order:


  • std :: numeric_limits< T> :: infinity() T 支持无穷大,则/ code>是最大的可表示值。当然,它是无穷大。 T 类型是否支持无穷大由 std :: numeric_limits< T> :: has_infinity 表示。

  • std::numeric_limits<T>::infinity() is the largest representable value, if T supports infinity. It is, of course, infinity. Whether the type T supports infinity is indicated by std::numeric_limits<T>::has_infinity.

std :: numeric_limits< T> :: max()是最大的有限值。对于 double ,这是2 −2 ,大约1.79769•10

std::numeric_limits<T>::max() is the largest finite value. For double, this is 2−2, approximately 1.79769•10.

std :: numeric_limits< T> :: min()是最小的正态值。浮点格式通常具有一个间隔,在该间隔中,指数不能变得更小,但是有效数字(数字的分数部分)可以变得更小,直到达到零为止。这是以精度为代价的,但是具有一些合乎需要的数学计算特性。 min()是精度损失开始的地方。对于 double ,这是2 ,大约是2.22507•10 。

std::numeric_limits<T>::min() is the smallest positive normal value. Floating-point formats often have an interval where the exponent cannot get any smaller, but the significand (fraction portion of the number) is allowed to get smaller until it reaches zero. This comes at the expense of precision but has some desirable mathematical-computing properties. min() is the point where this precision loss starts. For double, this is 2, approximately 2.22507•10.

std :: numeric_limits< T> :: denorm_min()是最小的正值。在具有次标准值的类型中,它是次标准的。否则,它等于 std :: numeric_limits< T> :: min()。对于 double ,这是2 ,大约是4.94066•10 。

std::numeric_limits<T>::denorm_min() is the smallest positive value. In types which have subnormal values, it is subnormal. Otherwise, it equals std::numeric_limits<T>::min(). For double, this is 2, approximately 4.94066•10.

std :: numeric_limits< T> :: lowest()是最小限度值。通常为负数,幅度很大。对于 double ,这是−(2 −2 ),大约为−1.79769•10 。

std::numeric_limits<T>::lowest() is the least finite value. It is usually a negative number large in magnitude. For double, this is −(2−2), approximately −1.79769•10.

如果 std :: numeric_limits< T> :: has_infinity std :: numeric_limits< T> :: is_signed 为true,则 -std :: numeric_limits< T> :: infinity()是最小值。当然,它是负无穷大。

If std::numeric_limits<T>::has_infinity and std::numeric_limits<T>::is_signed are true, then -std::numeric_limits<T>::infinity() is the least value. It is, of course, negative infinity.

您可能感兴趣的另一个特征是:

Another characteristic you may be interested in is:


  • std :: numeric_limits< T> :: digits10 是最大的十进制数字,因此可以转换任何具有如此多位数的小数位数都将变为 T ,然后再转换回相同的小数位数将产生原始数。对于 double ,这是15。

  • std::numeric_limits<T>::digits10 is the greatest number of decimal digits such that converting any decimal number with that many digits to T and then converting back to the same number of decimal digits will yield the original number. For double, this is 15.

这篇关于浮点和双精度(C ++)的实际最小/最大值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:02