3 ms·
There's nothing like hitting the source and seeing how someone has actually done it in a library in common use; let's look at one C library implementation in pa
by EliRivers 11y ago
There's nothing like hitting the source and seeing how someone has actually done it in a library in common use; let's look at one C library implementation in particular. I chose uLibC ( http://git.uclibc.org/uClibc/tree/ http://git.uclibc.org/uClibc/tree/ )
Here's the sin function:
http://git.uclibc.org/uClibc/tree/libm/s_sin.c http://git.uclibc.org/uClibc/tree/libm/s_sin.c
which looks like it handles a few special cases, and then carries out some argument reduction to map the input to the range [-pi/4,pi/4], (splitting the argument into two parts, a big part and a tail) before calling
http://git.uclibc.org/uClibc/tree/libm/k_sin.c http://git.uclibc.org/uClibc/tree/libm/k_sin.c
which then operates on those two parts.
If there is no tail, an approximate answer is generated using a polynomial of degree 13.
If there is a tail, you get a small corrective addition based on the principal that sin(x+y) = sin(x) + sin'(x')y