From ac2bde9caf41764eacca4f9ece1b724dc924babf Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 17 Dec 2015 13:07:17 -0800 Subject: QVariant: make sure two floating points compare equal if they are equal Not just fuzzily equal. The fuzzy comparison fails for infinities, since the expression p1 - p2 where p1 = p2 = infinity is NaN. And NaN comparisons are always false. As a nice side-effect, we don't do the more expensive computation of a multiplication if the two numbers really are equal. Task-number: QTBUG-50036 Change-Id: I11f559ef75544c50b3f8ffff1420cec7c7273295 Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/corelib/kernel/qvariant.cpp | 2 +- tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index fdcbdb1c45..3e6aefab2a 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -3448,7 +3448,7 @@ static int numericCompare(const QVariant::Private *d1, const QVariant::Private * Q_ASSERT(ok); qreal r2 = qConvertToRealNumber(d2, &ok); Q_ASSERT(ok); - if (qFuzzyCompare(r1, r2)) + if (r1 == r2 || qFuzzyCompare(r1, r2)) return 0; return r1 < r2 ? -1 : 1; } diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 40eb2e5c9d..c91bb21399 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -1711,12 +1711,16 @@ void tst_QVariant::compareNumbers_data() const QTest::newRow("float3") << qVariantFromValue(0.f) << qVariantFromValue(-1.f) << +1; QTest::newRow("float4") << qVariantFromValue(-float(qInf())) << qVariantFromValue(0.f) << -1; QTest::newRow("float5") << qVariantFromValue(0.f) << qVariantFromValue(-float(qInf())) << +1; + QTest::newRow("float6") << qVariantFromValue(-float(qInf())) << qVariantFromValue(-float(qInf())) << 0; + QTest::newRow("float7") << qVariantFromValue(float(qInf())) << qVariantFromValue(float(qInf())) << 0; QTest::newRow("double1") << qVariantFromValue(0.) << qVariantFromValue(0.) << 0; QTest::newRow("double2") << qVariantFromValue(-1.) << qVariantFromValue(0.) << -1; QTest::newRow("double3") << qVariantFromValue(0.) << qVariantFromValue(-1.) << +1; QTest::newRow("double4") << qVariantFromValue(-qInf()) << qVariantFromValue(0.) << -1; QTest::newRow("double5") << qVariantFromValue(0.) << qVariantFromValue(-qInf()) << +1; + QTest::newRow("double6") << qVariantFromValue(-double(qInf())) << qVariantFromValue(-qInf()) << 0; + QTest::newRow("double7") << qVariantFromValue(qInf()) << qVariantFromValue(qInf()) << 0; // mixed comparisons // fp + fp @@ -1725,6 +1729,8 @@ void tst_QVariant::compareNumbers_data() const QTest::newRow("float+double3") << qVariantFromValue(0.f) << qVariantFromValue(-1.) << +1; QTest::newRow("float+double4") << qVariantFromValue(-float(qInf())) << qVariantFromValue(0.) << -1; QTest::newRow("float+double5") << qVariantFromValue(0.f) << qVariantFromValue(-qInf()) << +1; + QTest::newRow("float+double6") << qVariantFromValue(-float(qInf())) << qVariantFromValue(qInf()) << 0; + QTest::newRow("float+double7") << qVariantFromValue(float(qInf())) << qVariantFromValue(qInf()) << 0; // fp + int QTest::newRow("float+int1") << qVariantFromValue(0.f) << qVariantFromValue(0) << 0; @@ -1735,6 +1741,7 @@ void tst_QVariant::compareNumbers_data() const QTest::newRow("double+int3") << qVariantFromValue(0.) << qVariantFromValue(-1) << +1; QTest::newRow("float+int4") << qVariantFromValue(1.5f) << qVariantFromValue(1) << +1; QTest::newRow("double+int4") << qVariantFromValue(1.5) << qVariantFromValue(1) << +1; + QTest::newRow("double+int5") << qVariantFromValue(qInf()) << qVariantFromValue(1) << +1; // fp + uint QTest::newRow("float+uint1") << qVariantFromValue(0.f) << qVariantFromValue(0U) << 0; -- cgit v1.2.1