• ORBITER-FORUM will be temporarily closed at 2026-07-23 18:00 UTC while we complete some OF maintenance tasks. The amount of downtime is expected to take up to one hour, but probably less.

Sin/cos in C++

  • Thread starter Thread starter Bj
  • Start date Start date

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) ?
 
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.
 
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:
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
 
You can also define a function like this:

Code:
double sin2(double angle) // in degrees
{
      return DEG*(sin(angle*RAD));
}
 
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...
 
Back
Top