-3

I'm completely stumped dealing with something quite simple. The following lines are a part of a much much larger program. Thing and stuff are two objects on a grid, and I need to find the angle between them, in relation to the y-axis. math.atan takes in a floating point number, which I why I need to type cast the distance between the objects.

m_angle = math.degrees(math.atan(float(thing.position()[1]-stuff.position()[1]) / float(thing.position()[0]-stuff.position()[0])))

m_angle = math.degrees(math.atan(float(thing.position()[1]-stuff.position()[1]) / thing.position()[0]-stuff.position()[0]))

The .position calls all return integers.

I get different results while running my program, for each line. The first two should return exactly the same result, a float.

What I don't understand, is how I can possibly get different results depending on whether I run line 1 or line 2 :/

This is a part of a simulation,

Asgeir
  • 727
  • 3
  • 9
  • 20
  • "should get the message across" - yes, using `thing` and `stuff` really clears things up, particularly as `thing` seems to be both a function and an instance. Could you give all relevant code, inputs, expected outputs and actual outputs? – jonrsharpe May 08 '14 at 11:01
  • @Asgeir Simplify your question - the third line is not relevant. Then simplify it to find simplest expressions which are making difference. I guess, that removing `math.degrees(math.atan(` will be possible to present the problem. Last, make your question runnable, people will help you, if you allow them to reproduce it. – Jan Vlcinsky May 08 '14 at 11:01
  • -1 -- The expression are different and involve variables that you didn't even mention in your question. The real question is: how can the first and second return *the same* result, since they are different? Only you can know this, and not explaning what those variables are you are just wasting our time. – Bakuriu May 08 '14 at 11:02
  • After your edit the two lines are now identical except that the first one has unmatched parentheses and is therefore a syntax error. Please, cut and paste you *actual* code, not some paraphrased messed up variant thereof. – Duncan May 08 '14 at 11:05
  • Sorry, I changed the variable names afterwards because this is part of a competition (I can still ask questions here, don't worry :) ). I updated some of the information, but I felt I had to have the math part of it there, because it might be relevant. – Asgeir May 08 '14 at 11:07
  • @Duncan sorry about that, noticed it as well and fixed. – Asgeir May 08 '14 at 11:08
  • Sorry about the ill formed question. I guess it was a mixture of me being irritated by the problem, and having read it so many times it's completely obvious to me :) The answer came below, thanks for the help guys, and the lesson on wording my questions. – Asgeir May 08 '14 at 11:20
  • @Asgeir, you might also want to use atan2(y,x) instead of atan(y/x) to get the correct angle without further modifications based on quadrants. Then there would also be no need for explicit conversions to avoid integer divisions, since there are no divisions before the function call. – Lutz Lehmann May 08 '14 at 11:34

2 Answers2

4

Difficult to answer at the rate you are editing but:

m_angle = math.degrees(math.atan(float(thing.position()[1]-stuff.position()[1]) / float(thing.position()[0]-stuff.position()[0])))

the bit inside the atan is equivalent to something/(a-b)

m_angle = math.degrees(math.atan(float(thing.position()[1]-stuff.position()[1]) / thing.position()[0]-stuff.position()[0]))

the bit inside the atan is equivalent to (something/a)-b

Duncan
  • 92,073
  • 11
  • 122
  • 156
  • Yes, this is a simple order of operations problem. OP: remember that parentheses are free! – jonrsharpe May 08 '14 at 11:13
  • Thanks a lot for the help! somehow I just disregarded that this might be the cause. Teaches me to calculated parts of the calculation first, or as you said, use my parenthesis! – Asgeir May 08 '14 at 11:19
0

To avoid all confusion, use

m_angle = math.atan2(thing.position()[1]-stuff.position()[1], thing.position()[0]-stuff.position()[0]);
m_angle = math.degrees(m_angle);

No division included, thus no integer-to-float conversion necessary.

This will give, in difference to the atan function, the correct angle if the angle is in the II. or III. quadrant. The atan function gives the result mod 180°, the atan2 function mod 360°. Depending on the application, the one or the other could be the intended result.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51