CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
Aug 1999
Location
Gastonia, NC
Posts
391

## sin cos tan

I'm trying to do some pie chart math,
and doing so I'm using the sin() and cos()
functions.

My delima is, if I use sin() on an angle of 18
for example, the sin() function returns

-0.75098724677168

if you put 18 in Calculator and press the SIN
button, it gives you

0.30901699437494742410229341718282

The one from Calculator is the one I'm after
to get my math right, what's up?

Also, if you go to the def of sin in MSDN
it shows this little snipplet:

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

#include &lt;math.h&gt;
#include &lt;stdio.h&gt;

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

If I punch in the numbers in Calculator, none of
this works out right. Even based on solely the
bottom output, Calc says the sin(1.570796) is
0.027412127890540702705615104925336 and not
1.000000000.

Thanks...

*DM*
AIM/AOL: EdisonCPP
ICQ: 39184886 - ID EdisonCPP
Email: edisoncpp@carolina.rr.com
sedison@protronix.com

2. Elite Member Power Poster
Join Date
Aug 2000
Location
West Virginia
Posts
7,712

## Re: sin cos tan

It should have an option to change this.

3. Member
Join Date
Jul 2001
Location
Otaki, New Zealand
Posts
303

## Re: sin cos tan

The difference is that what you get when you do sin(18) is in fact sin (18 Radians) which is different from sin(18 degrees).

If you put your calculator into Rad mode and then repeat sin(18) you get the answer -0.75098.

A lot of maths calculations are based on Radians and are expressed in terms of pi.

2*pi = 360 degrees
=&gt;
pi = 180 degrees
pi/2 = 90 degrees
sin(90) = sin(pi/2) = 1.0

The MSDN help for sin does sya that the parameter x is the 'Angle in Radians'

Regards
Alan

4. Elite Member Power Poster
Join Date
Aug 2000
Location
Los Angeles
Posts
4,658

## Re: sin cos tan

sin is work with radians and your calcularor - with degrees! Set calculator to radians and see the same results

Rating isn't important...But gurus respect it and keep high

5. Member
Join Date
Aug 1999
Location
Gastonia, NC
Posts
391

## Re: sin cos tan

So how do I use Degrees with sin()?
You were right about calc.exe, it
then gave the same result.

*DM*
AIM/AOL: EdisonCPP
ICQ: 39184886 - ID EdisonCPP
Email: edisoncpp@carolina.rr.com
sedison@protronix.com

6. Member
Join Date
Jul 2001
Location
Otaki, New Zealand
Posts
303

## Re: sin cos tan

Store it as a constant and multiply before calling sin,cos etc.

7. Elite Member Power Poster
Join Date
Aug 2000
Location
West Virginia
Posts
7,712

## Re: sin cos tan

float angle_in_degrees = 18.0f;
//
// convert degrees to radians ... 3.14159 (PI) radians = 180 degrees
//
float angle_in_radians = angle_in_degrees * 3.14159f/ 180f;
//
//
//

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•