2016-04-30 47 views
0

想做使用數字計算的二維數組計算使用二維數組數:ValueError異常:數學域錯誤

import math 
result = 0 
data = [[0],[1],[1],[1],[0],[1],[1],[1]] 
for i in data: 
    result += (math.log(i[0], 2)) 

報告的錯誤是ValueError: math domain error

有人能告訴我什麼是不對的碼?

+0

您正在嘗試採取'0 log'在數學上是不可能的。 – AKS

+0

@AKS感謝您的回覆。在這種情況下,給定的數字都是非負的。 – Echo0831

+0

看看我的其他評論。同樣的邏輯適用於0. – AKS

回答

1

log 0 is undefined. It's not a real number, because you can never get zero by raising anything to the power of anything else. You can never reach zero, you can only approach it using an infinitely large and negative power. 3.

>>> math.log(0) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: math domain error