Sin/cos in C++

Bj

Addon Developer
Addon Developer
Donator
Joined
Oct 16, 2007
Messages
1,886
Reaction score
11
Points
0
Location
USA-WA
Website
www.orbiter-forum.com
When I take the sin of something in C++ it returns the sin in rad. I want it in degree.

so is there a any other way other than taking rad * (180/pi) ?
 

computerex

Addon Developer
Addon Developer
Joined
Oct 16, 2007
Messages
1,282
Reaction score
17
Points
0
Location
Florida
When I take the sin of something in C++ it returns the sin in rad. I want it in degree.

so is there a any other way other than taking rad * (180/pi) ?

I don't know if such a function exist in libc, however you can make your own.
 

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,217
Reaction score
1,565
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
When I take the sin of something in C++ it returns the sin in rad. I want it in degree.

so is there a any other way other than taking rad * (180/pi) ?

Are you asking whether there is a function that can do that for you? After all, to convert radians to degrees someone has to perform the calculation, and all the standard math functions in math.h work with radians. If you are writing Orbiter code you can just use the DEG constant defined in OrbiterAPI.h as follows:

Code:
double myDegrees = rad * DEG;

That's about as simple as it gets. :)

If you aren't writing Orbiter code you can just define your own DEG constant as follows and then use rad * DEG as shown above:

Code:
const double PI = 3.14159265358979;
const double DEG = 180.0/PI;

If you'd rather have an inline method to do the calculation, just add these lines to your class's header file:

Code:
const double PI = 3.14159265358979;
double ToDegrees(const double rad) { return rad * 180.0 / PI; }

...and then use it like this:

Code:
double myDegrees = ToDegrees(rad);

Personally I prefer just using the DEG constant as shown in the first example.
 
Last edited:

Chode

Addon Developer
Addon Developer
Beta Tester
Joined
Mar 21, 2008
Messages
107
Reaction score
1
Points
0
When I take the sin of something in C++ it returns the sin in rad. I want it in degree.

so is there a any other way other than taking rad * (180/pi) ?

I find this a question bit confusing, so to clarify a bit: when you "take the sin of something", that something is an angle, and the return value is dimensionless. The angle you provide can be expressed in degrees or radians, and C(++) is expecting radians, so you may need to convert the arguement you pass to sin(argument) from degrees to radians. If you are talking about asin, which is the inverse sin function, then what you say is correct, the asin function returns a value in radians.

Regards
 

computerex

Addon Developer
Addon Developer
Joined
Oct 16, 2007
Messages
1,282
Reaction score
17
Points
0
Location
Florida
You can also define a function like this:

Code:
double sin2(double angle) // in degrees
{
      return DEG*(sin(angle*RAD));
}
 

markl316

XR2 Ravenstar Commander
Addon Developer
Tutorial Publisher
Joined
Mar 30, 2008
Messages
450
Reaction score
1
Points
18
I don't know if such a function exist in libc, however you can make your own.

Use a taylor polinomial.

1 - (1/3!)x^3 + (1/5!)x^5 - (1/7!)x^7...
 
Top