C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-29-2004, 08:53 PM   #1
Registered User
 
Join Date: Jan 2003
Posts: 118
Why do I get wrong values when I use the sin and cos functions?

Like if I I put
Code:
double a = cos(45);
I get that a = .525322 when a should be = 7.07106. This happens with all thease kinds of funtions, whats happening?
__________________
Why drink and drive when you can smoke and fly?
Marcos is offline   Reply With Quote
Old 02-29-2004, 08:56 PM   #2
Registered User
 
Join Date: Feb 2004
Posts: 6
a = .525322 because the function cos takes in radians rather than degrees (which should be .707106, not 7.07106)
BoredCrzy is offline   Reply With Quote
Old 02-29-2004, 08:56 PM   #3
Registered User
 
Join Date: Apr 2002
Posts: 1,571
Those functions take radians not degrees.
__________________
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
MrWizard is offline   Reply With Quote
Old 02-29-2004, 09:00 PM   #4
Registered User
 
Join Date: Jan 2003
Posts: 118
Smile

duh! of course haha, Thanks a lot for the help guys
__________________
Why drink and drive when you can smoke and fly?
Marcos is offline   Reply With Quote
Old 02-29-2004, 09:05 PM   #5
Registered User
 
Join Date: Jan 2003
Posts: 118
oh and... so... like if I want to put cos(45°) (the degrees that you need 360 for a circle) how would I do it?
__________________
Why drink and drive when you can smoke and fly?
Marcos is offline   Reply With Quote
Old 02-29-2004, 09:21 PM   #6
Senior Member
 
joshdick's Avatar
 
Join Date: Nov 2002
Location: Phildelphia, PA
Posts: 1,146
You could write wrapper functions that do the conversions so that when you call them, you can always do it in degrees.
__________________
FAQ

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.
joshdick is offline   Reply With Quote
Old 02-29-2004, 09:47 PM   #7
Registered User
 
Join Date: Feb 2004
Posts: 6
As Joshdick mentioned, write a wrapper function that converts the degree measure into radians (degree * PI/180)

e.g.

Code:
double sin_d (double degrees)
{
     return (sin(degrees * 3.1416/180));
}
BoredCrzy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with cos, sin fuctions xxxhunter C Programming 7 11-16-2007 03:33 AM
Help with cos, sin and tan^-1 (arctan); xIcyx C Programming 15 04-23-2007 09:14 AM
Help creating function of sin cos and tan Niz C Programming 12 01-09-2007 05:55 PM
sin() and cos() that use degrees dwks C Programming 3 05-14-2005 04:29 PM
integral of 1/x Silvercord A Brief History of Cprogramming.com 43 03-19-2004 07:47 AM


All times are GMT -6. The time now is 04:48 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22