[Tutor] Problems with subtraction!

col speed ajarncolin at gmail.com
Wed Jul 7 13:44:05 CEST 2010


I apologise in advance for such a silly question. Normally, I start to write
to the list and work the answer out before sending the mail. Not this time.
I'm trying to work out which triangles contain the cartesian origin (0, 0)
and have the following:

t = [-340, 495, -153, -910, 835, -947]
print  (0 - t[0])*(t[3] - t[1]) - (0 - t[1])*(t[2] - t[0])
print  (0 - t[4])*(t[3] - t[5]) - (0 - t[5])*(t[2] - t[4])
print  (0 - t[4])*(t[1] - t[5]) - (0 - t[5])*(t[0] - t[4])

where t is coordinates of triangle a,b,c and the maths is cross-product of
vectors - if all are negative then it contains the origin. Unfortunately,
when I run the programme, I get:

colin at colin-laptop:~/lib/python/euler$ python p102origin.py
-385135
904741
-91345

The second number should be negative ( I WANT it to be negative). For
example:

print  (0 - t[4])*(t[3] - t[5]) , (0 - t[5])*(t[2] - t[4])   gives :

-30895 -935636

And in the python shell:

>>> -30895 -935636
-966531

But:

print  (0 - t[4])*(t[3] - t[5]) <  (0 - t[5])*(t[2] - t[4])  gives :

False








>>> a = "No Error Message"
>>> b = "Correct Answer"
>>> a == b
False


Please forgive an old man a stupid question
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100707/c04dd9b3/attachment.html>


More information about the Tutor mailing list