QDom: use correct precision when converting float/double values

d7cb21ac08 change the way a double is
converted which resulted in less precision.
Fix it by explictily setting the precision in QString::setNum()

Task-number: QTBUG-80068
Change-Id: I1fd9d00837155ceb707e84bfeb9deff03b5ab57e
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Reviewed-by: Konstantin Shegunov <kshegunov@gmail.com>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
bb10
Christian Ehrlicher 2020-02-19 18:48:32 +01:00
parent 4f370d36ec
commit 8c883c8da3
2 changed files with 21 additions and 10 deletions

View File

@ -62,6 +62,7 @@
#include <qshareddata.h>
#include <qdebug.h>
#include <stdio.h>
#include <limits>
QT_BEGIN_NAMESPACE
@ -4866,7 +4867,7 @@ void QDomElement::setAttribute(const QString& name, float value)
if (!impl)
return;
QString x;
x.setNum(value);
x.setNum(value, 'g', 8);
IMPL->setAttribute(name, x);
}
@ -4880,7 +4881,7 @@ void QDomElement::setAttribute(const QString& name, double value)
if (!impl)
return;
QString x;
x.setNum(value);
x.setNum(value, 'g', 17);
IMPL->setAttribute(name, x);
}
@ -5049,7 +5050,7 @@ void QDomElement::setAttributeNS(const QString nsURI, const QString& qName, doub
if (!impl)
return;
QString x;
x.setNum(value);
x.setNum(value, 'g', 17);
IMPL->setAttributeNS(nsURI, qName, x);
}

View File

@ -38,6 +38,7 @@
#include <QtTest/QtTest>
#include <QtXml>
#include <QVariant>
#include <cmath>
QT_FORWARD_DECLARE_CLASS(QDomDocument)
QT_FORWARD_DECLARE_CLASS(QDomNode)
@ -408,7 +409,9 @@ void tst_QDom::setGetAttributes()
const int intVal = std::numeric_limits<int>::min();
const uint uintVal = std::numeric_limits<uint>::max();
const float floatVal = 0.1234f;
const double doubleVal = 0.1234;
const double doubleVal1 = 1./6.;
const double doubleVal2 = std::nextafter(doubleVal1, 1.);
const double doubleVal3 = std::nextafter(doubleVal2, 1.);
rootNode.setAttribute("qstringVal", qstringVal);
rootNode.setAttribute("qlonglongVal", qlonglongVal);
@ -416,7 +419,9 @@ void tst_QDom::setGetAttributes()
rootNode.setAttribute("intVal", intVal);
rootNode.setAttribute("uintVal", uintVal);
rootNode.setAttribute("floatVal", floatVal);
rootNode.setAttribute("doubleVal", doubleVal);
rootNode.setAttribute("doubleVal1", doubleVal1);
rootNode.setAttribute("doubleVal2", doubleVal2);
rootNode.setAttribute("doubleVal3", doubleVal3);
QDomElement nsNode = doc.createElement("NS");
rootNode.appendChild(nsNode);
@ -426,7 +431,9 @@ void tst_QDom::setGetAttributes()
nsNode.setAttributeNS("namespace", "intVal", intVal);
nsNode.setAttributeNS("namespace", "uintVal", uintVal);
nsNode.setAttributeNS("namespace", "floatVal", floatVal); // not available atm
nsNode.setAttributeNS("namespace", "doubleVal", doubleVal);
nsNode.setAttributeNS("namespace", "doubleVal1", doubleVal1);
nsNode.setAttributeNS("namespace", "doubleVal2", doubleVal2);
nsNode.setAttributeNS("namespace", "doubleVal3", doubleVal3);
bool bOk;
QCOMPARE(rootNode.attribute("qstringVal"), qstringVal);
@ -440,8 +447,10 @@ void tst_QDom::setGetAttributes()
QVERIFY(bOk);
QCOMPARE(rootNode.attribute("floatVal").toFloat(&bOk), floatVal);
QVERIFY(bOk);
QCOMPARE(rootNode.attribute("doubleVal").toDouble(&bOk), doubleVal);
QVERIFY(bOk);
QVERIFY(rootNode.attribute("doubleVal1").toDouble(&bOk) == doubleVal1 && bOk);
QVERIFY(rootNode.attribute("doubleVal2").toDouble(&bOk) == doubleVal2 && bOk);
QVERIFY(rootNode.attribute("doubleVal3").toDouble(&bOk) == doubleVal3 && bOk);
QCOMPARE(nsNode.attributeNS("namespace", "qstringVal"), qstringVal);
QCOMPARE(nsNode.attributeNS("namespace", "qlonglongVal").toLongLong(&bOk), qlonglongVal);
@ -454,8 +463,9 @@ void tst_QDom::setGetAttributes()
QVERIFY(bOk);
QCOMPARE(nsNode.attributeNS("namespace", "floatVal").toFloat(&bOk), floatVal);
QVERIFY(bOk);
QCOMPARE(nsNode.attributeNS("namespace", "doubleVal").toDouble(&bOk), doubleVal);
QVERIFY(bOk);
QVERIFY(nsNode.attributeNS("namespace", "doubleVal1").toDouble(&bOk) == doubleVal1 && bOk);
QVERIFY(nsNode.attributeNS("namespace", "doubleVal2").toDouble(&bOk) == doubleVal2 && bOk);
QVERIFY(nsNode.attributeNS("namespace", "doubleVal3").toDouble(&bOk) == doubleVal3 && bOk);
QLocale::setDefault(oldLocale);
}