-1

In my code, I have a large number of checks for equality...

for example:

int main(int argc, char *argv[])
{
    QApplication  a(argc, argv);


    QGraphicsLineItem* x = new QGraphicsLineItem(50, 50, -50, -50);
    QGraphicsView view(new QGraphicsScene(-200, -150, 400, 300) );
    view.scene()->addItem(x);
    view.show();

    bool sameLine = true;
    QLineF line1 = x->line();
    qreal _length = line1.length();

    foreach(QGraphicsItem* item, view.scene()->selectedItems())
    {
        QGraphicsLineItem *item2 = dynamic_cast<QGraphicsLineItem*>(item);
        if(item2->line().length() != _length )
            sameLine = false;
    }
    qDebug("same line: %d", sameLine);
}

It seems to work... in debug. Then when tested in release, it fails ?

Assume a single selected item, so item1 and item2 are the same, so regardless of precision, the above lengths should be equal....

In debug, I have not been able to see this fail... yet in release, it always fails !

The functions above (length()) return a qreal

The only work-arounds I see would be
- to implement my own check for equality, that limits the precision, or
- cast any qreal values to float.
But it is illogical (and a lot of work, I would have to check LOTS of potential places).

Can somebody please explain why this is happening and how best to go around this problem ?

Thalia
  • 13,637
  • 22
  • 96
  • 190
  • You clearly did not compile this code, as it would have told you that you tried to access a member of a pointer. Therefore, I can infer that you did not post the actual code with which you have a problem, which renders your question utterly worthless and completely unanswerable. Kindly come back when you have an actual question. – Puppy Jul 16 '15 at 20:52
  • @Puppy fixed, I copied this from a very large program with a very large number of checks, so indeed this is not the complete code. I copied and pasted the relevant portion. – Thalia Jul 16 '15 at 20:55
  • Can you provide an [MCVE](https://stackoverflow.com/help/mcve) that demonstrates this problem? It's difficult to diagnose what might be happening without full context. – Greg Hewgill Jul 16 '15 at 20:59
  • Changed code to insert into a main() – Thalia Jul 16 '15 at 21:19

1 Answers1

2

You should compare floating point values with for example qFuzzyCompare if you already use Qt. You should never compare two floating point variables with simply using ==.

p.i.g.
  • 2,815
  • 2
  • 24
  • 41
  • Thanks, it is probably what I need... yet I never realized how bad floating point comparison was... I have a lot of comparisons for other types, like QPointF and even QLineF - can I hope that internally the test for equality is implemented the same way ? – Thalia Jul 16 '15 at 21:01
  • Hope so, the [documentation](http://doc.qt.io/qt-4.8/qlinef.html#operator-eq-eq) does not tell us the details in this depth. – p.i.g. Jul 16 '15 at 21:06
  • I searched and I found that `QPointF` uses the `qFuzzyIsNull` function for this purpose. You can check it in the `QPoint` header file. – p.i.g. Jul 16 '15 at 21:09
  • `friend inline bool operator==(const QPointF &, const QPointF &);` Not sure – Thalia Jul 16 '15 at 21:21
  • Check the definition. It is also in the header. – p.i.g. Jul 16 '15 at 21:21
  • Never compare floating point with == and never use floating point numbers to represent money. Two "gotchas" even seasoned programmers get caught by. – Michael Vincent Jul 17 '15 at 09:26