Main Contents

Support for ‘long double’ in Qt tests.

March 5, 2009

Qt supports most types for most compilers…… but long double.  I do not know why. When asking on #qt (IRC), people say that nobody seems to care. Well… I do. long double have some use in scientific software at least. If you ever need to use long double in QTest, you will need to add this somewhere in some of your headers used by unit tests.

This is tested with Qt 4.4.2 and 4.5, on linux(gcc 4.3.3) and windows (mingw/gcc 3.4.5).

Once you have this code, you can do things like QCOMPARE() between long double and so on.

namespace QTest {
    template <>
    inline char *toString<long double>(const long double &t)
    {
        char *msg = new char[128];
        ::snprintf(msg, 128, "%.123Lg", t);
        return msg;
    }
    static inline bool qFuzzyCompare(long double p1, long double p2)
    {
        return (qAbs(p1 - p2) <= 0.00000000000001 * qMin(qAbs(p1), qAbs(p2)));
    }
    template <>
    inline bool Q_TESTLIB_EXPORT qCompare<long double>(long double const &t1,
            long double const &t2, const char *actual, const char *expected, const char *file, int line)
    {
        return qFuzzyCompare(t1, t2)
            ? compare_helper(true, "COMPARE()", file, line)
            : compare_helper(false, "Compared long doubles are not the same (fuzzy compare)",
                    toString(t1), toString(t2), actual, expected, file, line);
    }
}

Filed under: Gentoo, KDE |

1 Comment

  1. Anand Rangarajan March 5, 2009 @ 3:07 pm

    Thanks. We use long double all the time in scientific computing.

Leave a comment

CAPTCHA image