Fast, inaccurate sin function without lookup

时光毁灭记忆、已成空白 提交于 2019-12-05 11:34:39

Use a Chebyshev approximation for as many terms as you need. This is particularly easy if your input angles are constrained to be well behaved (-π .. +π or 0 .. 2π) so you do not have to reduce the argument to a sensible value first. You might use 2 or 3 terms instead of 9.

You can make a look-up table with sin values for some values and use linear interpolation between that values.

A rational algebraic function approximation to sin(x) is:

f = (C1 * x) / (C2 * x^2 + 1.) 

with the constants:

c1 =   1.043406062 
c2 =  .2508691922 

These constants were found with a least-squares subroutine, DHFTI, by Lawson & Hanson. This is valid only over 0 to π / 2, so reduce the input with something like:

IF (t  < pi) THEN
  IF (t < pi/2) THEN
    x = t
  ELSE
      x = pi - t
   END IF
 ELSE 
   IF (t < (3./2)*pi) THEN
     x = t - pi
  ELSE
     x = twopi - t
   END IF
END IF

Then calculate:

f = (C1 * x) / (C2 * x*x + 1.0)
IF (t > pi) f = -f

If the input is outside [0, 2π], you'll need to take x mod 2 π
The results should be within about 5% of the real sine.

Well, you don't say how accurate you need it to be. The sine can be approximated by straight lines of slopes 2/pi and -2/pi on intervals [0, pi/2], [pi/2, 3*pi/2], [3*pi/2, 2*pi]. This approximation can be had for the cost of a multiplication and an addition after reducing the angle mod 2*pi.

Using a lookup table is probably the best way to control the tradeoff between speed and accuracy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!