Update
This commit is contained in:
parent
ec4393d204
commit
8ffe198bf4
15 changed files with 1948 additions and 1899 deletions
36
sin.md
36
sin.md
|
@ -178,3 +178,39 @@ int sinInt(int x)
|
|||
return sign * (16 * x) / ((5 * PI * PI - 4 * x) / UNIT);
|
||||
}
|
||||
```
|
||||
|
||||
Mainstream way of implementing [floating point](float.md) sine (but potentially fixed point too) is through [Taylor series](taylor_series.md), i.e. with a [polynomial](polynomial.md) of order *N* that has first *N* [derivatives](derivative.md) identical to the approximated function near some given point. For *sin(x)* near *x = 0* this series is:
|
||||
|
||||
*sin(x) = x/1! - x^(3)/3! + x^(5)/5! - x^(7)/7! + ...*
|
||||
|
||||
Here is a simple implementation using fixed number of terms, which nonetheless gives quite precise results:
|
||||
|
||||
```
|
||||
double sinF(double x)
|
||||
{
|
||||
#define _PI 3.141593
|
||||
if (x < 0)
|
||||
x = -1 * x + _PI;
|
||||
|
||||
int part = (2 * x) / _PI;
|
||||
|
||||
x -= part * _PI / 2;
|
||||
|
||||
if (part % 2)
|
||||
x = _PI / 2 - x;
|
||||
#undef _PI
|
||||
|
||||
double x2 = x * x, r = x;
|
||||
|
||||
x *= x2;
|
||||
r -= x / 6;
|
||||
x *= x2;
|
||||
r += x / 120;
|
||||
x *= x2;
|
||||
r -= x / 5040;
|
||||
x *= x2;
|
||||
r += x / 362880;
|
||||
return r;
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue