在Ruby中处理非常小的数字

时间:2015-05-23 20:47:25

标签: ruby bigdecimal

我希望乘以超过10K的概率估计值(0到1之间的值)。 我正在使用Ruby。我使用BigDecimal来存储像

这样的小数字
prod = BigDecimal.new("1")
prod = prod * BigDecimal.new("#{ngramlm[key]}")

但经过几次迭代后,prod变为零。你能帮我解决一下如何将最终产品存放到产品中(这个数字非常接近零)!!

2 个答案:

答案 0 :(得分:2)

您所描述的内容听起来像是使用日志概率(http://en.wikipedia.org/wiki/Log_probability)的典型案例。使用log(y)=log(x1)+log(x2)代替y=x1*x2(将乘法转换为对数概率的加法)将提高速度和数值稳定性。

答案 1 :(得分:0)

您可以使用本机Ruby Rational类。作为a rational number can be represented as a paired integer number; a/b (b>0).

e.g。

Rational(0.3)    #=> (5404319552844595/18014398509481984)
Rational('0.3')  #=> (3/10)
Rational('2/3')  #=> (2/3)

0.3.to_r         #=> (5404319552844595/18014398509481984)
'0.3'.to_r       #=> (3/10)
'2/3'.to_r       #=> (2/3)
0.3.rationalize  #=> (3/10)

所以你的数字将被转换为有理数,你可以获得更大的精确度,因为理性的理性会给你理性。 E.g。

Rational(2, 3)  * Rational(2, 3)   #=> (4/9)
Rational(900)   * Rational(1)      #=> (900/1)
Rational(-2, 9) * Rational(-9, 2)  #=> (1/1)
Rational(9, 8)  * 4                #=> (9/2)

所以你基本上会处理分子和分母中整数的乘法,这是精确的。

相关问题