java中tan是什么意思,Java中的tan()返回一个奇怪的值

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);

}

}

}

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

解决方案

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.)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值