Create sine lookup table in C++

后端 未结 6 1270
庸人自扰
庸人自扰 2020-12-13 15:27

How can I rewrite the following pseudocode in C++?

real array sine_table[-1000..1000]
    for x from -1000 to 1000
        sine_table[x] := sine(pi * x / 100         


        
6条回答
  •  囚心锁ツ
    2020-12-13 15:54

    One more point: calling trigonometric functions is pricey. if you want to prepare the lookup table for sine with constant step - you may save the calculation time, in expense of some potential precision loss.

    Consider your minimal step is "a". That is, you need sin(a), sin(2a), sin(3a), ...

    Then you may do the following trick: First calculate sin(a) and cos(a). Then for every consecutive step use the following trigonometric equalities:

    • sin([n+1] * a) = sin(n*a) * cos(a) + cos(n*a) * sin(a)
    • cos([n+1] * a) = cos(n*a) * cos(a) - sin(n*a) * sin(a)

    The drawback of this method is that during this procedure the round-off error is accumulated.

提交回复
热议问题