# radian to degree value problem?

• 04-02-2009
maifs
i want to convert the value of

sin45

it gives the value of

0.850903534 of sin45(which is in radian).

i have to use its degree 's value

mean to say that
0.70710678118654752440084436210485 which is its degree value.

what should i do?

i am trying this
0.850903534 * 180 / Pi
but
it gives this 48.753180728611482440956052242364 which is in degree but not actual result
• 04-02-2009
matsp
No, you need to do 45 * pi / 180, and then feed that value into sin. There is no (reasonable) way to convert a sin-value from radians to degrees or the other way around, after you have called sin. You need to convert the degrees to radians before calling sin.

--
Mats
• 04-02-2009
grumpy
You mean you need to compute the sine of 45 degrees, I assume. Then you need to convert the value of 45 (not sin45) from degrees to radians, and compute the sine of that to store in the variable sin45.
• 04-02-2009
ulillillia
A "macro" I frequently use for this very thing (it's not a real macro):

Code:

`#define degrees 0.0174532925199432958 // to the limit of a double plus a bit extra`
Just multiply by this value to convert degrees to radians. For the inverse, you could divide by this value or use this "macro" (it's not a real one):

Code:

`#define radians 57.295779513082321 // to the limits of a double plus a bit extra`
Using them is done this way:

Code:

```TestValue = sin(30.0*degrees); // you should get exactly 0.5 TestValueInverse = asin(TestValue)*radians; // you should get 30 back TestValueInverse2 = asin(TestValue)/degrees; // the alternative, but less clear method```
I do this a lot for calculating slopes in my game. It speeds things up and makes it easy to read as well. Hopefully this will help.

Edit: 30.0 in example usage, not 30 (the latter would be for an int, not a double).
• 04-02-2009
EVOEx
Quote:

Originally Posted by ulillillia
Edit: 30.0 in example usage, not 30 (the latter would be for an int, not a double).

Actually, 30 (integer) multiplied by a double would result in a double.

Your macro (or better yet: use a constant) won't make things faster. The compiler will (or, should) optimize it to that anyway. But I do agree it makes things more readable.
• 04-02-2009
maifs
thx you matsp and
grumpy

you are saying it right which i need

thx you:):cool:
• 04-02-2009
ulillillia
I mainly meant that it's faster to type it. What use is there in using a constant instead?

Code:

```const double degrees = 0.0174532925199432958; const double radians = 57.295779513082321; ... TestValue = sin(30.0*degrees); ...```
For the int/double thing, the program would (I think) have to convert the int to a double, which is a bit CPU intensive compared to just using an all-out double as is.
• 04-02-2009
matsp
Quote:

Originally Posted by ulillillia
I mainly meant that it's faster to type it. What use is there in using a constant instead?

Code:

```const double degrees = 0.0174532925199432958; const double radians = 57.295779513082321; ... TestValue = sin(30.0*degrees); ...```
For the int/double thing, the program would (I think) have to convert the int to a double, which is a bit CPU intensive compared to just using an all-out double as is.

Yes, but conversion to double will happen at compile-time for constants, so it's no cost at the runtime phase of the code. [At least for decent compilers, which includes the two most popular here: gcc and Microsofts C/C++]

--
Mats