使用std :: hexfloat进行读写

时间:2017-03-05 04:42:19

标签: c++ floating-point iostream iomanip

这段代码在我的机器上打印0,但我预计0.3。怎么了?我在最新的Arch Linux上使用g ++ 6.3.1。编译标志似乎无关紧要。

#include <iostream>
#include <sstream>
int main() {
    std::stringstream s;
    s << std::hexfloat << 0.3 << std::endl;
    double d = -1.0;
    while(s >> std::hexfloat >> d)
        std::cout << d << std::endl;
}

1 个答案:

答案 0 :(得分:5)

使用double d = std::strtod(s.str().c_str(), NULL);作为解决方法。这似乎是一个错误。

相关问题