I have this code:
def fit(self,p1,p2,w):
p=p1
m=self.bit(p,w)
if p1.y!=p2.y:
if m&0x30:
p.y=w.y1 if m&0x10 else w.y2-1
try:
p.x=int((p.y-p2.y)*(p1.x-p2.x)/(p1.y-p2.y)+p2.x)
except Exception,e:
print e
print p1.y!=p2.y
print p1.y,p2.y
The output is:
cannot convert float NaN to integer开发者_运维技巧
False
199 199.0
I have no idea why p1.y!=p2.y
evaluates to true and then in the exception it evaluates to false
I'm running python 2.6.6
You are dividing 0/0
!
# (p.y-p2.y)*(p1.x-p2.x)
x = p.y - p2.y
x = 199 - 199.0 = 0
x1 = p1.x-p2.x
0 * x1 = 0* p1.x - 0* p2.x
This means
(p.y-p2.y)*(p1.x-p2.x) = 0
Now we are devising 0 by this:
#(p1.y-p2.y)
p1.y-p2.y
x = p.y - p2.y
x = 199 - 199.0 = 0
So in this case you are trying to do 0/0
Then at the end of the whole thing you try to add p2.x
and i believe thats why you get the error cannot convert float NaN to integer
and not ZeroDivisionError: integer division or modulo by zero
Its really easy to solve. change if p1.y!=p2.y:
line 4 to If p1.y-p2.y != 0:
精彩评论