本文介绍了Java中的tan()返回一个奇怪的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码将弧度角传递给costansin.除了90的tan之外,其他所有东西似乎都工作正常,出于某种奇怪的原因,它给出了值16331239353195370.示例代码:

My code passes an angle in radians to the cos, tan and sin. Everything seems to work fine except tan of 90, which gives the value 16331239353195370 for some odd reason. Example code:

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无穷大?

Why is the value returned not strictly equal to IEEE infinity?

推荐答案

以弧度表示的tan(pi/2)本质上是无限的,不是吗?因此,您期望得到一个非常大的数字,不是吗? (它不是无穷大的,因为pi/2不能精确地表示为double.您正在渐近曲线上找到一个值,该值非常接近可能变为无限的位置.)

Well, tan(pi/2) in radians is essentially infinite, isn't it? So you'd expect to get a very large number, wouldn't you? (It's not infinity because pi/2 can't be exactly represented as a double. You're finding a value on an asymptotic curve very close to where it would become infinite.)

请参见这些sin/cos/tan 明白我的意思,记得pi/2弧度是90度.

See these graphs of sin/cos/tan to see what I mean, remembering that pi/2 radians is 90 degrees.

这篇关于Java中的tan()返回一个奇怪的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!