# Help with atan2f

• 11-21-2008
Cherry65
Help with atan2f
Im doing some exercises from a book, and in one of them i have to rewrite the function atan2f. First, i wanted to explore a bit about this function, and while doing this i noticed that the results given by this function differed from the ones given in the book. For example, the book says that atan2f(4,2) = 63.46(degrees) and when i do this:
Code:

```#include <iostream> #include <cmath> using namespace std; int main() {         cout << atan2f(4,2) << endl;         system("PAUSE"); }```
the result is 1.1 pi, that is 198 degrees. When i check this on a sheet of paper i realise that the result given by the book is the correct one. Whats wrong with this?

Maybe i misunderstood the excercise. This is the exercise:
Code:

```Using atanf, write a function with the following prototype: float MyArcTangent(float y, float x); This function should examine the signs of coordinates of the point (x, y) and return the correct angle based on what quadrant the point lies in. Test your function with the following points: (2,4), (-1,5), (-6,-4), (4, -6). You sould get the following results: MyArcTangent (4, 2) = 63.4671 MyArcTangent (5, -1) = 101.27 MyArcTangent (-4, -6) = 213.707 MyArcTangent (-6, 4) = -56.3385 Press any key to continue Now that you have written the function yourself, you should know that the C++ standard math library already includes a function that does what your function does. Its prototype is: float atan2f(float y, float x);```
Thank you,
Cherry65.
• 11-21-2008
tabstop
Are you sure the result isn't 1.1, not 1.1 pi?
• 11-21-2008
Cherry65
yes..well atan2f give the result in "pi" isnt it? so u have to do result*180 to get the result in degrees, and the result in degrees checked in the sheet of paper is the one that the book gives...
• 11-21-2008
matsp
According to google, atan(2) is 1.1..., which is approximately pi/3 -> approx 60 degrees. So are you sure that you are not confusing yourself and multiplying by pi once too many.

--
Mats
• 11-21-2008
matsp
Quote:

Originally Posted by Cherry65
yes..well atan2f give the result in "pi" isnt it? so u have to do result*180 to get the result in degrees, and the result in degrees checked in the sheet of paper is the one that the book gives...

It gives the result in radians, where 360 degrees = 2pi, so to get degrees, you should divide by pi and multiply by 180.

--
Mats
• 11-21-2008
Cherry65
wait, so i messed up with maths... if i get 1.1 from atan2f function, thats 1.1 pi isnt it? whats that aprox in degrees? and whats the math? cause 360/1.1 * 180 cannot be since it would be a long number.
• 11-21-2008
matsp
Quote:

Originally Posted by Cherry65
wait, so i messed up with maths... if i get 1.1 from atan2f function, thats 1.1 pi isnt it? whats that aprox in degrees? and whats the math? cause 360/1.1 * 180 cannot be since it would be a long number.

No, 1.1 means 1.1 radians, which is (1.1 / pi) * 180 as the result in degrees.

--
Mats
• 11-21-2008
abachler
Code:

```float MyAtan2f(float y , float x){   return atan(y/x);   }```
1.1 radians, there are 2pi radians in a circle. so (radians/(2*pi))*360 = degrees, or (radians / pi) * 180 to simplify or radains * 57.29577951 to simplify even further.
• 11-21-2008
matsp
Quote:

Originally Posted by abachler
Code:

```float MyAtan2f(float y , float x){   return atan(y/x);   }```
any other questions?

I think the idea was to do this "by hand" as an exercise in writing functions that do math. Just like when learning about strings, you can't actually just call strcpy when you are supposed to write a "mystrcpy".

--
Mats
• 11-21-2008
Cherry65
oh so i messed up with my maths. Well thank you very much. and abachler, that could be a rpoblem because atan range is just [-90, 90] so what about all the rest? what about points in quadrants 2 and 3?. and i know how to do the code, but i had this question only.
• 11-21-2008
abachler
thats because the arctangeant fo 91 degrees is the same as the atan of 89 degrees.
• 11-21-2008
matsp
Quote:

Originally Posted by abachler
thats because the arctangeant fo 91 degrees is the same as the atan of 89 degrees.

Yes, so you look at the sign of both y and x to determine which quadrant that the value belongs in, as the man page says:

http://linux.die.net/man/3/atan2f

--
Mats