Math domain error in python when using log
问题 Here is what I am writing: >>> import math >>> 2/3*math.log(2/3,2) Here is the error I'm getting: Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: math domain error Can someone please explain what I'm doing wrong? Thanks. 回答1: I'm assuming this is Python 2.7. In 2.7, 2/3 evaluates to 0 since division floors by default. Therefore you're attempting a log 0, hence the error. Python 3 on the other hand does floating point division by default. To get the correct