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

前端 未结 15 2018
执笔经年
执笔经年 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:39

    The fastest way is to pre-compute values an use a table like in this example:

    Create sine lookup table in C++

    BUT if you insist upon computing at runtime you can use the Taylor series expansion of sine or cosine...

    Taylor Series of sine

    For more on the Taylor series... http://en.wikipedia.org/wiki/Taylor_series

    One of the keys to getting this to work well is pre-computing the factorials and truncating at a sensible number of terms. The factorials grow in the denominator very quickly, so you don't need to carry more than a few terms.

    Also...Don't multiply your x^n from the start each time...e.g. multiply x^3 by x another two times, then that by another two to compute the exponents.

提交回复
热议问题