简体   繁体   中英

Weird std::atof bug with gcc 7.4.0

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM