Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and write using std::hexfloat

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;
}
like image 835
johnchen902 Avatar asked Mar 05 '17 04:03

johnchen902


1 Answers

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

like image 106
srinivirt Avatar answered Oct 22 '22 02:10

srinivirt