From 6b19e159580b5c6fdd693262bf9bc67b2755c1b7 Mon Sep 17 00:00:00 2001
From: Thomas Hartmann <Thomas.Hartmann@nokia.com>
Date: Fri, 22 Jul 2011 12:55:51 +0200
Subject: [PATCH] QmlDesigner.rewriter: fixing compare function

Comparing doubles is hard.
I forgot to round properly.
I also added some early rejects.

Task-number: QTCREATORBUG-5503
Change-Id: I959abbe28a312aea63a170d9c769b5555ac6a30e
Reviewed-on: http://codereview.qt.nokia.com/2032
Reviewed-by: Thomas Hartmann <Thomas.Hartmann@nokia.com>
---
 .../designercore/model/texttomodelmerger.cpp   | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/plugins/qmldesigner/designercore/model/texttomodelmerger.cpp b/src/plugins/qmldesigner/designercore/model/texttomodelmerger.cpp
index b7c918b9aac..09d33976855 100644
--- a/src/plugins/qmldesigner/designercore/model/texttomodelmerger.cpp
+++ b/src/plugins/qmldesigner/designercore/model/texttomodelmerger.cpp
@@ -646,13 +646,20 @@ static inline bool smartVeryFuzzyCompare(QVariant value1, QVariant value2)
 { //we ignore slight changes on doubles and only check three digits
     if ((value1.type() == QVariant::Double) || (value2.type() == QVariant::Double)) {
         bool ok1, ok2;
-        int a = value1.toDouble(&ok1) * 1000;
-        int b = value2.toDouble(&ok2) * 1000;
+        qreal a = value1.toDouble(&ok1);
+        qreal b = value2.toDouble(&ok2);
 
         if (!ok1 || !ok2)
             return false;
 
-        if (qFuzzyCompare((qreal(a) / 1000), (qreal(b) / 1000))) {
+        if (qFuzzyCompare(a, b)) {
+            return true;
+        }
+
+        int ai = qRound(a * 1000);
+        int bi = qRound(b * 1000);
+
+        if (qFuzzyCompare((qreal(ai) / 1000), (qreal(bi) / 1000))) {
             return true;
         }
     }
@@ -661,10 +668,11 @@ static inline bool smartVeryFuzzyCompare(QVariant value1, QVariant value2)
 
 static inline bool equals(const QVariant &a, const QVariant &b)
 {
+    if (a == b)
+        return true;
     if (smartVeryFuzzyCompare(a, b))
         return true;
-    else
-        return a == b;
+    return false;
 }
 
 TextToModelMerger::TextToModelMerger(RewriterView *reWriterView) :
-- 
GitLab