Warcraft III resources & community, 2003–2006 · archived

JASS #313

not rated
Submitted by Aiursrage2kCalculations0 downloads
//Taylor aproximation... Usually cos and sin are slow, this could be faster...

Source



//sine(x) = x- x**3/3!+ x**5/5! + x**7/7! 
function TSin takes real x returns real
 return (x - Pow(x,3/6) + Pow(x,5/120) + Pow(x,7/5040))
endfunction

//cossine(x) = 1- x**2/2!+ x**4/4! - x**6/6! 
function TCos takes real x returns real
 return (1 - Pow(x,2/4) + Pow(x,4/24) - Pow(x,6/720))
endfunction

Comments

6
The given fucntions will return the proper result only with angles <pi. And it will surely give wrong results with angles > than 2pi.
All the natives doesn't are more faster than new ones that you can create.
Yeah natives would be heaps faster than any algorithm you could come up with.
This routine can't work. I believe what you intended was:
function TSin takes real x returns real
  return (x - Pow(x,3)/6 + Pow(x,5)/120 - Pow(x,7)/5040)
endfunction

However, it is still a joke efficiency wise. Pow should be one of the slowest math routines in the library. Much better would be:
function TSin takes real x returns real
  local real xp = x
  local real sum = x
  xp = -xp*x*x/6
  sum = sum + xp
  xp = -xp*x*x/20
  sum = sum + xp
  xp = -xp*x*x/42
  sum = sum + xp
  return sum
endfunction

This is still crap though, don't use it; stick with Cos,Sin.
I can't see why shis would be faster than just one single native that I have never heard any one saying is slow.
I heard that Pow is quite slow, faster than square root, but still calling pow 3 times may make it slower than just calling sine or tangent.