我使用C ++和Qt创建了一些组件和辅助方法,以便在我的QML GUI中使用。其中一些方法计算GUI元素(大小,转换,场景图项目创建)。目标是使用OpenGl / Eglfs渲染输出的ARM 32位。
OpenGl正在使用float
,QML正在使用real
(定义为double
)。
我应该在C ++中使用什么来不失去任何精度?
答案 0 :(得分: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中,有很多代码已经转换为仅使用float:
commit 51d40d7e9bdfc63c5109aef5b732aa2ba10f985a
Author: Sean Harmer <sean.harmer@kdab.com>
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的大多数用户更重要。