我的精确度有问题。我将许多非常低的数字(例如0.000001
)相乘很多次(有时候在500-1000
左右),因此我失去了精确度。我在double
(也在字典中)得到了我的号码。
有没有办法解决这个问题? 这是代码:
foreach (var word in fileDictionary)
{
dictionaryTotal.TryGetValue(word.Key, out percent);
temp = percent;
A *= temp;
B *= (1 - temp);
}
在此示例中, A/B
变为0.0
。
答案 0 :(得分:1)
您可以使用对数,并在以后根据需要处理结果。
foreach (var word in fileDictionary)
{
dictionaryTotal.TryGetValue(word.Key, out percent);
temp = percent;
A += Math.Log10(temp);
B += Math.Log10(1 - temp);
}
然后,您可以操作生成的对数,而不是结果数。
答案 1 :(得分:0)
如果您希望最终A / B结果超出双倍幅度范围,最佳解决方案是使用cakephp 3.0中建议的对数。
如果temp
变化,您不需要A或B,并且最终的A / B结果预计是双倍范围,或者如果不是,则可以视为零,您可能能够通过直接处理比率得到它:
ratio = ratio * temp / (1 - temp)