How should I deal with floating numbers that numbers that can get so small that the become zero

Posted by Tristan Havelick on Stack Overflow See other posts from Stack Overflow or by Tristan Havelick
Published on 2010-06-08T17:48:44Z Indexed on 2010/06/08 17:52 UTC
Read the original article Hit count: 193

So I just fixed an interesting bug in the following code, but I'm not sure the approach I took it the best:

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)

Because the result doesn't need to be exact, I solved this by simply keeping the smallest non-zero value, and using that if p ever becomes 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)

This works, but it seems a bit kludgy to me. I don't do a ton of this kind of numerical programming, and I'm not sure if this is the kind of fix people use, or if there is something better I can go for.

© Stack Overflow or respective owner

Related posts about floating-point

Related posts about numerical-methods