将double转换为string to double throws exception

sig*_*igy 8 c++ gcc type-conversion visual-studio

以下代码在Visual Studio 2013中抛出std :: out_of_range异常,在我看来它不应该:

#include <string>
#include <limits>

int main(int argc, char ** argv)
{
    double maxDbl = std::stod(std::to_string(std::numeric_limits<double>::max()));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我也用gcc 4.9.2测试了代码,并且它没有抛出异常.该问题似乎是由转换为字符串后的字符串表示不准确引起的.在Visual Studio中std::to_string(std::numeric_limits<double>::max())产生

179769313486231610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000

这确实看起来太大了.然而,在gcc中,它会产生

179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

这似乎小于传递的值.

但是,不std::numeric_limits<double>::max()应该归还

最大有限可表示的浮点数?

那么为什么字符串表示会出现呢?我在这里错过了什么?

Ser*_*sta 1

直接回答

Gcc(以及 Clang 和 VS2105)正确返回 (2 1024 - 1) - (2 1024-53 - 1)的整数值,即用 52 位有效数和 1023 的无偏指数表示的值 (2 1024 - 1)将是 1023 位的整数值,我只需减去 IEE754 格式的 52 以下的所有位)

我可以确认一个大整数库给出179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368L

之前的精确浮点数将小 2 971 (971 = 1023 - 52),即:179769313486231550856124328384506240234343437157459335924404872448581845754556114388470639943126220321960804027157371570809852884964511743044087662767600909594331927728237078876188760579532563768698654064825262115771015791463983014857704008123419459386245141723703148097529108423358883457665451722744025579520L

下一个不可表示的值将大 2 971,即: 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216L

但MSVC2013及之前使用的值接近2 1024 + 2 971,即:179769313486231610731333614426100589925524828262616317947942685512308090830973387504827396012048193870699768806228404251083258210739369062217227314575410731769485876273179688476358949112102859294830297395714877595371718127781702814782017661749531126051903195165027873311156314696040132728420308633064323416064L 。由于它大于 IEEE754 双精度可表示的任何值,因此无法将其解码为双精度。

因为最多可以说 2 1024 - 2 971 ( std::numeric_limits<double>::max()) 和 2 1024之间的任何值都可以舍入为std::numeric_limits<double>::max(),但大于 2 1024的值显然是溢出。


准确性讨论

双精度数中只有 16 位十进制数字是准确的,所有其他数字都可以被视为垃圾或随机值,因为它们不依赖于值本身,而只依赖于您选择的一种计算方式。只需尝试减去 1e+288(这已经是一个很大的值),maxDbl看看会发生什么:

maxLess = max Dbl - 1.e+288;
if (maxLess == maxDbl) {
   std::cout << "Unchanged" << std::endl;
}
else std::cout << "Changed" << std::endl;
Run Code Online (Sandbox Code Playgroud)

你应该看到......不变。

看起来 VS 2013 在舍入浮点值的方式上有点不连贯:它将 maxDbl 舍入比最大实际可表示值高一位,并且以后无法对其进行解码。

问题在于该标准选择使用的%f格式会产生错误的准确性。如果你想在 gcc 中看到等效的问题,只需使用:

#include <iostream>
#include <string>
#include <limits>
#include <iomanip>
#include <sstream>

int main() {
    double max = std::numeric_limits<double>::max();
    std::ostringstream ostr;
    ostr << std::setprecision(16) << max;
    std::string smax = ostr.str();
    std::cout << smax << std::endl;
    double m2 = std::stod(smax);
    std::cout << m2 << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

四舍五入到 16 位 mxDbl 写入(正确):1.797693134862316e+308,但无法再解码回来

和这个 :

#include <iostream>
#include <string>
#include <limits>

int main() {
    double maxDbl = std::numeric_limits<double>::max();
    std::string smax = std::to_string(maxDbl);
    std::cout << smax << std::endl;
    
    std::string smax2 = "179769313486231570800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000";

    double max2 = std::stod(smax2);
    if (max2 == maxDbl) {
       std::cout << smax2 << " is same double as " << smax << std::endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

显示:

179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
179769313486231570800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 is same double as 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
Run Code Online (Sandbox Code Playgroud)

TL/DR :我的意思是,一个很大的双精度值当然可以用一个精确的整数表示(根据 IEEE754)。但它确实代表了上一个的一半和下一个的一半之间的所有整数。因此,该范围内的任何整数都可以是双精度数的可接受表示形式,并且以 16 位十进制数字舍入的一个值应该是可接受的,但当前标准库仅允许将最大浮点值截断16 位十进制数字。但 VS2013 给出的数字高于范围的最大值,无论如何都是错误。

参考

维基百科上的 IEEE 浮点