Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dieseltwitch

macrumors regular
Original poster
Jan 24, 2008
142
0
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
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
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);
}
 

dieseltwitch

macrumors regular
Original poster
Jan 24, 2008
142
0
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;
}
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
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;
}
 
Last edited:

dieseltwitch

macrumors regular
Original poster
Jan 24, 2008
142
0
Umm when i copy that directly I get these errors...

ScreenShot2011-12-24at95523PM.png


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

Senor Cuete

macrumors 6502
Nov 9, 2011
421
30
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.
 
Last edited:

JoshDC

macrumors regular
Apr 8, 2009
115
0
You could optimize the second two functions by replacing the (2.0 * M_PI) with 6.2831853071795862.

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.
 

robvas

macrumors 68040
Mar 29, 2009
3,240
629
USA

Sander

macrumors 6502a
Apr 24, 2008
520
67
You can use this macro:

#define Deg_to_Rad(X) (X*M_PI/180.0)

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);
 

Senor Cuete

macrumors 6502
Nov 9, 2011
421
30
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?

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.