Fastest implementation of sine, cosine and square root in C++ (doesn't need to be much accurate)

前端 未结 15 1939
执笔经年
执笔经年 2020-12-04 10:55

I am googling the question for past hour, but there are only points to Taylor Series or some sample code that is either too slow or does not compile at all. Well, most answe

15条回答
  •  自闭症患者
    2020-12-04 11:27

    This is an implementation of Taylor Series method previously given in akellehe's answer.

    unsigned int Math::SIN_LOOP = 15;
    unsigned int Math::COS_LOOP = 15;
    
    // sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
    template 
    T Math::sin(T x)
    {
        T Sum       = 0;
        T Power     = x;
        T Sign      = 1;
        const T x2  = x * x;
        T Fact      = 1.0;
        for (unsigned int i=1; i
    T Math::cos(T x)
    {
        T Sum       = x;
        T Power     = x;
        T Sign      = 1.0;
        const T x2  = x * x;
        T Fact      = 1.0;
        for (unsigned int i=3; i

提交回复
热议问题