Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I am trying to do a logarithm of zero in Python.

from math import log log(0) 

And this raises me an exception.

ValueError: math domain error 

I understand that a log of a negative number is mathematically undefined, and that's why Python's log can raise this ValueError exception. But why does it raise also the exception when calling it with a zero value? It should return a -inf value, and I've seen that infinite numbers can be represented in Python (as in hereas in here).

Can I deal with this problem without personally treating it? I mean personally when doing something like this.

from math import log, inf foo = ... # Random value. if foo != 0: return log(foo) else: return -inf 

I am trying to do a logarithm of zero in Python.

from math import log log(0) 

And this raises me an exception.

ValueError: math domain error 

I understand that a log of a negative number is mathematically undefined, and that's why Python's log can raise this ValueError exception. But why does it raise also the exception when calling it with a zero value? It should return a -inf value, and I've seen that infinite numbers can be represented in Python (as in here).

Can I deal with this problem without personally treating it? I mean personally when doing something like this.

from math import log, inf foo = ... # Random value. if foo != 0: return log(foo) else: return -inf 

I am trying to do a logarithm of zero in Python.

from math import log log(0) 

And this raises me an exception.

ValueError: math domain error 

I understand that a log of a negative number is mathematically undefined, and that's why Python's log can raise this ValueError exception. But why does it raise also the exception when calling it with a zero value? It should return a -inf value, and I've seen that infinite numbers can be represented in Python (as in here).

Can I deal with this problem without personally treating it? I mean personally when doing something like this.

from math import log, inf foo = ... # Random value. if foo != 0: return log(foo) else: return -inf 
Treat negative numbers
Source Link
Santiago Gil
  • 1.3k
  • 7
  • 21
  • 53

I am trying to do a logarithm of zero in Python.

from math import log log(0) 

And this raises me an exception.

ValueError: math domain error 

I understand that a log of a negative number is mathematically undefined, and that's why Python's log can raise this ValueError exception. But why does it raise also the exception when calling it with a zero value? It should return a -inf value, and I've seen that infinite numbers can be represented in Python (as in here).

Can I deal with this problem without personally treating it? I mean personally when doing something like this.

from math import log, inf foo = ... # Random value. if foo >!= 0: return log(foo) else: return -inf 

I am trying to do a logarithm of zero in Python.

from math import log log(0) 

And this raises me an exception.

ValueError: math domain error 

I understand that a log of a negative number is mathematically undefined, and that's why Python's log can raise this ValueError exception. But why does it raise also the exception when calling it with a zero value? It should return a -inf value, and I've seen that infinite numbers can be represented in Python (as in here).

Can I deal with this problem without personally treating it? I mean personally when doing something like this.

from math import log, inf foo = ... # Random value. if foo > 0: return log(foo) else: return -inf 

I am trying to do a logarithm of zero in Python.

from math import log log(0) 

And this raises me an exception.

ValueError: math domain error 

I understand that a log of a negative number is mathematically undefined, and that's why Python's log can raise this ValueError exception. But why does it raise also the exception when calling it with a zero value? It should return a -inf value, and I've seen that infinite numbers can be represented in Python (as in here).

Can I deal with this problem without personally treating it? I mean personally when doing something like this.

from math import log, inf foo = ... # Random value. if foo != 0: return log(foo) else: return -inf 
Source Link
Santiago Gil
  • 1.3k
  • 7
  • 21
  • 53

Logarithm of zero in Python

I am trying to do a logarithm of zero in Python.

from math import log log(0) 

And this raises me an exception.

ValueError: math domain error 

I understand that a log of a negative number is mathematically undefined, and that's why Python's log can raise this ValueError exception. But why does it raise also the exception when calling it with a zero value? It should return a -inf value, and I've seen that infinite numbers can be represented in Python (as in here).

Can I deal with this problem without personally treating it? I mean personally when doing something like this.

from math import log, inf foo = ... # Random value. if foo > 0: return log(foo) else: return -inf