我目前正在尝试为字符串实现一个基本的算术编码器,而我在找到频率分布的范围方面遇到了麻烦。
这是我的代码:
TreeMap <Character,Integer> freq = new TreeMap <Character,Integer>();
TreeMap <Character,Range> rangeMap = new TreeMap <Character,Range>();
String encoding;
public void getRanges() {
double rangeSize = 1.0 / encoding.length();
System.out.println(rangeSize);
double lower = 0.0, higher = 0.0;
for (int i = 0; i < freq.size(); i++) {
Object key = freq.keySet().toArray()[i];
double val = freq.get(key);
higher = higher + (val * rangeSize);
rangeMap.put((char) key, new Range(lower, higher));
lower = higher;
}
}
当我输入一个长度为5的字符串,即“hello”时,它可以正常工作,
{e=0.0 - 0.2, h=0.2 - 0.4, l=0.4 - 0.8, o=0.8 - 1.0}
但是当我输入一个长度为10的字符串,即“账单门”时,它给了我这个:
{ =0.0 - 0.1, a=0.1 - 0.2, b=0.2 - 0.30000000000000004, e=0.30000000000000004 - 0.4, g=0.4 - 0.5, i=0.5 - 0.6, l=0.6 - 0.8, s=0.8 - 0.9, t=0.9 - 1.0}
我不明白0.30000000000000004来自哪里?
任何帮助都将不胜感激,谢谢。