Degrees rather than radians

Hello C++ forums! This is my first post.

I am currently programming a flight simulator for a school project, it is going excellently. I am currently configuring the relationship between pitch, roll and yaw, I have the notes and basic mathematical framework.

Upon utilising the relevant trigonometry I found that C++ defaults to radians, this is terrible for my purpose, I require (for example) sin(180) = 0 not -0.80115263573 as with radians.

I am aware that the conversion may be done by utilising Pi, however I wish to make this simulator flawless, the dilemma follows: Pi is transcendental, to ensure 100% accuracy I would need infinite places, I require the overall calculation to be exact, i.e. sin(180degrees) = 0. Are there any #defines I may utilise? I really hope to carry out all calculations in degrees.

Any help provided is appreciated, my knowledge of C++ is intermediate at best.
2*Pi radians = 360 degrees
I do not wish to utilise pi, I wished to know whether there was a define that I could edit to directly access degrees rather than radians.

Of course, if there is no way to do this by default, or if the program makes the calculation anyway then the above answer will fulfil the question.
Last edited on
Can't you do the conversion yourself now that you know what a radian is?
Last edited on
What would one of these magical #defines do that you can't? They sure can't get around the problem of not having infinite space to hold pi. You won't get 100% accuracy when dealing with floating point numbers in computers. But you can get close enough. There's a vast amount of literature on the web where you can read all about this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
using std::cout; using std::endl;

inline double sind(double angle);

int main()
{
    cout << "sin(60) = " << sind(60) << endl;
}

inline double sind(double angle)
{
    // converts the angle(°) into radians, as sin() takes radians
    // 60° = 60π/180 = π/3
	return sin(angle * (M_PI/180));
}



sin(60) = 0.866025
Last edited on
Hi,

I have these, in my math header, note that they may not be in every implementation of math headers - that is they are not portable.

And the usual recommendation is to make them const doubles or long double. There are plenty of significant places there - 20 is usually good enough for most things.


1
2
3
4
5
6
7
8
9
10
11
12
13
# define M_E		2.7182818284590452354	/* e */
# define M_LOG2E	1.4426950408889634074	/* log_2 e */
# define M_LOG10E	0.43429448190325182765	/* log_10 e */
# define M_LN2		0.69314718055994530942	/* log_e 2 */
# define M_LN10		2.30258509299404568402	/* log_e 10 */
# define M_PI		3.14159265358979323846	/* pi */
# define M_PI_2		1.57079632679489661923	/* pi/2 */
# define M_PI_4		0.78539816339744830962	/* pi/4 */
# define M_1_PI		0.31830988618379067154	/* 1/pi */
# define M_2_PI		0.63661977236758134308	/* 2/pi */
# define M_2_SQRTPI	1.12837916709551257390	/* 2/sqrt(pi) */
# define M_SQRT2	1.41421356237309504880	/* sqrt(2) */
# define M_SQRT1_2	0.70710678118654752440	/* 1/sqrt(2) */ 
OK, I believe that this has resolved the problem. I will use Pi to make the conversion.

Your replies have been very helpful.

Thankyou,
@Lavaguava TheIdeasMan is generaly the best way since trigonometry calculus needs hundred of cycles , with a table you can interpolate . But All calculus will use radians no exception. There are several reasons for it. If you plan to use degrees instead , simply create yourself a function called convertDegToRad().
Topic archived. No new replies allowed.