Solved

Trigonometry problem with radians and degrees

Posted on 2002-05-26
8
206 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

636 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