简单的三角算法来计算定点数的sin()和cos()

时间:2013-09-12 01:08:55

标签: c++ algorithm template-meta-programming

正如我在其他问题中所展示的,我目前正在实现一个C ++元编程库,其中包括一组用于编译时算术的类型和元函数。

我现在的目标是为我的定点类型实现三角函数sincos
我的问题是,我发现的关于三角算法的每篇论文都涉及CORDIC或某种类型的泰勒系列。 CORDIC的问题在于它需要通过查找表提供大量预先计算的值,而且我不能通过tmp提供它。另外,CORDIC的要点是在没有乘法器的硬件中计算三角函数,并且我完全可以用我的库进行乘法运算。

所以我的问题是:有没有其他简单的替代CORDIC和Taylor系列来计算三角函数?

2 个答案:

答案 0 :(得分:3)

最后,我通过Taylor系列实现了sin元函数,默认情况下使用10个术语系列(可以配置)。 我的实施基于this interesting article

我的库包含使用迭代器的tmp for循环的实现,以及expression templates to allow write complex expressions in a "clear" way(与常见的模板 - 元编程语法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>() );
};

Here是项目仓库中实现的标题。

答案 1 :(得分:2)

  

通过查找表

的大量预先计算的值

有多少是“巨大的”?听起来像是一次性的努力,一旦你完成就会很快。我的建议?拿一把铁锹填写那张桌子。当你在这里得到另一个答案时,你已经完成了它。