python math domain errors in math.log function

As long as your input is within the half-open interval (0, 1] (not including 0), you are fine. You can’t be too close to zero:

>>> math.log(sys.float_info.min)

So simply checking for exactly zero (maybe as the result of an underflow) should be enough, or alternatively catch the exception and handle it.

EDIT: This also holds for the denormal minimum floating point number:

>>> math.log(sys.float_info.min * sys.float_info.epsilon)

Leave a Comment