简单的三角函数算法,用于计算定点数的 sin() 和 cos()
Simple trigonometric algorithm to compute sin() and cos() of fixed-point numbers
正如我在其他问题中所展示的,我目前正在实现一个C++元编程库,其中包括一组用于编译时算术的类型和元函数。
我现在的目标是为我的不动点类型实现三角函数sin
和cos
。
我的问题是,我找到的每篇关于三角算法的论文都谈到了CORDIC或某种泰勒级数。CORDIC 的问题在于它需要通过查找表预先计算出大量值,而我无法轻松地为 tmp 提供它。此外,CORDIC 的重点是计算没有乘法器的硬件中的三角函数,我完全可以使用我的库进行乘法。
所以我的问题是:除了CORDIC和泰勒级数之外,还有其他简单的替代方案来计算三角函数吗?
最后,
我通过泰勒级数实现了sin
元函数,默认使用 10 个项的系列(可配置)。我在这篇有趣的文章中基于我的实现。
我的库包括使用迭代器和表达式模板的 tmp for 循环的实现,以允许以"清晰"的方式编写复杂的表达式(与常见的模板元编程语法相比清晰add<mul<sub<1,2>>>
......这允许我从字面上复制粘贴本文提供的 C 实现:
template<typename T , typename TERMS_COUNT = mpl::uinteger<4>>
struct sin_t;
template<typename T , typename TERMS_COUNT = mpl::uinteger<4>>
using sin = typename sin_t<T,TERMS_COUNT>::result;
/*
* sin() function implementation through Taylor series (Check http://www10.informatik.uni-erlangen.de/~pflaum/pflaum/ProSeminar/meta-art.html)
*
* The C equivalent code is:
*
* // Calculate sin(x) using j terms
* float sine(float x, int j)
* {
* float val = 1;
*
* for (int k = j - 1; k >= 0; --k)
* val = 1 - x*x/(2*k+2)/(2*k+3)*val;
*
* return x * val;
* }
*/
template<mpl::fpbits BITS , mpl::fbcount PRECISION , unsigned int TERMS_COUNT>
struct sin_t<mpl::fixed_point<BITS,PRECISION>,mpl::uinteger<TERMS_COUNT>>
{
private:
using x = mpl::fixed_point<BITS,PRECISION>;
using begin = mpl::make_integer_backward_iterator<TERMS_COUNT-1>;
using end = mpl::make_integer_backward_iterator<-1>;
using one = mpl::decimal<1,0,PRECISION>;
using two = mpl::decimal<2,0,PRECISION>;
using three = mpl::decimal<3,0,PRECISION>;
template<typename K , typename VAL>
struct kernel : public mpl::function<decltype( one() - ( x() * x() )/(two() * K() + two())/(two()*K()+three())*VAL() )> {};
public:
using result = decltype( x() * mpl::for_loop<begin , end , one , kernel>() );
};
下面是项目存储库中实现的标头。
通过查找表预先计算的大量值集
有多少是"巨大的"? 听起来像是一次性的努力,一旦你完成,就会像地狱一样快。 我的建议? 拿一把铲子,把那张桌子填满。 当你在这里得到另一个答案时,你已经完成了它。
相关文章:
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 未解析的外部符号 fabs, cos, sin.
- sin cos 函数在大于 90 的度数时给出错误的结果
- 用C 创建SIN/COS图
- 使用SIN和COS函数迭代时,NAN会导致
- 我的神经网络学习sin x,但不学习cos x
- C 程序创建SIN COS和切线值
- cos 和 sin 不提供预期的输出
- 将 cos 和 sin 存储到一个向量中并获得奇怪的值
- 泰勒级数 在sin(90)和cos(120)之后得到nan
- 简单的三角函数算法,用于计算定点数的 sin() 和 cos()
- 在表中预先计算一次cos()和sin()
- sin() cos() ring formula
- 在 glm::rotate 中使用 "custom" sin 和 cos
- 关于cos / sin的矩阵数学
- 为什么在启用优化时sin/cos会变慢
- Sin和cos图表绘图仪…c++的一些算法错误
- 对于已知的角度Sin和Cos给出了意想不到的结果
- 求解cos()和sin()中的变量