c ++ Sin和Cos [英] c++ Sin and Cos

查看:282
本文介绍了c ++ Sin和Cos的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的代码是:

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

DegreesToRadians()是:

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


h2_lin>解决方案

C / C ++提供 sin(a) cos(a) tan(a)等需要具有弧度单位而不是的参数的函数。 double DegreesToRadians(d)执行关闭的转换,但转换结果四舍五入后为近似值。另外,机器 M_PI 是接近的,但不是与数学非理性π相同的值。



OP的代码 180 传递给 DegreesToRadians(d),然后到 sin()/ cos()由于舍入,有限精度 double()改进之处是在之前执行自变量减少。

调用trig函数。以下将角度首先降低到-45°至45°范围,然后调用 sin()。这将确保 sind(90.0 * N) - >中的 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:
//使用* 1.0避免-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(%.1f度的sin(%)是%。* e \\\
i,DBL_DECIMAL_DIG-1,
sin(d2r(i)));
printf(%.1f degrees的sind()为%。* e \\\
,1.0 * i,DBL_DECIMAL_DIG - 1,
sind(i)
}
return 0;
}

输出



<$ p -360.0度的$ p> sin()是2.4492935982947064e-16
-360.0度的sind()是-0.0000000000000000e + 00 //精确

sin ()-345.0度是2.5881904510252068e-01 // 76-68 = 8分
// 2.5881904510252076e-01
-345.0度的sind()是2.5881904510252074e-01 // 76-74 = 2 away
$ b $ -33.0度的sin()是5.0000000000000044e-01 // 44 away
// 0.5 5.0000000000000000e-01
-330.0度的sind 4.9999999999999994e-01 // 6离开

-315.0度的sin()是7.0710678118654768e-01 // 68-52 = 16离开
//平方根0.5 - > 7.0710678118654752e-01
-315.0度的sind()是7.0710678118654746e-01 // 52-46 = 6离开

-300.0度的sin()是8.6602540378443860e-01 $ b -300.0度的$ b sind()是8.6602540378443871e-01 $ b $ -285.0度的sin()是9.6592582628906842e-01
-285.0度的sind()是9.6592582628906831e-01
-270.0度的sin()为1.0000000000000000e + 00 //精确
-270.0度的sind()为1.0000000000000000e + 00 //精确
...


I am sure this is a really stupid question, but when I pass an angle of 180 degrees into c/c++'s cos() and sin() functions I appear to receive an incorrect value. I know that it should be: sin of 0.0547 and cos of 0.99 but I get sin of 3.5897934739308216e-009 and cos of -1.00000

My code is:

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

DegreesToRadians() is:

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

Thank you :)

解决方案

C/C++ provides sin(a), cos(a), tan(a), etc. functions that require a parameter with radian units rather than degrees. double DegreesToRadians(d) performs a conversion that is close but an approximate as the conversion results are rounded. Also machine M_PI is close, but not the same value as the the mathematical irrational π.

OP's code with 180 passed to DegreesToRadians(d) and then to sin()/cos() gives results that differ than expected due to rounding, finite precision of double() and possible a weak value for PI.

An improvement is to perform argument reduction in degrees before calling the trig function. The below reduces the angle first to a -45° to 45° range and then calls sin(). This will insure that large values of N in sind(90.0*N) --> -1.0, 0.0, 1.0. . Note: sind(360.0*N +/- 30.0) may not exactly equal +/-0.5. Some additional considerations needed.

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

Output

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
...

这篇关于c ++ Sin和Cos的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆