Java:不准确使用双 [英] Java: Inaccuracy using double
问题描述
我正在做一个直方图类,我遇到了一个奇怪的问题。
I'm making a histogram class, and I'm encountering a weird issue.
这是类的基础知识,有更多的方法,
Here are the basics of the class, there are more methods but they aren't relevant to the issue.
private int[] counters;
private int numCounters;
private double min, max, width;
public Histogram(double botRange, double topRange, int numCounters) {
counters = new int[numCounters];
this.numCounters = numCounters;
min = botRange;
max = topRange;
width = (max - min) / (double) numCounters;
}
public void plotFrequency() {
for (int i = 0; i < counters.length; i++) {
writeLimit(i * width, (i + 1) * width);
System.out.println(counters[i]);
}
}
private void writeLimit(double start, double end) {
System.out.print(start + " <= x < " + end + "\t\t");
}
当我绘制频率时,会出现问题。我创建了2个实例。
new直方图(0,1,10);
new Histogram(0,10,10);
the problem happens when I plot the frequencies. I've created 2 instances. new Histogram(0, 1, 10); new Histogram(0, 10, 10);
这是他们输出的。
Frequecy
0.0 <= x < 0.1 989
0.1 <= x < 0.2 1008
0.2 <= x < 0.30000000000000004 1007
0.30000000000000004 <= x < 0.4 1044
0.4 <= x < 0.5 981
0.5 <= x < 0.6000000000000001 997
0.6000000000000001 <= x < 0.7000000000000001 1005
0.7000000000000001 <= x < 0.8 988
0.8 <= x < 0.9 1003
0.9 <= x < 1.0 978
Frequecy
0.0 <= x < 1.0 990
1.0 <= x < 2.0 967
2.0 <= x < 3.0 1076
3.0 <= x < 4.0 1048
4.0 <= x < 5.0 971
5.0 <= x < 6.0 973
6.0 <= x < 7.0 1002
7.0 <= x < 8.0 988
8.0 <= x < 9.0 1003
9.0 <= x < 10.0 982
所以我的问题是,为什么在第一个例子中我得到了很长的十进制数,但不是第二个?
So my question is, why am I getting the really long decimal limits in the first example, but not the second one?
推荐答案
某些小数不能用双精度值来表示。 0.3是这些值之一。
Some decimals cannot be exactly represented by double values. 0.3 is one of those values.
小于一定数字的所有整数值(我忘记了哪一个)恰好具有双重值的精确表示,所以你不要看看近似值。
All integer values less than a certain number (I forget which) happen to have an exact representation by a double value, so you don't see the approximation.
考虑我们如何看待数字:数字123表示为(1 * 100)+(2 * 10)+(3 * 1) 。我们以10为基础。二进制数使用两个。所以当你看一个数字的分数,你怎么能代表0.3加上个别的力量2?你不行你可以想出的最好的是大约0.3亿亿美元(我必须看到确切的二进制数字,看看它是如何达到的)。
Consider how we think of numbers: the number 123 is represented as (1 * 100) + (2 * 10) + (3 * 1). We use 10 as our base. Binary numbers use two. So when you look at fractions of a number, how could you represent 0.3 by adding individual powers of 2? You can't. The best you can come up with is about 0.30000000000000004 (I'd have to see the exact binary digits to see how it reaches that).
这篇关于Java:不准确使用双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!