计算余弦算法
我创建了这个函数CalculateCos
:
int Factorial (long int n)
{
long int r = 1;
for (int i = 2; i<=n; i++)
{
r = r*i;
}
return r;
}
float CalculateVariable(int CVnumber, int CVloopCounter)
{
float CVresult = 0;
CVresult = pow(CVnumber, (CVloopCounter*2)) / (long int)Factorial(CVnumber*2);
return CVresult;
}
float CalculateCos(int number)
{
float result = 1;
int loopCounter = 1;
int minusOrPlus = 1;
while(loopCounter <= precision && loopCounter <= 8)
{
if(!minusOrPlus)
{
result = result - CalculateVariable(number, loopCounter);
printf("%f\n", result);
minusOrPlus = 1;
}
else
{
result = result + CalculateVariable(number, loopCounter);
printf("%f\n", result);
minusOrPlus = 0;
}
loopCounter++;
}
return result;
}
我在减法或加法之后 printf 的原因是因为它给了我奇怪的输出,例如:
Enter a number, for the cos function
6
1.000000
0.999997
1.000095
0.996588
1.122822
-3.421593
160.177368
-5729.385254
Result is: -5729.3852539
Official function result is: 0.9601703
你能帮我得到正确的结果吗?
更新:
现在我的解决方案是:
float CalculateCos(float number)
{
float result = 0;
float step = 1;
int loopCounter = 1;
while(loopCounter <= 5)
{
step = step * (-number) * number / (((2*loopCounter)-1)*((2*loopCounter)-2));
result += step;
loopCounter++;
}
return result;
}
I created this function CalculateCos
:
int Factorial (long int n)
{
long int r = 1;
for (int i = 2; i<=n; i++)
{
r = r*i;
}
return r;
}
float CalculateVariable(int CVnumber, int CVloopCounter)
{
float CVresult = 0;
CVresult = pow(CVnumber, (CVloopCounter*2)) / (long int)Factorial(CVnumber*2);
return CVresult;
}
float CalculateCos(int number)
{
float result = 1;
int loopCounter = 1;
int minusOrPlus = 1;
while(loopCounter <= precision && loopCounter <= 8)
{
if(!minusOrPlus)
{
result = result - CalculateVariable(number, loopCounter);
printf("%f\n", result);
minusOrPlus = 1;
}
else
{
result = result + CalculateVariable(number, loopCounter);
printf("%f\n", result);
minusOrPlus = 0;
}
loopCounter++;
}
return result;
}
The reason why I printf after the subtraction or adding, is because it gives me strange output, like:
Enter a number, for the cos function
6
1.000000
0.999997
1.000095
0.996588
1.122822
-3.421593
160.177368
-5729.385254
Result is: -5729.3852539
Official function result is: 0.9601703
Can you help me to get correct results on this?
UPDATE:
Now my solution is:
float CalculateCos(float number)
{
float result = 0;
float step = 1;
int loopCounter = 1;
while(loopCounter <= 5)
{
step = step * (-number) * number / (((2*loopCounter)-1)*((2*loopCounter)-2));
result += step;
loopCounter++;
}
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当前问题:
由于您的 Factorial 函数返回 int 并且将其转换为 long int ,因此即使在输入之前,其结果也会溢出
16
在你的情况下(14!> max_int)。您正在使用泰勒级数计算
cos
:我不会写代码。但是您的程序中存在一些错误,可以轻松修复:
number
应该是float
。float
变量:step = 1
首先,在第 k 次循环迭代中step = step * (- x) * x / ((2*k-1)*(2*k))
。这样,您只需在循环中将step
添加到result
中,就不再需要minusOrPlus
了。8
为界,该值太小,因此结果可能不够精确。abs(step)
abs(step)
abs(step)
abs(step)
abs(step) precision
,我们将终止循环。Current problem:
since your
Factorial
function returnsint
and you casts it tolong int
, its result is going to overflow even before the input goes to16
in your case (14! > max_int).You're calculating
cos
using Taylor series:I'm not going to write code. But there are some things wrong in your program, which can be fixed easily:
number
should be afloat
.float
variable:step = 1
at first and in kth loop iterationstep = step * (- x) * x / ((2*k-1)*(2*k))
. In this way, you simply addstep
toresult
in the loop and don't needminusOrPlus
anymore.8
which is too small, so the result could be not precise enough.precision
variable anywhere. It could be used to check precision of the result. For example, whenabs(step) < precision
, we're going to terminate the loop.