正弦 180 的值是 1.22465e-16 [英] Value of sine 180 is coming out as 1.22465e-16
本文介绍了正弦 180 的值是 1.22465e-16的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 ios4 中实现一个正弦和余弦计算器:
I want to implement a Sine and Cosine calculator in ios4:
if([operation isEqual:@"sin"]){
operand = (operand*M_PI/180.0);
operand=sin(operand);
}
代码给出了从 0 到 90 值的正确答案.
The code gives me correct answer for values from 0 through 90.
当我给出 180 的值时,我得到 1.22465e-16
作为答案.我预计为零.
When I give value of 180, I get 1.22465e-16
as an answer. I expect zero.
这个小差异从何而来?
推荐答案
这是由于二进制数字系统无法准确表示 PI 造成的.
This is caused by the inability of a binary number system to exactly represent PI.
一种可能的解决方案是使用 sin 的对称性:
One possible solution would be to use the symmetry of sin:
sindeg(x) = sindeg(180 - x)
(或替代):
sin(x) = sin(M_PI - x)
将角度转换为范围 (-pi/2 : pi/2) 可减少近似误差.
Transforming the angle into the range (-pi/2 : pi/2) reduces the error of the approximation.
基本上:
if([operation isEqual:@"sin"]){
operand = fmod(operand, 360);
if (operand > 270) operand -= 360;
else if (operand > 90) operand = 180 - operand;
operand=sin(operand*M_PI/180.0);
}
这篇关于正弦 180 的值是 1.22465e-16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文