Sign in to follow this  
vcGamer

manually implementing trigonometric functions

Recommended Posts

vcGamer    100
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
Lord_Evil    680
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
Evil Steve    2017
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
alvaro    21266
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
theOcelot    498
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
alvaro    21266
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this