sin
Syntax:
#include <cmath> double sin( double arg );
The function sin returns the sine of arg
, where arg
is given in radians. The
return value of sin will be in the range [-1,1]. If arg
is infinite, sin
will return NAN and raise a floating-point exception.
One possible way to approximate the sine function using the Taylor series takes advantage of the fact that sin(x) = x - x3/3! + x5/5! - x7/7! + …, yielding the following code:
long factrl(int n) { long la = 1; for( int i = 2; i <= n; i++ ) la *= i; return la; } float sin2(float x) { int i; float y=x ,r=x; for( int i=0; i < 10; i++ ) { y *= -x*x; r += 1.0 / factrl( 1+2*(i+1) ) * y; } return r; } float sin(float theta) { float sign = 1, x = theta/M_PI; if (x < 0.0) { sign = -1; x = -x; } int i = static_cast<int>(x+0.5); float a = x-i; if( (i-i/2*2) != 0 ) sign = -sign; return sign * sin2(a*M_PI); }
Related Topics: acos, asin, atan, atan2, cos, cosh, sinh, tan, tanh