Fast implementation of trigonometric functions for c++

后端 未结 9 1820
孤城傲影
孤城傲影 2020-12-07 19:24

Short version: I\'d like to know whether there are implementations of the standard trigonometric functions that are faster than the ones included in math.h.

9条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 19:52

    I've implemented a fast sine function on cpu side which is at least two times faster than math.h ' s sine function however I used a very small lookup table(20 floats). it's accuracy is also not bad at all; average relative error rate is 0.095%. you can check it out from http://www.hevi.info/tag/fast-sine-function/

    Explanation of the method is quite simple and relies on the fact that for small a's sin(a) = a * pi / 180 (see the link above for the proof)

    enter image description here

    Some Trigonometry

    Although it is possible to achieve relatively accurate results with the formula shown above for angles between 0 and 10, as the angle gets wider as it loses accuricy. Therefore we should use the formula for angles less than 10 but how?!

    The answer comes from the trigonometric sine addition formula;

    sin(a+b) = sin(a) cos(b) + sin(b) cos(a)

    If we can keep the ‘b’ less than 10 then we will be able to use our formula in order to find the sine with a couple of aritchmetic operations.

    Let’s say we are asked the sine value for 71.654, then;

    a = 70

    b = 1.654

    and,

    sin(71.654) = sin(70 + 1.654) = sin(70) cos(1.654) + sin(1.654) cos (70)

    In this formula we are able to use the fast calculation for the sin(1.654) part and for the rest unfortunately we need to have sine and cosine tables. The good thing is we only need the multiply of tens for sine and natural number angles between 0 and 10 for cosine.

提交回复
热议问题