在 Visual Studio 2019 中使用 numeric_limits 的错误 C++ long double min/max 值

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

使用 Visual Studio Community 2019 v16.4.2 及其在 64 位 Win10 上附带的最新内容。

在将各种数据类型限制 运行 测试为一个奇怪的错误时,numeric_limits 无法区分 double 和 long double min/max 值。使用带有默认 GNU Mac 工具链的 NetBeans 显示更合理的结果。

    // 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";

控制台输出

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

long doubledouble 等同于使用 Visual Studio。

doublelong double都包含64位:1位符号,11位指数,52位尾数。它的范围是+/-1.7E308,精度至少为15位。

这符合c++标准。它只需要 long double 至少具有 double 的精度。请参阅 here 以获得官方参考。

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

引自标准(§3.9.1,第 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.