Wrote some Python code to verify if my Vectors are parallel and/or orthogonal. Parallel seems to be alright, orthogonal however misses out in one case. I thought that if the dotproduct of two vectors == 0, the vectors are orthogonal? Can someone tell me what's wrong with my code?
def isParallel(self,v):
try:
print self.findTheta(v,1)
if (self.findTheta(v,1) == 0 or self.findTheta(v,1) == 180):
return True
except ZeroDivisionError:
return True
return False
def isOrthogonal(self,v):
print self.dotProduct(v)
if self.dotProduct(v) == 0:
return True
return False
def dotProduct(self,v):
dotproduct = sum([self.coordinates[i]*v.coordinates[i] for i in range(len(self.coordinates))])
return dotproduct
the above code runs fine, as there are chances of rounding errors in my original code. Hans Lundmark got is straight: floating point rounding errors can cause the code to falter and produce false negatives.