![]() |
| | #1 |
| Registered User Join Date: Jan 2003
Posts: 118
| Why do I get wrong values when I use the sin and cos functions? Code: double a = cos(45);
__________________ Why drink and drive when you can smoke and fly? |
| Marcos is offline | |
| | #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 | |
| | #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 | |
| | #4 |
| Registered User Join Date: Jan 2003
Posts: 118
| duh! of course haha, Thanks a lot for the help guys
__________________ Why drink and drive when you can smoke and fly? |
| Marcos is offline | |
| | #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 | |
| | #6 |
| Senior Member 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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |