我想在ios4中实现一个正弦和余弦计算器:
if([operation isEqual:@"sin"]){
operand = (operand*M_PI/180.0);
operand=sin(operand);
}
Run Code Online (Sandbox Code Playgroud)
代码为我提供了从0到90的正确答案.
当我给出180的价值时,我会得到1.22465e-16答案.我期待零.
这个小差异来自哪里?
我的Math.cos()方法有点问题.我知道,我必须在使用之前将角度转换为Radians Math.cos().但如果我这样做:
System.out.println(Math.cos(Math.toRadians(90));
Run Code Online (Sandbox Code Playgroud)
输出:6.123233995736766E-17
Math.sin() 运作良好.