• Advertisement
Sign in to follow this  

manually implementing trigonometric functions

This topic is 2936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi friends I'm using C# (VS 2008), I think Math.sin & Math.cos are not satisfying my needs, how can I implement them manually? can I use series? if so, how many expressions should I use? can I use any DLL/library? is there anything else available? thanks

Share this post


Link to post
Share on other sites
Advertisement
Yes you can. Using the exponentian number e = ~2.718281828

Take a look here: Wikipedia/Sine#Series_definitions

It shows you the series to use for the sine and cosine. I do wonder though, how are the current implementations not satisfying? Too inaccurate?

Share this post


Link to post
Share on other sites
Maybe he means that they are too slow.

In that case you might trade accuracy (and memory) for speed and use a lookup table.
You might, for example, store a precalculated sin/cos value for every 1/10 degree effectively resulting in 3600 (or 7200, if using separate sin/cos) values, which would require 14-56 KB, depending on whether you store floats or doubles.

A lookup then could be: sin(x) = sin_table[((int)((x + 0.05 )* 10)) % 3600];, i.e. you truncate all decimal digits except the first.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lord_Evil
In that case you might trade accuracy (and memory) for speed and use a lookup table.
Lookup tables are not necessarily faster. On modern CPUs, the cost of a cache miss could well far outweigh the cost of a cos or sin call. If you do use a lookup table, make sure to profile it.

Share this post


Link to post
Share on other sites
The obvious options are:
* the built-in functions
* look-up tables
* polynomial approximations
* CORDIC implementations

Chances are the built-in functions are the best of the four in most circumstances. As others have said, it's hard to recommend an alternative if you don't tell us why you are unsatisfied with them.

Oh, there is a much much better option: Don't use trigonometry at all. Many situations where you think you might need it can actually be coded without using angles, and the resulting code is often cleaner and faster. So perhaps you could tell us what you are trying to do, to see if we can think of a way to avoid the call altogether.

Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
The obvious options are:
* the built-in functions
* look-up tables
* polynomial approximations
* CORDIC implementations


I thought the built-in implementations used polynomial approximations...

Share this post


Link to post
Share on other sites
Quote:
Original post by theOcelot
Quote:
Original post by alvaro
The obvious options are:
* the built-in functions
* look-up tables
* polynomial approximations
* CORDIC implementations


I thought the built-in implementations used polynomial approximations...


I don't know exactly what it does under the hood, but by "polynomial approximations" I mean you can roll your own. According to Wikipedia, the built-in implementation uses a combination of look-up table and the add a correction using a polynomial.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement