Solved

Trigonometry problem with radians and degrees

Posted on 2002-05-26
8
198 Views
Last Modified: 2010-04-02
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
Comment
Question by:josamoto
8 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 7035588
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
 
LVL 86

Expert Comment

by:jkr
ID: 7035594
>>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
 

Accepted Solution

by:
GregToombs earned 50 total points
ID: 7037841
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
 
LVL 2

Expert Comment

by:jonnin
ID: 7040472
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:havman56
ID: 7042080
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
 

Author Comment

by:josamoto
ID: 7111732
_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
 

Author Comment

by:josamoto
ID: 7111738
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
 

Expert Comment

by:GregToombs
ID: 7112727
You're very welcome, and I emailed you.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now