tan() 在 Java 中返回一个奇怪的值



我的代码将弧度的角度传递给costansin。一切似乎都很好,除了 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 无穷大?

嗯,弧度中的tan(pi/2)本质上是无限的,不是吗?所以你会期望得到一个非常大的数字,不是吗?(这不是无穷大,因为 pi/2 不能完全表示为double。你在渐近曲线上找到一个值,非常接近它变成无穷大的位置。

看看这些sin/cos/tan的图表来理解我的意思,记住pi/2弧度是90度。

最新更新