Weird std::atof bug with gcc 7.4.0

165 Views Asked by At

I've got this weird behavior with std::atof in Ubuntu 18.0.4 with Qt Creator(4.10) as IDE / gcc 7.4.0 :

It parses strings as normal when i run in debug mode from QtCreator. But it floors when i run normally.

Example code with this behavior:

std::string exampleStr = "3.0303";
std::cout << "string value: " << exampleStr << std::endl;
std::cout << "double value - c_str(): " << std::atof(exampleStr.c_str()) << std::endl;

Output with normal run from IDE:

string value: 3.0303

double value - c_str(): 3

Output with running directly from executable:

string value: 3.0303

double value - c_str(): 3

Output with debug mode:

string value: 3.0303

double value - c_str(): 3.0303

I've tried both std::stof and std::strtof. Both same. Anybody knows the reason or work around of this bug?

Edit: I've workaround with this, but still wonder the reason of this behavior.

std::string exampleStr = "3.0303";    
std::stringstream ss;
ss << exampleStr;
float val = 0;
ss >> val;
std::cout << "Float value: " << val << std::endl;
1

There are 1 best solutions below

0
hsyntsy On

I've changed locale with this:

std::setlocale(LC_ALL, "en_US.UTF-8");

and it worked for both debug and run modes. It's still interesting running in debug mode gets different locale than running normally. Thx for all responses.