C++中使用Visual Studio 2019时,numeric_limits获取错误的long double最小/最大值。

huangapple go评论70阅读模式
英文:

Wrong C++ long double min/max values using numeric_limits in Visual Studio 2019

问题

使用Visual Studio Community 2019 v16.4.2,使用64位Win10,并带有最新的附带内容。

在测试各种数据类型限制时,遇到了一个奇怪的bug,numeric_limits无法区分double和long double的最小/最大值。使用默认GNU Mac工具链的NetBeans显示更合理的结果。

	// 类型限制:float
	std::cout 
        << "MIN float        " << numeric_limits<float>::min() << "\n"
		<< "MAX float        " << numeric_limits<float>::max() << "\n"
		<< "MIN double       " << numeric_limits<double>::min() << "\n"
		<< "MAX double       " << numeric_limits<double>::max() << "\n"
		<< "MIN long double  " << numeric_limits<long double>::min() << "\n"
		<< "MAX long double  " << numeric_limits<long double>::max() << "\n";

控制台输出

MIN float        1.17549e-38
MAX float        3.40282e+38
MIN double       2.22507e-308
MAX double       1.79769e+308
MIN long double  2.22507e-308    // 在Mac上的NetBeans为3.3621e-4932
MAX long double  1.79769e+308    // 在Mac上的NetBeans为1.18973e+4932
英文:

Using Visual Studio Community 2019 v16.4.2 with the latest stuff it comes with on 64bit Win10.

While testing various datatype limits ran into a weird bug, numeric_limits can't distinguish between double and long double min/max values. Displays more reasonable results using NetBeans with default GNU Mac tool chain.

	// Type limits: float
	std::cout 
        &lt;&lt; &quot;MIN float        &quot; &lt;&lt; numeric_limits&lt;float&gt;::min() &lt;&lt; &quot;\n&quot;
		&lt;&lt; &quot;MAX float        &quot; &lt;&lt; numeric_limits&lt;float&gt;::max() &lt;&lt; &quot;\n&quot;
		&lt;&lt; &quot;MIN double       &quot; &lt;&lt; numeric_limits&lt;double&gt;::min() &lt;&lt; &quot;\n&quot;
		&lt;&lt; &quot;MAX double       &quot; &lt;&lt; numeric_limits&lt;double&gt;::max() &lt;&lt; &quot;\n&quot;
		&lt;&lt; &quot;MIN long double  &quot; &lt;&lt; numeric_limits&lt;long double&gt;::min() &lt;&lt; &quot;\n&quot;
		&lt;&lt; &quot;MAX long double  &quot; &lt;&lt; numeric_limits&lt;long double&gt;::max() &lt;&lt; &quot;\n&quot;;

Console Output
>
&gt; MIN float 1.17549e-38
&gt; MAX float 3.40282e+38
&gt; MIN double 2.22507e-308
&gt; MAX double 1.79769e+308
&gt; MIN long double 2.22507e-308 // NetBeans on Mac 3.3621e-4932
&gt; MAX long double 1.79769e+308 // NetBeans on Mac 1.18973e+4932
&gt;

答案1

得分: 8

C++标准只要求long double至少具有double的精度,因此您的程序输出没有问题。

标准引用(§3.9.1,第8段):

有三种浮点类型:float、double和long double。类型double提供的精度至少与float一样多,类型long double提供的精度至少与double一样多。类型float的值集合是类型double值集合的子集;类型double的值集合是类型long double值集合的子集。浮点类型的值表示是实现定义的。整数和浮点类型统称为算术类型。标准模板std::numeric_limits(18.3)的特化应指定每种算术类型的最大和最小值,用于实现。

英文:

The c++ standard only requires long double to have at least the precision of double, so there's nothing wrong with the output of your program.

Quote from the standard (§3.9.1, lit 8):

> There are three floating point types: float,double, and long double.The type double provides at least as much precision as float, and the type long double provides at least as much precision as double.The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double. The value representation of floating-point types is implementation-defined. Integral and floating types are collectively called arithmetic types. Specializations of the standard template std::numeric_limits (18.3) shall specify the maximum and minimum values of each arithmetic type for an implementation.

答案2

得分: 1

long doubledouble 在使用 Visual Studio 时是等效的。

doublelong double 都包含 64 位:1 位用于符号,11 位用于指数,52 位用于尾数。其范围为+/-1.7E308,至少具有 15 位有效数字的精度。

这符合 C++ 标准。它只要求 long double 至少具有与 double 相同的精度。详见这里,官方参考文档。

英文:

long double and double are equivalent using Visual Studio.

Both double and long double are containing 64 bits: 1 for sign, 11 for the exponent, and 52 for the mantissa. Its range is +/-1.7E308 with at least 15 digits of precision.

This is in correspondence with the c++ standard. It only requires long double to have at least the precision of double. See here for a official reference.

huangapple
  • 本文由 发表于 2020年1月6日 20:19:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定