manually implementing trigonometric functions

Started by
6 comments, last by alvaro 14 years, 2 months ago
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
Visit galaxyroad.com which soon will have english contents too
Advertisement
Why do you think the existing library functions are inadequate?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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?
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
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.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
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.
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.
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...
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.

This topic is closed to new replies.

Advertisement