Java Math.cos(Math.toRadians(<angle>)) 返回奇怪的值 [英] Java Math.cos(Math.toRadians(<angle>)) returns weird values
问题描述
Math.cos()
方法有一点问题.我知道,在使用 Math.cos()
之前,我必须将角度转换为弧度.但如果我只是这样做:
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 <浮点系统中的最低有效位.在这种情况下是 2^-53 = 1.1102e-16.
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(<angle>)) 返回奇怪的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!