8

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
Community
  • 1
  • 1
Santiago Gil
  • 1,292
  • 7
  • 21
  • 52

1 Answers1

10

Actually Python is not wrong. The logarithm of zero is also undefined, which is because it's a limit, and it's -inf only from the right, not from the left:

enter image description here

So, no, you have to deal with this yourself. That's wrong, but you can do that. something like this: log(x) if x != 0 else -inf.

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • Handle negative numbers, they would return `-inf` too – Adirio Mar 23 '17 at 15:25
  • 1
    `log(x) if x !=0 else -inf`, I think, captures the right idea where a value of `-inf` can be accomodated. It still (correctly) raises an exception for negative `x`. – chepner Mar 23 '17 at 15:32
  • This answer makes no sense, because python has no problem with computing `math.log(math.inf)`. So python is kind of wrong because it treats these two limits differently for apparently no good reason. – Hyperplane Jul 06 '22 at 18:01
  • @Hyperplane With all due respect, you need to go back to high school math and learn what limits are. Generally it's better to question yourself when many people in a professional forum upvote an answer, or at least be less assertive. Perhaps go to the maths stack exchange and ask them about limits and why log(0) is undefined except with limits. – The Quantum Physicist Jul 06 '22 at 20:45
  • 1
    @TheQuantumPhysicist I have a master's degree in mathematics, thanks very much for the lecturing. I stand by my claim. One can do one of 3 sensible things. (1) define log on the open interval (0, ∞), (2) define log on the closed interval [0, ∞], making use of the [extended real number line](https://en.wikipedia.org/wiki/Extended_real_number_line) or (3) consider an extension to the [complex logarithm](https://en.wikipedia.org/wiki/Complex_logarithm), in which case the limit of the real part of log(z) as z→0 is always well defined as -∞. – Hyperplane Jul 06 '22 at 21:04
  • 1
    @TheQuantumPhysicist What python does is a weird in-between, implementing the log on (0, ∞] with `math.log(math.inf)==math.inf`, but `math.log(0)` raising a `ValueError`. Other libraries like `numpy` or `scipy` give a `RunTimeWarning` but correctly return -∞. Maybe ask me this next time when you are on maths.stackexchange.com, I have >10k reputation on that platform. The personal attacks are really uncalled for. – Hyperplane Jul 06 '22 at 21:08
  • @Hyperplane I didn't mean to personally attack you, but your answer was unjustified. Thanks for explaining. And btw, I have a PhD in physics too. Back to topic, I think the reason why your answer is different than Python's is that Python uses the real numbers field while you use the complex numbers field. If you try `math.log(0+0j)`, Python will complain that you're not using a real number. The real issue is that `cmath.log(0+0j)` is a domain error. That's another story. – The Quantum Physicist Jul 06 '22 at 21:18
  • @TheQuantumPhysicist None of your comments have explained so far why it is fine that python allows `math.log(math.inf)` but disallows `math.log(0)`. **Both** only exist in the limit sense, but only one of them is supported. Hence, the original answer provided is unsatisfying / can only be partial. Oh, and if we want to be really pedantic then we should note that this is FP-arithmetic anyway which is not equivalent to ℝ-arithmetic. In fact, I'd argue that floating points are indeed closer to the extended reals since they explicitly include ±∞, – Hyperplane Jul 06 '22 at 21:24