2
votes

I've created some components and helper methods using C++ and Qt to be used in my QML GUI. Some of those methods calculate GUI elements (size, transformations, scene graph items creation). The target is an ARM 32 Bit using OpenGl / Eglfs to render the output.

OpenGl is using float, QML is using real (defined as double).

What should I use in C++ to do not lose any precision?

1
You can find description here - t3ft3l--i

1 Answers

3
votes

If you use qreal (double) then you shouldn't lose any precision. If you git grep float in qtbase.git, you can find out a bit more:

dist/changes-5.2.0-****************************************************************************
dist/changes-5.2.0-*                   Important Behavior Changes                             *
dist/changes-5.2.0-****************************************************************************
dist/changes-5.2.0-
dist/changes-5.2.0- - Qt is now compiled with qreal typedef'ed to double on all
dist/changes-5.2.0:   platforms. qreal was a float on ARM chipsets before. This guarantees more
dist/changes-5.2.0-   consistent behavior between all platforms Qt supports, but is binary
dist/changes-5.2.0-   incompatible to Qt 5.1 on ARM. The old behavior can be restored by
dist/changes-5.2.0:   passing -qreal float to configure.

It's interesting to note that within Qt, there are large pieces of code that have switched to using only float:

commit 51d40d7e9bdfc63c5109aef5b732aa2ba10f985a
Author: Sean Harmer <[email protected]>
Date:   Mon Aug 20 20:55:40 2012 +0100

    Make gui/math3d classes use float rather than qreal

    This corrects the mismatch between using floats for internal storage
    and qreal in the API of QVector*D which leads to lots of implicit
    casts between double and float.

    This change also stops users from being surprised by the loss of
    precision when using these classes on desktop platforms and removes
    the need for the private constructors taking a dummy int as the final
    argument.

    The QMatrix4x4 and QQuaternion classes have been changed to use float
    for their internal storage since these are meant to be used in
    conjunction with the QVector*D classes. This is to prevent unexpected
    loss of precision and to improve performance.

    The on-disk format has also been changed from double to float thereby
    reducing the storage required when streaming vectors and matrices. This
    is potentially a large saving when working with complex 3D meshes etc.

    This also has a significant performance improvement when passing
    matrices to QOpenGLShaderProgram (and QGLShaderProgram) as we no
    longer have to iterate and convert the data to floats. This is
    an operation that could easily be needed many times per frame.

    This change also opens the door for further optimisations of these
    classes to be implemented by using SIMD intrinsics.

As it says, this was to avoid surprise loss of precision as the functions took qreal (double) but stored their data as float. If you use qreal everywhere in your own code, you won't have this problem.

The other reason was performance. I can't comment too much on that, but I'd say that it's more important for Qt to optimise for such things than it is for most users of Qt.