Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Floating point 我应该如何处理浮点数,这些浮点数会变得非常小,以至于数字变成零_Floating Point_Numerical Methods - Fatal编程技术网

Floating point 我应该如何处理浮点数,这些浮点数会变得非常小,以至于数字变成零

Floating point 我应该如何处理浮点数,这些浮点数会变得非常小,以至于数字变成零,floating-point,numerical-methods,Floating Point,Numerical Methods,因此,我在下面的代码中修复了一个有趣的bug,但我不确定我采用的最佳方法: p = 1 probabilities = [ ... ] # a (possibly) long list of numbers between 0 and 1 for wp in probabilities: if (wp > 0): p *= wp # Take the natural log, this crashes when 'probabilites' is long enough t

因此,我在下面的代码中修复了一个有趣的bug,但我不确定我采用的最佳方法:

p = 1
probabilities = [ ... ] # a (possibly) long list of numbers between 0 and 1
for wp in probabilities:

  if (wp > 0):
    p *= wp

# Take the natural log, this crashes when 'probabilites' is long enough that p ends up
# being zero
try:
    result = math.log(p)
因为结果不需要精确,我通过简单地保持最小的非零值来解决这个问题,如果p变为0,就使用它

p = 1
probabilities = [ ... ] # a long list of numbers between 0 and 1
for wp in probabilities:

  if (wp > 0):
    old_p = p
    p *= wp
    if p == 0:
      # we've gotten so small, its just 0, so go back to the smallest
      # non-zero we had
      p = old_p
      break

# Take the natural log, this crashes when 'probabilites' is long enough that p ends up
# being zero
try:
    result = math.log(p)
这是可行的,但对我来说似乎有点笨拙。我没有做过很多这样的数值编程,我不确定这是否是人们使用的修复方法,或者是否有更好的方法可以使用

既然
math.log(a*b)
等于
math.log(a)+math.log(b)
,为什么不取
概率
数组所有成员的日志之和呢

这将避免
p
在流量下变得太小的问题

编辑:这是numpy版本,对于大型数据集,它更干净、速度更快:

import numpy
prob = numpy.array([0.1, 0.213, 0.001, 0.98 ... ])
result = sum(numpy.log(prob))

天才!我知道我必须摘下我的程序员帽,戴上我的数学家帽