qFastSin 和 qFastCos(速度、安全性和精度) [英] qFastSin and qFastCos (Speed, safety and precision)

查看:27
本文介绍了qFastSin 和 qFastCos(速度、安全性和精度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在qmath.h 中发现了两个数学函数,名为qFastSinqFastCos.这些函数是 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.

有人知道这些函数的精度和性能吗?(特别精确)

推荐答案

它们不是公共 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆