在 C++ 中使用 istringstream 进行字符串到双精度转换的意外结果
题
我目前正在尝试使用 Xcode 10.6 中的 C++ 和 gcc4.2 获取一个字符串(“0.1”)并将其转换为双精度值。
我正在使用一个我捏的函数 另一个问题, ,但是当我尝试使用该函数时,根据gdb我的输入是(字符串)“0.1”,但我的输出是(双)2.1220023981051542e-314。
这是我直接复制的代码片段:
double strToDouble(std::string const &numberString)
{
istringstream inStream(numberString);
double doubleValue;
inStream >> doubleValue;
if(!(inStream && (inStream >> std::ws).eof()))
{
return 0.0;
}
return doubleValue;
};
我使用 C++ 而不是 Obj-C,因为它最终可能必须在 *nix 或 windows 机器上编译。
我天生就是一名 PHP 程序员,但是我需要加快一些数字运算的速度,所以我使用编译语言来完成它。自从大学处理低级语言以来已经很长时间了=P。因此,如果有人有想法,我们将不胜感激......
德鲁·J。索恩。
完整代码:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
/*
* @var delimiters the string to define a break by
* @var str the string to break
* @var tokens where to store the broken string parts.
*/
void explode(const string& delimiters, const string& str, vector<string>& tokens)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
};
/*
* @var numberString double to be converted as a string.
*/
double strToDouble(std::string const &numberString)
{
istringstream inStream(numberString);
double doubleValue;
inStream >> doubleValue;
if(!(inStream && (inStream >> std::ws).eof()))
{
return 0.0;
}
return doubleValue;
};
class Cluster {
private:
vector<double> latStore;
vector<double> lngStore;
public:
Cluster(int,string,string);
};
/*
* @var latString a comma seperated list of doubles for the latitudes.
* @var lngString a comma separated list of doubles for the longitudes.
*/
Cluster::Cluster(int newLocationLength, string latString, string lngString)
{
// mark our vectors
vector<string> latStoreString;
vector<string> lngStoreString;
// Explode our input strings.
explode(",", latString, latStoreString);
explode(",", lngString, lngStoreString);
for( int i = 0; i < latStoreString.size(); i++)
{
// Grab the latitude and store it.
string tempLat = latStoreString[i];
double latDouble = strToDouble(tempLat);
latStore.push_back( strToDouble(tempLat) );
// Grab the longitude and store it.
string tempLng = lngStoreString[i];
lngStore.push_back( strToDouble(tempLng) );
}
}
int main (int argc, char * const argv[]) {
Cluster *rect = new Cluster("0.1,0.4","0.1,0.4");
return 0;
}
解决方案
这可以通过STL调试模式引起的。在你的目标的预处理宏构建设置中删除_GLIBCXX_DEBUG宏。
其他提示
为什么不使用 atof() 来代替呢? 关联
我可以看到没有错,你的代码片段。你能告诉你使用到的输出的结果
这是代码作为迈尔斯建议,ATOF()可能是更简单的。可以通过调用c_str()成员函数转换字符串对象到C字符串:
double result = atof(inputString.c_str()); // 0.0 if a double couldn't be read
这里的一些信息 http://www.cplusplus.com/reference / clibrary / cstdlib / ATOF /
#include <iostream>
#include <sstream>
double strToDouble(std::string const &numberString)
{
std::istringstream inStream(numberString);
double doubleValue;
std::cout << doubleValue << '\n';
inStream >> doubleValue;
if(!(inStream && (inStream >> std::ws).eof()))
{
return 0.0;
}
return doubleValue;
};
int main()
{
std::cout << strToDouble("0.1") << '\n';
return 0;
}
上面的代码给我以下输出:结果 -2.36907e-39点击 0.1
一个偶然的机会返回之前检查的doubleValue的价值吗?
Cluster::Cluster(string latString, string lngString)
{
// mark our vectors
vector<string> latStoreString;
vector<string> lngStoreString;
// Explode our input strings.
explode(",", latString, latStoreString);
explode(",", lngString, lngStoreString);
for( int i = 0; i < latStoreString.size(); i++)
{
// Grab the latitude and store it.
string tempLat = latStoreString[i];
std::cout << "tempLat=" << tempLat << '\n';
double latDouble = strToDouble(tempLat);
std::cout << "latDouble=" << latDouble << '\n';
latStore.push_back( strToDouble(tempLat) );
// Grab the longitude and store it.
string tempLng = lngStoreString[i];
lngStore.push_back( strToDouble(tempLng) );
}
}
int main (int argc, char * const argv[]) {
Cluster *rect = new Cluster("0.1,0.4","0.1,0.4");
return 0;
}
输出:结果 tempLat = 0.1,点击 latDouble = 0.1,点击 tempLat = 0.4结果 latDouble = 0.4
您可以运行这些修改的代码(你没有使用在构造函数中的int参数,所以我只是删除它)。我还使用的valgrind没有错误(仅内存泄漏)跑您的代码。
貌似你没有得到回答您的问题implemetion。也有人建议使用ATOF()。如果你想解决这个问题在你的FPGA实现,那么就看看下面的代码。代替具有矢量爆炸方法,把载体,以避免冗余代码。我在strToDouble改变了代码。
void explode(const string& delimiters, const string& str, vector<double>& tokens)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
string temp = str.substr(lastPos, pos - lastPos);
tokens.push_back(strToDouble(temp));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
double strToDouble(std::string const &numberString)
{
istringstream inStream(numberString);
int pos = numberString.find_first_not_of("1234567890.", 0);
if (string::npos != pos)
{
return 0.0;
}
else
{
double doubleValue;
inStream >> doubleValue;
return doubleValue;
}
}