Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 数学中的函数.h don';t返回期望值_C - Fatal编程技术网

C 数学中的函数.h don';t返回期望值

C 数学中的函数.h don';t返回期望值,c,C,我正在编写的程序应该计算以下函数: f(x,y)= 2 sin(x) + cos(y) - tg(x+y) 我试着做到以下几点: #include<stdio.h> #include<math.h> double toDegrees(double radians){ return radians * (180.0 / M_PI); } int main(void){ double x,y; printf("What's the value of x?\n

我正在编写的程序应该计算以下函数:

f(x,y)= 2 sin(x) + cos(y) - tg(x+y)
我试着做到以下几点:

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

double toDegrees(double radians){
  return radians * (180.0 / M_PI);
}

int main(void){
  double x,y;
  printf("What's the value of x?\n");
  scanf("%lf", &x);
  printf("What's the value of y?\n");
  scanf("%lf", &y);
  printf("The values are %lf\n", toDegrees(2*sin(x)+cos(y)-tan(x+y)));
  return 0;
}
#包括
#包括
双到度(双弧度){
返回弧度*(180.0/M_-PI);
}
内部主(空){
双x,y;
printf(“x的值是多少?\n”);
扫描频率(“%lf”、&x);
printf(“y的值是多少?\n”);
扫描频率(“%lf”、&y);
printf(“这些值是%lf\n”,toDegrees(2*sin(x)+cos(y)-tan(x+y));
返回0;
}
toDegrees函数将函数的默认输出从math.h从弧度转换为度

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

double toRadians(double degrees){
  return degrees * (M_PI / 180.0);
}

int main(void){
  double x,y;
  printf("What's the value of x?\n");
  scanf("%lf", &x);
  printf("What's the value of y?\n");
  scanf("%lf", &y);
  printf("The values are %lf\n", 2*sin(toRadians(x))+cos(toRadians(y))+tan(toRadians(x+y)));
  return 0;
}
不带度数功能的预期输出(弧度)为
-2.07746705002370603998583034482545686045261881310920233482

这确实是产出

函数toDegrees的预期输出(以度为单位)为
1.881737400858622861572140474303286479652718537288463725776

但是,输出为
-119.030094

我期望的输出是我在中使用
x=10
y=21
得到的输出

为什么会发生这种情况?我如何修复它


我确实把-lm放在了编译中。

这不是一个编程错误,而是一个数学错误:trig函数的输入是以度或弧度表示的角度,而不是输出。此外,您的转换方法是错误的:您希望将度转换为弧度,而不是将弧度转换为度

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

double toRadians(double degrees){
  return degrees * (M_PI / 180.0);
}

int main(void){
  double x,y;
  printf("What's the value of x?\n");
  scanf("%lf", &x);
  printf("What's the value of y?\n");
  scanf("%lf", &y);
  printf("The values are %lf\n", 2*sin(toRadians(x))+cos(toRadians(y))+tan(toRadians(x+y)));
  return 0;
}
#包括
#包括
双环面(双度){
返回度*(M_PI/180.0);
}
内部主(空){
双x,y;
printf(“x的值是多少?\n”);
扫描频率(“%lf”、&x);
printf(“y的值是多少?\n”);
扫描频率(“%lf”、&y);
printf(“值为%lf\n”,2*sin(toRadians(x))+cos(toRadians(y))+tan(toRadians(x+y));
返回0;
}

在这两个错误都已修复的情况下,为x输入
10
,为y输入
21
,将正确返回所需的
1.881737

这不是编程错误,而是数学错误:是以度或弧度为单位的三角函数的输入,而不是输出。此外,您的转换方法是错误的:您希望将度转换为弧度,而不是将弧度转换为度

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

double toRadians(double degrees){
  return degrees * (M_PI / 180.0);
}

int main(void){
  double x,y;
  printf("What's the value of x?\n");
  scanf("%lf", &x);
  printf("What's the value of y?\n");
  scanf("%lf", &y);
  printf("The values are %lf\n", 2*sin(toRadians(x))+cos(toRadians(y))+tan(toRadians(x+y)));
  return 0;
}
#包括
#包括
双环面(双度){
返回度*(M_PI/180.0);
}
内部主(空){
双x,y;
printf(“x的值是多少?\n”);
扫描频率(“%lf”、&x);
printf(“y的值是多少?\n”);
扫描频率(“%lf”、&y);
printf(“值为%lf\n”,2*sin(toRadians(x))+cos(toRadians(y))+tan(toRadians(x+y));
返回0;
}

修复这两个错误后,为x输入
10
,为y输入
21
,将正确返回所需的
1.881737

它们的默认输出不是弧度吗?这就是为什么我将它们转换为弧度它们的默认输入是弧度,所以需要将值(即度)转换为弧度,然后将它们传递给函数。输出既不是度也不是弧度,因为它不再表示角度,而是一个比率;我弄糊涂了,谢谢!它们的默认输出不是弧度吗?这就是为什么我将它们转换为弧度它们的默认输入是弧度,所以需要将值(即度)转换为弧度,然后将它们传递给函数。输出既不是度也不是弧度,因为它不再表示角度,而是一个比率;我弄糊涂了,谢谢!旁注:在顶部你写了
-tg(x+y)
,但是在你的代码中你有
+tan(x+y)
。哦,捕捉得好!非常感谢。旁注:在顶部你写了
-tg(x+y)
,但是在你的代码中你有
+tan(x+y)
。哦,捕捉得好!非常感谢。