Thread: Help with atan2f

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    44

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you sure the result isn't 1.1, not 1.1 pi?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    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...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Cherry65 View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Cherry65 View Post
    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
    Last edited by matsp; 11-21-2008 at 09:42 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    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.
    Last edited by abachler; 11-21-2008 at 09:44 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    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. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    thats because the arctangeant fo 91 degrees is the same as the atan of 89 degrees.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed