float 和 double (C) 的实际最小/最大值是多少

新手上路,请多包涵

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

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

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

最大值:47.2498237715 最小值:-34.8045265148

…但这些并不能澄清事情。

这是我的代码片段

   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,实际值并不清楚。

原文由 user9318444 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 3.5k
2 个回答

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

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

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

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

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

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

  • std::numeric_limits<T>::lowest() 是最小有限值。它通常是一个数量级很大的负数。对于 double ,这是 -(2 1024 -2 971 ),大约为 -1.79769•10 308 。

  • 如果 std::numeric_limits<T>::has_infinitystd::numeric_limits<T>::is_signed 为真,那么 -std::numeric_limits<T>::infinity() 是最小值。当然,它是负无穷大。

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

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

原文由 Eric Postpischil 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

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

花车和双打

浮点数可以存储以下值:

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

双精度值可以存储来自:

  • -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000 Double lowest

  • 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000 Double max

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

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

例如

     float x = 1.426;
     double y = 8.739437;

小数和数学

由于浮点数能够携带 7 个 实数 小数,而双精度数能够携带 15 个 实数 小数,因此在执行计算时必须使用正确的方法将它们打印出来。

例如

包括

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 */

这个描述大概有多准确?迷茫的时候可以作为标准吗?

原文由 user9318444 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题