Solved

Trigonometry problem with radians and degrees

Posted on 2002-05-26
8
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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!

 
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
 
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX TCameraComponent Problem 2 106
Android development question 2 110
C++ help/ Toy problem 19 71
Coding for the first time 9 103
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 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