英文:
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
<< "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";
Console Output
>
> 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 // NetBeans on Mac 3.3621e-4932
> MAX long double 1.79769e+308 // NetBeans on Mac 1.18973e+4932
>
答案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 double
和 double
在使用 Visual Studio 时是等效的。
double
和 long 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论