我的代码将一个以弧度为单位的角度传递给cos
, tan
and sin
。一切似乎都工作正常,除了晒黑90
,这给出了值16331239353195370
由于某些奇怪的原因。示例代码:
import java.text.DecimalFormat;
public class mathtable {
public static void main(String[] args) {
System.out.println("Angle Sin Cos Tan");
System.out.println("----- --- --- ---");
for (double angle = 0.0; angle < 180; angle +=5) {
double angle_rad = Math.toRadians(angle);
double sin = Math.sin(angle_rad);
String sin_4 = new DecimalFormat("#.####").format(sin);
double cos = Math.cos(angle_rad);
String cos_4 = new DecimalFormat("#.####").format(cos);
double tan = Math.tan(angle_rad);
String tan_4 = new DecimalFormat("#.####").format(tan);
System.out.println(angle + " " + sin_4 + " " + cos_4 + " " + tan_4);
}
}
}
为什么返回的值不严格等于 IEEE 无穷大?
Well, tan(pi/2)
以弧度表示本质上是无限的,不是吗?所以你会期望得到一个非常大的数字,不是吗? (它不是无穷大,因为 pi/2 不能精确地表示为double
。你正在渐近曲线上找到一个值close到它变得无限的地方。)
See 这些 sin/cos/tan 图表 http://www.bbc.co.uk/schools/gcsebitesize/maths/algebra/furthergraphhirev5.shtml明白我的意思,记住 pi/2 弧度是 90 度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)