Important: Please read the Qt Code of Conduct -

Implementation of qFuzzyCompare and zero values

  • Hello all,
    I'm using qFuzzyCompare(a, b) in order to handle all floating point comparisons, but it has severe limitations when a or b may be zero.

    Somewhere in the docs it is recommended to add +1 to the values, but this is not a really useful suggestion because some of the values might be -1.0, resulting in 0.0 after the addition.

    So far, in each file which needs to perform floating point comparisons, I'm using the following macro:

    #include <QtGlobal> // qMax, qMin, qFuzzyCompare
    #define qFuzzyCompare(a, b) (qFuzzyIsNull(a) && qFuzzyIsNull(b)) || qFuzzyCompare((a), (b))

    But instead of ad-hoc workarounds, IMHO this caveat should be fixed straight in the implementation of the qFuzzyCompare() function. A really old bug report already exists: "QTBUG-16819": it is open but it seems that discussion ended on 2011 for some reason.

    Could anyone add a bit more on this subject?

  • Moderators

    Hmm... I'm not familiar with this issue, but it does look like something that could be handled better by the library itself.

    You can post to the "Development mailing list": to start a discussion with Qt's engineers.

  • My guess is the good folks at Qt aren't interested because the good folks at Qt don't use qFuzzyCompare themselves; instead, they favour a set of 'qFsk...' routines which you'll find here -


    (or somewhere like it, depending on where your Qt3d source code is)

    The routines you find there should solve your problems.

Log in to reply