Degrees to Radians

Discussion in 'Mac Programming' started by dieseltwitch, Dec 24, 2011.

  1. macrumors regular

    Joined:
    Jan 24, 2008
    #1
    How do I convert Degrees to radians?

    I'm wiring a program (Obj-C, command line) to calculate the earths declination at any given day of the year.

    The formal is:
    Declination Angle = asin(0.39795*cos((0.98563*(DOY - 173))))

    I started by trying to evaluate how the computer deals with each ( ) and whats inside of it.

    The first part (DOY - 173) is easy

    The second part (0.98563 * (DOY - 173)) i run into a little bit of problems as this is degrees but needs to be converted to radians to make cos and asin work properly. right now I found that:
    (0.98563 * (DOY - 173)) * (3.14159/180)

    works but is their an easier way? I'm going to have a lot of these in the feature todo and would very much like to find an easier way.

    Also how do I round values. I'm using float but I read don't need the earths declination down to the millionths of a degree?

    I would also like to say thank you to any one who has been helping me in my other post. I hope that I'm not driving anyone nuts. I am actually going some where with this.:D
     
  2. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Code:
    #include <math.h>				// M_PI
    
    float RadiantoDegree(float radian)
    {
        return ((radian / M_PI) * 180.0f);
    }
    
    float DegreetoRadian(float degree)
    {
        return ((degree / 180.0f) * M_PI);
    }
    
     
  3. thread starter macrumors regular

    Joined:
    Jan 24, 2008
    #3
    Ok so I think I understand this as a way to put in an argument that is a radian and get out a degree with out having to re-write it over and over.... my question is how do I employ it? I'm still new enough that I don't know how to call stuff correctly.

    here is what I tried....

    I get nothing in output, even with the NSLog... How come?
    Code:
    #import <Foundation/Foundation.h>
    #import <math.h>
    int main (int argc, const char * argv[])
    {
    
        @autoreleasepool {
            float radian, degree;
            
            
            float RadiantoDegree(float radian);
            {
                return ((radian / M_PI)*180.0f);
            
            }
            float DegreetoRadian(float degree);
            {
                return ((degree / 180.0f)*M_PI);
            }
            
            float myTest,myInput;
            
            myInput = 5.0f;
            
            myTest = RadiantoDegree(myInput);
            NSLog(@"%f",myTest);
            
            
        }
        return 0;
    }
     
  4. lloyddean, Dec 24, 2011
    Last edited: Dec 24, 2011

    macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    Code:
    #import <Foundation/Foundation.h>
    #import <math.h>
            
            
    float RadiantoDegree(float radian)
    {
    	return ((radian / M_PI) * 180.0f);
    }
    
    float DegreetoRadian(float degree)
    {
    	return ((degree / 180.0f) * M_PI);
    }
    
    int main (int argc, const char * argv[])
    {
    
        @autoreleasepool {
            float radian, degree;
            
            degree = RadiantoDegree(radian);
            radian = DegreetoRadian(degree);
        }
    
        return 0;
    }
    
     
  5. thread starter macrumors regular

    Joined:
    Jan 24, 2008
    #5
    Umm when i copy that directly I get these errors...

    [​IMG]

    PS. you don't have to respond to night, unless you a nerd like I am and will stay up all night programing
     
  6. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
  7. Senor Cuete, Dec 25, 2011
    Last edited: Dec 25, 2011

    macrumors regular

    Joined:
    Nov 9, 2011
    #7
    How about:

    double Radians(double x) /*decimal degrees to radians*/
    {
    return(x * 0.017453293);
    }

    double Degrees(double x) /*converts radians to decimal degrees*/
    {
    return(x * 57.29577952);
    }

    or:

    //M_PI defined in math.h:
    #include <math.h>

    double decimalDegreesToRadians(double x)
    {
    return(x/360.0 * (2.0 * M_PI));
    }

    double radiansToDecimalDegrees(double x)
    {
    return(x / (2.0 * M_PI) * 360.0);
    }

    You could optimize the second two functions by replacing the (2.0 * M_PI) with 6.2831853071795862. I just copied the first two functions from an astronomy program, so check to see if they're correct. If a type double is the default floating point type for your processor you should do the calculations as a double. There would be no performance advantage in using a smaller type such as a float.
     
  8. macrumors regular

    Joined:
    Apr 8, 2009
    #8
    The compiler will do this for you (gcc and clang do so even with "no optimizations"), so you'll only be making your function less readable.
     
  9. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #9
    Exactly. One should never use "magic numbers" in their code.
     
  10. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #10
    You can use this macro:

    #define Deg_to_Rad(X) (X*M_PI/180.0)
    floor() and ceil()

    http://stackoverflow.com/questions/485525/round-for-float-in-c

    Be careful working with floating point numbers, though! They are tricky.

    http://www.codeproject.com/KB/recipes/float_point.aspx
     
  11. macrumors 6502

    Joined:
    Apr 24, 2008
    #11
    Always use extra parentheses in macro arguments:

    Code:
    #define DEG2RAD(X) ([b]([/b]X[b])[/b]*M_PI/180)
    
    Consider what happens if I use the former with an expression instead of a constant:

    Code:
    double rad = Deg_to_Rad(deg + 45);
    
     
  12. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
  13. macrumors regular

    Joined:
    Nov 9, 2011
    #13
    Generally you calculate such things to the maximum precision available to avoid rounding errors and display them as a lower precision when you show them to the user.

    This probably means calculating them as doubles, not floats.
     

Share This Page