qFastSin和qFastCos(速度,安全性和精度) [英] qFastSin and qFastCos (Speed, safety and precision)
问题描述
最近,我在 qmath.h
中找到了两个数学函数,分别称为 qFastSin
和 qFastCos
.这些函数是 inline
,并使用查找表来计算sin和cos的值:
Recently I found two mathematical functions in qmath.h
named qFastSin
and qFastCos
. These functions are inline
and uses look-up tables to calculate the value of sin and cos:
inline qreal qFastSin(qreal x)
{
// Calculating si would be more accurate with qRound, but slower.
int si = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI));
qreal d = x - si * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
int ci = si + QT_SINE_TABLE_SIZE / 4;
si &= QT_SINE_TABLE_SIZE - 1;
ci &= QT_SINE_TABLE_SIZE - 1;
return qt_sine_table[si] + (qt_sine_table[ci] - 0.5 * qt_sine_table[si] * d) * d;
}
inline qreal qFastCos(qreal x)
{
// Calculating ci would be more accurate with qRound, but slower.
int ci = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI));
qreal d = x - ci * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
int si = ci + QT_SINE_TABLE_SIZE / 4;
si &= QT_SINE_TABLE_SIZE - 1;
ci &= QT_SINE_TABLE_SIZE - 1;
return qt_sine_table[si] - (qt_sine_table[ci] + 0.5 * qt_sine_table[si] * d) * d;
}
我在Google和Qt-Assistant中搜索了有关它们的信息,但是没有很好的文献记录.
I searched Google and Qt-Assistant for information about them, but there is no good documentaion.
有人知道这些功能的精度和性能吗?(特别精度)
Does anybody know about precision and performance of these function? (Specially precision)
推荐答案
它们不是公共API的一部分,不受支持,未记录且可能会更改.
They are not part of the public API, not supported, not documented, and subject to change.
Qt仅记录其支持的内容,而仅支持其记录的内容.这样很好.
Qt only documents what it supports and it only supports what it documents. It's good like that.
它看起来像一个简单的线性插值,所以精度取决于 QT_SINE_TABLE_SIZE
以及输入恰好接近采样点的程度.然后,最坏情况的错误将是 1-sin(pi/2 + 2 * pi *(QT_SINE_TABLE_SIZE/2))
It looks like a simple linear interpolation so accuracy depends on QT_SINE_TABLE_SIZE
and also how close to a sample point the input happens to be. The worse case error will then be 1-sin(pi/2 + 2*pi*(QT_SINE_TABLE_SIZE/2))
如果您更关心性能而不是准确性,则可以在实践中使用它们,但从理论上讲,将来它们可能会完全删除.
If you care about performance more than accuracy then you can use them in practice but in theory they may be removed entirely in future.
这篇关于qFastSin和qFastCos(速度,安全性和精度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!