添加和减去双打会产生奇怪的结果 [英] Adding and subtracting doubles are giving strange results
问题描述
因此,当我使用Doubles在Java中添加或减去时,它会给我带来奇怪的结果。以下是一些:
So when I add or subtract in Java with Doubles, it giving me strange results. Here are some:
如果我添加 0.0 + 5.1
,它会给我 5.1
。这是正确的。
If I add 0.0 + 5.1
, it gives me 5.1
. That's correct.
如果我添加 5.1 + 0.1
,它会给我 5.199999999999
(重复 9
的次数可能已关闭)。那是错的。
If I add 5.1 + 0.1
, it gives me 5.199999999999
(The number of repeating 9
s may be off). That's wrong.
如果我减去 4.8 - 0.4
,它会给我 4.39999999999995
(同样,重复的 9
可能会关闭)。那是错的。
If I subtract 4.8 - 0.4
, it gives me 4.39999999999995
(Again, the repeating 9
s may be off). That's wrong.
起初我认为这只是添加带小数值的双打的问题,但我错了。以下工作正常:
At first I thought this was only the problem with adding doubles with decimal values, but I was wrong. The following worked fine:
5.1 + 0.2 = 5.3
5.1 - 0.3 = 4.8
现在,添加的第一个数字是保存为变量的double,尽管第二个变量从的JTextField
。例如:
Now, the first number added is a double saved as a variable, though the second variable grabs the text from a JTextField
. For example:
//doubleNum = 5.1 RIGHT HERE
//The textfield has only a "0.1" in it.
doubleNum += Double.parseDouble(textField.getText());
//doubleNum = 5.199999999999999
推荐答案
在Java中, double
值为 IEEE浮点数。除非它们是2的幂(或2的幂的总和,例如1/8 + 1/4 = 3/8),否则即使它们具有高精度,也不能精确地表示它们。某些浮点运算会使这些浮点数中出现的舍入误差复杂化。在上面描述的情况下,浮点错误已经变得非常重要,足以显示在输出中。
In Java, double
values are IEEE floating point numbers. Unless they are a power of 2 (or sums of powers of 2, e.g. 1/8 + 1/4 = 3/8), they cannot be represented exactly, even if they have high precision. Some floating point operations will compound the round-off error present in these floating point numbers. In cases you've described above, the floating-point errors have become significant enough to show up in the output.
数字的来源无关紧要是,它是从 JTextField
解析字符串还是指定 double
字面值 - 问题是在浮动中继承 - 点数表示。
It doesn't matter what the source of the number is, whether it's parsing a string from a JTextField
or specifying a double
literal -- the problem is inherit in floating-point representation.
解决方法:
-
如果你认识你的话ll只有这么多的小数点,然后使用整数
算术,然后转换为小数:
If you know you'll only have so many decimal points, then use integer arithmetic, then convert to a decimal:
(double) (51 + 1) / 10
(double) (48 - 4) / 10
使用 BigDecimal
如果必须使用 double
,则可以使用: //en.wikipedia.org/wiki/Kahan_summation_algorithm\">Kahan求和算法。
If you must use double
, you can cut down on floating-point errors
with the Kahan Summation Algorithm.
这篇关于添加和减去双打会产生奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!