【发布时间】:2014-11-18 19:38:40
【问题描述】:
我写了这个程序来计算一个数字的正弦值,当我在 x 值上输入值 1.57 和在容差值上输入 0.00005 时,它应该说:“1.57 的正弦值是 1.000003。”,但相反它打印:“1.57 的罪是 0.000000。”我尝试在所有公式上添加随机浮点数(如乘以 1.0),但没有。如果有帮助,我正在 Linux 环境中运行 clang 编译器。任何帮助将不胜感激
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int fatorial(int n){
int f=1, t;
for(t=n;t>1;t--){
f=f*t*1;
}
return f;
}
float sen(float x, float tol){
float res=0, aux=0, auxi;
int n=0;
for(n=1;auxi>tol; n++){
res=res+(pow(-1,n+1))*((pow(x*1,2*n-1))/fatorial(2*n-1));
aux=res;
if((aux-res)>0){
auxi=aux-res;
}
else{
auxi=res-aux;
}
}
return res;
}
int main(){
float yo=0, tol, res;
printf("What's the value of x? ");
scanf(" %f", &yo);
printf("What's the value of the tolerance? ");
scanf(" %f", &tol);
res=sen(yo, tol);
printf("The sin of %.2f is %f.\n", yo, res);
return 0;
}
【问题讨论】:
-
您永远不会在
sen函数中输入for循环,因为您检查auxi是否大于tol,但辅助永远不会初始化!此外,for 循环中的n对条件没有意义 -
uau...我现在觉得...可笑...谢谢您的帮助-_-
-
很高兴能帮到你! (顺便说一句:做出了回答)
-
尽管有问题(geddit!)我要感谢您告诉我们您尝试了什么以及您在哪个平台上运行。这通常很重要,但在您的情况下并不重要!我们都去过那里。没有什么可耻的!
-
这个 printf 格式转换器:%.2f 只会打印小数点后的 2 位,因此它永远不会打印:'0.000000',而是会打印:'0.00' 格式转换器,供您使用期望的输出是:%8.6f