sin

来自cppreference.com
跳转到: 导航, 搜索

语法:

    #include <cmath>
    double sin( double arg );

函数 sin() 返回 arg 的正弦,其中arg以弧度给出。sin() 的返回值将在[-1,1]范围内。如果arg是无穷大,sin()将返回NAN并抛出浮点异常。、

C++ 同样提供了下列重载形式:

    #include <cmath>
    float sin( float arg ); // same as sinf() in C99
    long double sin( long double arg ); // same as sinl() in C99

一个可能的方法是 使用泰勒级数近似正弦函数 利用 sin(x) = x - x3/3! + x5/5! - x7/7! + ... 的事实产生下列代码:

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);
}

相关主题: acos, asin, atan, atan2, cos, cosh, sinh, tan, tanh

个人工具
名字空间
操作
导航
工具箱
其他语言