Why do I get wrong values when I use the sin and cos functions?

This is a discussion on Why do I get wrong values when I use the sin and cos functions? within the C++ Programming forums, part of the General Programming Boards category; Like if I I put Code: double a = cos(45); I get that a = .525322 when a should be ...

  1. #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?

  2. #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)

  3. #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

  4. #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?

  5. #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?

  6. #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.

  7. #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));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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