Read and write using std::hexfloat

有些话、适合烂在心里 提交于 2019-12-01 18:21:41

问题


This piece of code printed 0 on my machine, but I expected 0.3. What's wrong? I'm using g++ 6.3.1 on latest Arch Linux. Compilation flags seem unrelevent.

#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:


Use double d = std::strtod(s.str().c_str(), NULL); as a workaround. It seems like a bug.



来源:https://stackoverflow.com/questions/42604596/read-and-write-using-stdhexfloat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!