【问题标题】:What are the actual min/max values for float and double (C++)float 和 double (C++) 的实际最小/最大值是多少
【发布时间】:2018-02-05 19:31:11
【问题描述】:

我已经阅读了使用“FLT_MIN”和“FLT_MAX”值作为浮点数的建议。每当我这样做时,代码块都会告诉我它的

最大:3.40282e+038 最小:1.17549e-038

不知道这意味着什么,我试图获得真正的价值并得到了

最大值:47.2498237715 分钟:-34.8045265148

...但这些并不能说明问题。

这是我的代码中的一个 sn-p

   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;

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

【问题讨论】:

标签: c++ variables floating-point double


【解决方案1】:

好的。使用我从这里学到的东西(谢谢大家)和网络的其他部分,我写了一个简洁的小总结,以防万一我遇到这样的另一个问题。

在 C++ 中有两种方式来表示/存储十进制值。

花车和双打

浮点数可以存储来自:

  • -340282346638528859811704183484516925440.0000000000000000 最低浮动
  • 340282346638528859811704183484516925440.0000000000000000 最大浮点数

double 可以存储以下值:

  • -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000双人最低

    LI>
  • 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000双人最大

    LI>

Float 的精度允许它存储最多 9 位的值(7 个实数,+2 从十进制到二进制的转换)

Double,顾名思义,可以存储两倍于浮点数的精度。它最多可以存储 17 位数字。 (15个实数,+2从十进制到二进制转换)

例如

     float x = 1.426;
     double y = 8.739437;

小数和数学

由于 float 能够携带 7 个 real 小数,而 double 能够携带 15 个 real 小数,因此在执行计算时以适当的方法将它们打印出来必须使用。

例如

包括

typedef std::numeric_limits<double> dbl; 
cout.precision(dbl::max_digits10-2); // sets the precision to the *proper* amount of digits.
cout << dbl::max_digits10 <<endl; // prints 17.
double x = 12345678.312; 
double a = 12345678.244; 
// these calculations won't perform correctly be printed correctly without setting the precision.


cout << endl << x+a <<endl;

示例 2:

typedef std::numeric_limits< float> flt;
cout.precision(flt::max_digits10-2);
cout << flt::max_digits10 <<endl;
float x =  54.122111;
float a =  11.323111;

cout << endl << x+a <<endl; /* without setting precison this outputs a different value, as well as making sure we're *limited* to 7 digits. If we were to enter another digit before the decimal point, the digits on the right would be one less, as there can only be 7. Doubles work in the same way */

这个描述大概有多准确?困惑时可以作为标准吗?

【讨论】:

  • “存储最多 9 位的值(7 位实数,+2 从十进制到二进制转换)”更可能应该是“9 位(6 位实数,+3 从 ...`
  • Float's precision allows it to store a value of up to 9 digits,但是您之前写的号码包含大约 40 位数字..?
【解决方案2】:

&lt;limits&gt; 标头中的 std::numerics_limits 类提供有关数字类型特征的信息。

对于浮点类型T,以下是该类型中可表示的最大值和最小值,在“最大”和“最小”的各种意义上。我还包括了常见 IEEE 754 64 位二进制类型的值,在这个答案中称为 double。这些是按降序排列的:

  • std::numeric_limits&lt;T&gt;::infinity() 是最大可表示值,如果T 支持无穷大。当然,它是无限的。 T 类型是否支持无穷大由std::numeric_limits&lt;T&gt;::has_infinity 表示。

  • std::numeric_limits&lt;T&gt;::max() 是最大的有限值。对于double,这是 21024−2971,大约为 1.79769•10308

  • std::numeric_limits&lt;T&gt;::min() 是最小的正正常值。浮点格式通常有一个区间,指数不能变小,但有效数(数字的小数部分)可以变小,直到它达到零。这是以牺牲精度为代价的,但具有一些理想的数学计算特性。 min() 是这种精度损失的开始点。对于double,这是2-1022,大约是2.22507•10-308

  • std::numeric_limits&lt;T&gt;::denorm_min() 是最小的正值。在具有次正规值的类型中,它是次正规的。否则,它等于std::numeric_limits&lt;T&gt;::min()。对于double,这是2-1074,大约是4.94066•10-324

  • std::numeric_limits&lt;T&gt;::lowest() 是最小有限值。它通常是一个数量级很大的负数。对于double,这是-(21024-2971),大约是-1.79769•10308

    李>
  • 如果std::numeric_limits&lt;T&gt;::has_infinitystd::numeric_limits&lt;T&gt;::is_signed 为真,则-std::numeric_limits&lt;T&gt;::infinity() 是最小值。当然是负无穷大。

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

  • std::numeric_limits&lt;T&gt;::digits10 是最大的十进制位数,因此将任何具有这么多位数的十进制数转换为 T,然后再转换回相同的十进制位数将产生原始数字。对于 double,这是 15。

【讨论】:

  • 为什么没有简短的回答?浮点存储的最大值和最小值?
【解决方案3】:

这一切都可以在 numeric_limits 中找到。

但是小心

出于某种我不知道的原因,std::numeric_limits&lt;float&gt;:min() 没有返回最小浮点数。相反,它返回以标准化形式表示的最小正浮点数。要获得最小值,请使用std::numeric_limits&lt;float&gt;::lowest()。我不骗你。对于其他浮点类型也是如此,即doublelong double

http://en.cppreference.com/w/cpp/types/numeric_limits

【讨论】:

    【解决方案4】:

    在 C++ 中,您可以使用 std::numeric_limits 类来获取此类信息。

    如果has_infinitytrue(现在基本上适用于任何平台),那么您可以使用infinity 来获取大于或等于所有其他值的值(NaN 除外)。它的否定将给出负无穷大,并且小于或等于所有其他值(再次除了 NaN)。

    如果您想要有限值,那么您可以使用max,它将大于或等于所有其他有限值,以及lowest,它小于或等于所有其他有限值。

    有点令人困惑的是,min 实际上为您提供了最小的正标准化值,这与它提供的整数类型完全不同步(感谢@JiveDadson 指出这一点)。

    【讨论】:

    • 我不太明白。使用 FLT_MANT_DIG(我们可以在浮点数中存储多少位)您会得到 24,但是不可能存储这么多位。另外,如果浮点数和双精度数是无限的,这是否意味着我们可以在浮点数中存储任意大的值?并使用 std::numeric_limits::min() 返回 0.000000 - 这个剂量;没有意义
    • 表示基数格式的位数,在本例中为2。(即二进制32数的有效位有24位)
    • 很遗憾,`std::numeric_limits::min()' 并不意味着你认为它对浮点 T 的作用。我什至发现了 VC++ 的 STL 滥用它。
    • 那么我如何找出浮点数/双精度数中有多少位?和一个可以存储在其中的最大值/最小值?
    • 感谢@JiveDadson!我完全忘记了那个乱七八糟的行为。
    猜你喜欢
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    相关资源
    最近更新 更多