# 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 on other sites
ApochPiQ    23064
Why do you think the existing library functions are inadequate?

##### Share on other sites
Decrius    100
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 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 on other sites
Evil Steve    2017
Quote:
 Original post by Lord_EvilIn 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 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 on other sites
theOcelot    498
Quote:
 Original post by alvaroThe obvious options are: * the built-in functions * look-up tables * polynomial approximations * CORDIC implementations

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

##### Share on other sites
alvaro    21266
Quote:
Original post by theOcelot
Quote:
 Original post by alvaroThe 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.