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

Kyn*_*ian 7 java math

我的代码以弧度为角度传递给cos,tansin.除了tan之外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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么返回的值不严格等于IEEE无穷大?

Jon*_*eet 15

好吧,tan(pi/2)弧度基本上是无限的,不是吗?所以你希望得到一个非常大的数字,不是吗?(它不是无穷大,因为pi/2不能完全表示为a double.你在渐近曲线上找到一个非常接近无限曲线的值.)

请参阅sin/cos/tan的这些图表,看看我的意思,记住pi/2弧度是90度.