March 5, 2009

Support for ‘long double’ in Qt tests.

by orzel
Categories: Gentoo, KDE
Tags: ,
Comments: 3 Comments

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.

[cpp]
namespace QTest {

template <>

inline char *toString(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 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);

}

}
[/cpp]


3 Comments »

  1. Anand Rangarajan says:

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

  2. Ilkka Prusi says:

    That extended floating point (80-bit, long double) is not really supported on all platforms and compilers.
    Many CPUs does math in extended type internally but compilers such as MSVC does not support it. Rather, it’s truncated silently by compiler to “double” from “long double”..
    Also on ARM Qt seems to use 32-bit float instead of 64-bit double for performance reasons -> not much cross-platform use in supporting long double.

    I agree that support for it would be nice but I don’t see it coming anytime soon anyway, too many problems with different platforms.

  3. Fortunately, Qt does not support only those features that are available on all platforms. Of course, i see no use of ‘long double’ on ARM. But still, this exists and is very useful.

Leave a Reply

Your email address will not be published. Required fields are marked *