Sin/Cos-Tables faster? How..

Started by
12 comments, last by Austrian Coder 21 years, 10 months ago
quote:Original post by Coward
Well, could anyone help me out on the interpolation?

Parametric:
Given that you have an angle value t between angles t0 and t1 for which the sine/cosine values are known (ie, entries in your table).

sin(t) = [(1-t) * sin(t0)] + [t * sin(t1)]

How do we know for sure this works without trying all possible values? Sine and Cosine are continuous functions with lower and upper bounds of -/+ 1 in Cartesian coordinates. The reasoning for the above is that as t grows (ie, approaches t1) there should be less of an influence on the sine of t exerted by t0.
Advertisement
quote:
sin(t) = [(1-t) * sin(t0)] + [t * sin(t1)]

Unfortunaetly this doesn''t work! (try it)
Something more like
                   (             sin(t1) - sin(t0)    )sin(t) = sin(t0) + ( (t - t0) *  -------------------  )                   (                 t1  -  t0        ) 

should do the trick.
All you need is one table from 0° to 90° - all the other quadrants are reflections of each other.

You could even use the same table for both cos & sin, as they are the same wave 90° out-of-phase. (i.e. build a sine table, add 90° to the cosine value, and look it up in the sine table).

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
also any sin/cos table should be a power of two so a simple logical and will handle wrapping the angle you use. also arrays getting larger then 2048 or maybe 1024 (i dont have a good rule of thumb for this since i have not done much benchmarking, its very possible 4096 or even 8192 be fine, though a table that is 36000 will definatly be slow especially since mod is being used) will start to degrade performence significently if used as a lookup table. lookup tables should be small otherwise you will screw with the onboard cpu cache and things will slow down more then just calling the sin/cos function. its common misconception that lookup tables make things faster. it only gets faster if you dont trade one bottleneck 9ie calculating sin/cos) for another (ie memory bandwidth/cache thrashing).

it would be beneficial to even just go all out use fixed point math. though only if your using the values and frequently ocnverting to ints (ie perpixel/buffer type stuff, maybe for creating dynamic height maps or something or a plasma).

This topic is closed to new replies.

Advertisement