PDA

View Full Version : Degrees to Radians

dieseltwitch
Dec 24, 2011, 09:33 PM
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
Dec 24, 2011, 09:54 PM
#include <math.h> // M_PI

{
return ((radian / M_PI) * 180.0f);
}

{
return ((degree / 180.0f) * M_PI);
}

dieseltwitch
Dec 24, 2011, 10:18 PM
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?
#import <Foundation/Foundation.h>
#import <math.h>
int main (int argc, const char * argv[])
{

@autoreleasepool {

{

}
{
return ((degree / 180.0f)*M_PI);
}

float myTest,myInput;

myInput = 5.0f;

NSLog(@"%f",myTest);

}
return 0;
}

lloyddean
Dec 24, 2011, 10:38 PM
#import <Foundation/Foundation.h>
#import <math.h>

{
return ((radian / M_PI) * 180.0f);
}

{
return ((degree / 180.0f) * M_PI);
}

int main (int argc, const char * argv[])
{

@autoreleasepool {

}

return 0;
}

dieseltwitch
Dec 24, 2011, 10:57 PM
Umm when i copy that directly I get these errors...

http://i268.photobucket.com/albums/jj1/MrDieselTwitch/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

lloyddean
Dec 24, 2011, 10:59 PM
My mistake - changed code above!

Senor Cuete
Dec 25, 2011, 10:20 AM

{
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>

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

{
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.

JoshDC
Dec 25, 2011, 06:26 PM
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.

GorillaPaws
Dec 25, 2011, 10:46 PM
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.

Exactly. One should never use "magic numbers (http://en.wikipedia.org/wiki/Magic_number_(programming))" in their code.

robvas
Dec 26, 2011, 09:19 PM
How do I convert Degrees to radians?

You can use this macro:

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?

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

Sander
Dec 27, 2011, 03:57 AM
You can use this macro:

Always use extra parentheses in macro arguments:

Consider what happens if I use the former with an expression instead of a constant: