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

问题描述

我对 Math.cos()方法有一点问题。我知道,在使用 Math.cos()之前,我必须将角度转换为Radians。但是,如果我这样做:

I've got a little Problem with the Math.cos() method. I know, I have to convert the angle to Radians before using Math.cos(). But if I just do:

System.out.println(Math.cos(Math.toRadians(90));

输出:6.123233995736766E-17

It outputs: 6.123233995736766E-17

Math.sin()运作良好。

推荐答案

来自三角学:

sin x ~= x, for small values of x
sin x = cos x+pi/2

因为pi / 2不能完全用IEEE-754浮点表示,这意味着它必须是偏离某个值x,即它由pi / 2 + -x表示,其中x

Because pi/2 can't be represented exactly in IEEE-754 Floating point, it means, that it must be off by some value x, i.e it is represented by pi/2 +- x, where x < the least significant bit in the floating point system. Which in this case is 2^-53 = 1.1102e-16.

在这个特殊情况下,x~ = 6.123233995736766E-17,大约是最大误差的55%。
所以,这是一个相当不错的结果...

In this particular case x ~= 6.123233995736766E-17, which is about 55% of the maximum error.So, it's a rather good result...

这篇关于Java Math.cos(Math.toRadians(&lt; angle&gt;))返回奇怪的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:14