Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Trigonometry problem with radians and degrees

I have a small trigonometric query.

Using functions like sin, cos and tan declared in the <math.h> file returns the answer in radians, I need the answer in degrees. Some MSDN documentation told me to times radians by (180/pi) to convert them to degrees. However, this formula is not accurate when compared to the results given when calculating cos(30) for example with a calculator in RAD and then DEG mode.

How can I set the data format or result format for these functions, or more accurately convert the answer from RAD to DEG?
0
josamoto
Asked:
josamoto
1 Solution
 
jhanceCommented:
How can you possibly use ANY of these TRIG functions properly if you don't understand the relationship between DEGREES and RADIANS?  This is so fundamental (and also so simple) that I'm not even going to give it to you.  

But here is a hint: "How many degrees are there in a circle?  Ok, how many radians are there in a circle?"  

If you know these two things you can derive the relationship.

This is basic (8th Grade around here) algebra!!!

By the way, you have a 0.000 average here on EE when it comes to closing questions.  Your question from Feb. 2002 is STILL OPEN:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=3dgames&qid=20263123

Please resolve this.
0
 
jkrCommented:
>>Some MSDN documentation told me to times radians by (180/pi)

Use 180.0/pi, otherwise you will get incorrect results, e.g.

double rad2deg ( double r) {

 return r * 180 / 3.1415926535;
}

See the VC++ docs on "sin()":

Example

/* SINCOS.C: This program displays the sine, hyperbolic
 * sine, cosine, and hyperbolic cosine of pi / 2.
 */

#include <math.h>
#include <stdio.h>

void main( void )
{
   double pi = 3.1415926535;
   double x, y;

   x = pi / 2;
   y = sin( x );
   printf( "sin( %f ) = %f\n", x, y );
   y = sinh( x );
   printf( "sinh( %f ) = %f\n",x, y );
   y = cos( x );
   printf( "cos( %f ) = %f\n", x, y );
   y = cosh( x );
   printf( "cosh( %f ) = %f\n",x, y );
}


Output

sin( 1.570796 ) = 1.000000
sinh( 1.570796 ) = 2.301299
cos( 1.570796 ) = 0.000000
cosh( 1.570796 ) = 2.509178

0
 
GregToombsCommented:
Your problem may have been that in your conversion formula, you were using integer values instead of double values, as jkr suggested.
To guarantee that all of the numerical constants you're using are interpreted as doubles, write

180.0L
instead of 180.0

The "L" postfix (double) and "F" postfix (float) let you satisfy C's nasty appetite for explicit instructions.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jonninCommented:
I would not let the compiler divide for me, but whatever...

#define DTR 0.017453292519943295769236907684886
#define RTD 57.295779513082320876798154814105
#define pi 3.1415926535897932384626433832795
#define e 2.7182818284590452353602874713527

0
 
havman56Commented:
I support jhance comments...

first study basic maths then do programing...

if u dont know the relation between radians and degrees
waste of using a computer to program.

0
 
josamotoAuthor Commented:
_uck all of you with your sarcastic insultent comments. First of all I KNOW THE DIFFERENCE BETWEEN DEGREES AND RADIANS, I HAVE THE FORMULAS OF CONVERSION FOR BOTH, maby you guys need to go and enhance your English comprehension skills, maby go back to creche to learn to "speek ingleesh!"

I didn't ask the for the formulas of conversion first of all, or at all the relation between degrees and radians. My question was concerning the accuracy obtained when using C functions, they are not the same as those values received when performing the exact formula with a scientific calculator!

To GregTooms, ten out of ten, thanks for your tip, as we can see, simple problems have often simple solutions!
0
 
josamotoAuthor Commented:
Thanx Greg, I'll keep your comment in mind whenever I use mathematical formulas in VC++ again.

You could email me at josamoto@hotmail.com, I'd like to know what your interests in programming are!
0
 
GregToombsCommented:
You're very welcome, and I emailed you.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now