谭45给我0.9999 [英] tan 45 gives me 0.9999
问题描述
tan 45
(弧度为0.7853981633974483)给我 0.9999
?下面的代码有什么问题? System.out.println(Math.tan(Math.toRadians(45.0)));
我不认为这里有任何错字。
那么这里的解决方案是什么?解析方案
浮点计算通常会导致这样的不准确。问题是数字不能在固定的位数内准确表示。给你另一个例子(十进制),我们都同意: 3 *(1/3)= 1
。但是,如果您的计算器只有4位小数, 1/3
将被表示为 0.3333
。当它与 3
相乘时,您将得到 0.9999
不是 1
。
更多信息,大多数系统上的浮点通常是使用IEEE754标准表示的。您可以搜索它,或参考维基百科页面了解更多详情。 IEEE浮点数
Why does tan 45
(0.7853981633974483 in radian) give me 0.9999
? What's wrong with the following code?
System.out.println(Math.tan(Math.toRadians(45.0)) );
I don't think there's any typo in here.
So what's the solution here?
Floating point calculations will often lead to such inaccuracies. The problem is that numbers cannot be accurately represented within a fixed number of bits.
To give you another example (in decimal), we all agree that 3 * (1/3) = 1
. However, if your calculator only has 4 decimal places, 1/3
would be represented as 0.3333
. When that's multiplied with 3
, you would get 0.9999
not 1
.
As further information, floating points on most systems are usually represented using the IEEE754 standard. You could search for it, or refer the Wikipedia page for more details. IEEE floating point
这篇关于谭45给我0.9999的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!