gpt4 book ai didi

c++ - 从文本文件中读取值而不会丢失 Qt 中的精度

转载 作者:行者123 更新时间:2023-11-28 05:16:09 25 4
gpt4 key购买 nike

我有一个 x 和 y 坐标的数据文件,我试图将它们读入 Qt 中的两个 double vector 。我的问题是这些值在小数点后 3 位被截断或四舍五入,即使我的原始值上升到 6。我的数据文件看起来像

35.659569 139.723370
35.659546 139.723194
35.659527 139.723051
35.659523 139.722909
35.659383 139.722946

我的代码是

QVector<double> v, v2;
QFile textFile (":/new/files/02262017newdata.txt");
if(textFile.open(QIODevice::ReadOnly))
{
qInfo() << "opened file successfully";
double a, b;
QTextStream textStream (&textFile);
while (!textStream.atEnd()) {
QString line = textFile.readLine();
QStringList list = line.split(" ");
if(list.size() == 2){
a = list.at(0).toDouble();
b = list.at(1).toDouble();
}
qInfo() << "a and b after using split is" << a <<" "<< b;
v.append(a);
v2.append(b);
}
}

如何在不损失精度的情况下读取值?

最佳答案

您不会失去精度。问题出在 qInfo() 中。尝试使用 qSetRealNumberPrecision()

qInfo() 中设置精度

替换这一行:

qInfo() << "a and b after using split is" << a <<" "<< b;

与:

qInfo() << "a and b after using split is"<< qSetRealNumberPrecision(8) << a <<" "<< b;

关于c++ - 从文本文件中读取值而不会丢失 Qt 中的精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42603949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com