Sin和Cos为众所周知的角度带来意想不到的结果

时间:2015-07-19 14:14:05

标签: c++ sin cmath cos

我确信这是一个非常愚蠢的问题,但是当我向c / c ++的cos()和sin()函数传递180度的角度时,我似乎收到了一个不正确的值。我知道它应该是: sin为0.0547,cos为0.99 但我得到了3.5897934739308216e-009的罪和-1.00000的cos

我的代码是:

double radians = DegreesToRadians( angle );
double cosValue = cos( radians );
double sinValue = sin( radians );

DegreesToRadians()是:

double DegreesToRadians( double degrees )
{ 
    return degrees * PI / 180; 
} 

谢谢:)

3 个答案:

答案 0 :(得分:9)

首先,180度的余弦应该等于-1,所以你得到的结果是正确的。

其次,在使用sin/cos/tan等函数时,有时无法获得完全值,因为您总是得到最接近正确的结果。在您的情况下,您从sin获得的值最接近于零。

您获得的sin(PI)的值仅在浮点后的第9 (!)位数与零不同。 3.5897934739308216e-009几乎等于0.000000004,几乎等于零。

答案 1 :(得分:9)

C / C ++提供sin(a)cos(a)tan(a)等需要参数 radian 单位而不是double DegreesToRadians(d)执行 close 的转化,但转化结果已四舍五入。机器M_PI也很接近,但与数学无理π的值不同。

OP的代码180传递给DegreesToRadians(d)然后传递给sin()/cos()会产生与预期不同的结果,因为舍入,double()的有限精度和可能的弱值PI

改进是在调用trig函数之前在中执行参数减少。下面将角度首先降低到-45°到45°范围,然后调用sin()。这样可以确保Nsind(90.0*N) --> -1.0, 0.0, 1.0的值很大。 。注意:sind(360.0*N +/- 30.0)可能不完全等于+/-0.5。需要一些额外的考虑。

#include <math.h>
#include <stdio.h>

static double d2r(double d) {
  return (d / 180.0) * ((double) M_PI);
}

double sind(double x) {
  if (!isfinite(x)) {
    return sin(x);
  }
  if (x < 0.0) {
    return -sind(-x);
  }
  int quo;
  double x90 = remquo(fabs(x), 90.0, &quo);
  switch (quo % 4) {
    case 0:
      // Use * 1.0 to avoid -0.0
      return sin(d2r(x90)* 1.0);
    case 1:
      return cos(d2r(x90));
    case 2:
      return sin(d2r(-x90) * 1.0);
    case 3:
      return -cos(d2r(x90));
  }
  return 0.0;
}

int main(void) {
  int i;
  for (i = -360; i <= 360; i += 15) {
    printf("sin()  of %.1f degrees is  % .*e\n", 1.0 * i, DBL_DECIMAL_DIG - 1,
        sin(d2r(i)));
    printf("sind() of %.1f degrees is  % .*e\n", 1.0 * i, DBL_DECIMAL_DIG - 1,
        sind(i));
  }
  return 0;
}

输出

sin()  of -360.0 degrees is   2.4492935982947064e-16
sind() of -360.0 degrees is  -0.0000000000000000e+00  // Exact

sin()  of -345.0 degrees is   2.5881904510252068e-01  // 76-68 = 8 away
//                            2.5881904510252076e-01
sind() of -345.0 degrees is   2.5881904510252074e-01  // 76-74 = 2 away

sin()  of -330.0 degrees is   5.0000000000000044e-01  // 44 away
//  0.5                       5.0000000000000000e-01
sind() of -330.0 degrees is   4.9999999999999994e-01  //  6 away

sin()  of -315.0 degrees is   7.0710678118654768e-01  // 68-52 = 16 away
// square root 0.5 -->        7.0710678118654752e-01
sind() of -315.0 degrees is   7.0710678118654746e-01  // 52-46 = 6 away

sin()  of -300.0 degrees is   8.6602540378443860e-01
sind() of -300.0 degrees is   8.6602540378443871e-01
sin()  of -285.0 degrees is   9.6592582628906842e-01
sind() of -285.0 degrees is   9.6592582628906831e-01
sin()  of -270.0 degrees is   1.0000000000000000e+00  // Exact
sind() of -270.0 degrees is   1.0000000000000000e+00  // Exact
...

答案 2 :(得分:5)

将app转换为64位时,我遇到与OP相同的问题 我的解决方案是使用新的math.h函数__cospi()和__sinpi() 性能与cos()和sin()相似(甚至快1%)。

//    cos(M_PI * -90.0 / 180.0)   returns 0.00000000000000006123233995736766
//__cospi(       -90.0 / 180.0)   returns 0.0, as it should
// #define degree2rad 3.14159265359/180
// #define degree2rad M_PI/ 180.0
// double rot = -degree2rad * ang;
// double sn = sin(rot);
// double cs = cos(rot);

double rot = -ang / 180.0;
double sn = __sinpi(rot);
double cs = __cospi(rot);

从math.h:

/*  __sinpi(x) returns the sine of pi times x; __cospi(x) and __tanpi(x) return
the cosine and tangent, respectively.  These functions can produce a more
accurate answer than expressions of the form sin(M_PI * x) because they
avoid any loss of precision that results from rounding the result of the
multiplication M_PI * x.  They may also be significantly more efficient in
some cases because the argument reduction for these functions is easier
to compute.  Consult the man pages for edge case details.                 */