4

I have a question. What's the difference between atan and atan2 in JavaScript?

I want to figure out the angle between the vector V (-1, 0) and the axis.

When I called the method atan(0 / -1), the answer was -0 rad.
When I called the method atan2(0, -1), the answer was Pi rad.

Why do they have different answers?

Also, the answer I was expecting to get was Pi rad, so since I was using atan in my code, it kinda took me a while until I tried atan2 and it finally worked (to my surprise!)

Thanks in advance! ^_^

Tirafesi
  • 1,297
  • 2
  • 18
  • 36
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2 –  Nov 16 '16 at 18:46
  • Use `atan2`, there are reasons to this, https://en.wikipedia.org/wiki/Atan2 – Afzaal Ahmad Zeeshan Nov 16 '16 at 18:46
  • From @Amy's link above it looks like `atan` and `atan2` are defined exactly the same way in JS as C++. This answer is as applicable to JS as it is to C++ https://stackoverflow.com/a/12011762/3157094 (second answer on the linked question) – Aaron Apr 09 '19 at 15:11

0 Answers0