2015-07-30 3 views
2

Я создал некоторые компоненты и вспомогательные методы с использованием C++ и Qt, которые будут использоваться в моем QML-графическом интерфейсе. Некоторые из этих методов вычисляют элементы GUI (размер, преобразование, создание элементов графика сцены). Целью является бит 32 ARM с использованием OpenGl/Eglfs для вывода результата.Использование float или double для собственных классов QML

OpenGl использует float, QML использует real (определяется как double).

Что следует использовать в C++ для не потерять любую точность?

+3

Вы можете найти описание [здесь] (http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#basic-qt-data-types) –

ответ

3

Если вы используете qreal (double), то вы не должны терять точность. Если вы git grep float в qtbase.git, вы можете узнать немного больше:

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. 

Интересно отметить, что в Qt, есть большие куски кода, которые перешли на использование только плавать:

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. 

Как говорится, это должно было избежать неожиданной потери точности, поскольку функции принимали qreal (double), но сохраняли свои данные как float. Если вы используете qreal везде в своем собственном коде, у вас не будет этой проблемы.

Другая причина - выступление. Я не могу слишком много комментировать это, но я бы сказал, что для QT более важно оптимизировать такие вещи, как для пользователей пользователей Qt.

Смежные вопросы