Read and write using std::hexfloat
Asked Answered
C

1

6

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;
}
Circe answered 5/3, 2017 at 4:42 Comment(0)
B
6

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

Backman answered 6/3, 2017 at 3:41 Comment(2)
Running the code provided, I get 0.3 as the output. So seems like a bug to me too.Destructor
This is a bug, but not in gcc: see gcc.gnu.org/bugzilla/show_bug.cgi?id=81122#c1 .Hemorrhoid

© 2022 - 2024 — McMap. All rights reserved.