2011-10-11 58 views
1

最近我在qmath.h中发现了两个数学函数qFastSinqFastCos。这些功能是inline并使用查找表来计算sin和cos的价值:我搜索谷歌和Qt的助理有关它们的信息qFastSin和qFastCos(速度,安全和精度)

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; 
} 

,但没有很好的文件建立。

有人知道这些功能的精度和性能吗? (特别精准)

回答

7

他们不是公共API的一部分,不支持,没有记录,并可能会改变。

Qt只记录它支持的内容,并且它only supports what it documents。这很好。

它看起来像一个简单的线性插值,因此精度取决于QT_SINE_TABLE_SIZE以及输入碰巧是多么接近采样点。那么最坏的情况下误差将1-sin(pi/2 + 2*pi*(QT_SINE_TABLE_SIZE/2))

如果你关心性能超过精度那么您可以在实践中使用它们,但在理论上他们可能会在未来完全删除。