cos() returning wrong result?

This is a discussion on cos() returning wrong result? within the C++ Programming forums, part of the General Programming Boards category; Okay... I'm a bit confused as to why cos(330) returns -0.991199 and using a calculator I find cos (330) returns ...

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    cos() returning wrong result?

    Okay... I'm a bit confused as to why cos(330) returns -0.991199 and using a calculator I find cos (330) returns 0.866025403784438646763723170752936 (0.866)

    The result from the calculator is the one I want... why are these results different? What can I do to make cos() function differently?

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    hehe.. Soon after posting I found that cos() accepts radians not degrees. So I have to multiply by pi divided by 180 to convert to degrees..

    So my question has changed, is there any library which will return cos() in degrees so I don't have to perform this conversion every time?

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Sorry... I'm trying to go for max speed and I assumed there would be a library function that could do it better than I could

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by rmullen3
    Sorry... I'm trying to go for max speed and I assumed there would be a library function that could do it better than I could
    Nope, just make a macro that converts the number for you. This will make the conversion pretty fast. Also use cosf( ) if you do not need the double precision offered by cos( ). That will get you some extra speed. Also define them as intrisic if your compiler allows.
    "...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

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Nope, just make a macro that converts the number for you.
    Why use a macro in C++ when inline functions are just as good and far less cryptic?

    -Prelude
    My best code is written with the delete key.

  6. #6
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    ... and when the function already does it?

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Prelude
    >Nope, just make a macro that converts the number for you.
    Why use a macro in C++ when inline functions are just as good and far less cryptic?

    -Prelude
    Well I usually use them when I need a function to be typeless. Sure for this case you can use inline functions if you prefer.

    >>... and when the function already does it?

    Umm... the cos( ) and cosf( ) functions do not convert degrees to radians for you.
    "...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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with basic calculation program.
    By StateofMind in forum C Programming
    Replies: 18
    Last Post: 03-06-2009, 01:44 AM
  2. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  3. FFT returning values in wrong order
    By DavidP in forum C# Programming
    Replies: 3
    Last Post: 10-25-2007, 02:15 PM
  4. Sorting out cause of wrong response from class members
    By imCrushedByCode in forum C++ Programming
    Replies: 11
    Last Post: 04-18-2006, 01:30 AM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM

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