I have this code:
import math
n2 = [0,1,2,3]
for i in range(1,3):
x = math.log2(n2[i])
n2 += x
print(n2)
When I try it, I get: ValueError: math domain error
. Why? What is wrong, and how do I fix it?
I have this code:
import math
n2 = [0,1,2,3]
for i in range(1,3):
x = math.log2(n2[i])
n2 += x
print(n2)
When I try it, I get: ValueError: math domain error
. Why? What is wrong, and how do I fix it?
This code, as posted, raise another error: TypeError: 'float' object is not iterable
.
However, the error you say that you get is reproduced by this code:
import math
n2 = [0, 1, 2, 3]
for i in n2:
x = math.log2(i)
You are attempting to call math.log2
with 0
which makes no sense. As the error says, 0
is not in the domain of any of the log
functions. There is no such x
that satisfies k
x
= 0
for any k
.
You define n2
as a list. But near the end you try to execute
n2 += x
You are trying to add the float value x
to the list n2
. This causes the error
'float' object is not iterable
Note that is not the error you state in your question. The error you state is the result if you change the range(1,3)
to range(3)
or range(0, 3)
. That attempts to take the logarithm of the first element in your array, namely zero, and that does not have a logarithm.
You probably want to indent that next-to-last line and change it to
n2 += [x]
That has the printout
[0, 1, 2, 3, 0.0, 1.0]