weird output using cos

This is a discussion on weird output using cos within the C++ Programming forums, part of the General Programming Boards category; Hi, using the following code: Code: #include <cmath> #include <iostream> using namespace std; int main(){ double z = 0; double ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    30

    weird output using cos

    Hi,

    using the following code:

    Code:
    #include <cmath>
    #include <iostream>
    
    using namespace std;
    
    int main(){
    
    double z = 0;
    double z2 = 180;
    z = cos(z2);
    
    cout << z;
    
    return 0;
    }
    The result should be -1 but it gives me out a value of -0,59..

    Anyone knows the problem?


    Thanks in advance!

    ~Jan

  2. #2
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    the argument to cos isn't in degrees, it's in radians
    Convert degrees to radians using:
    radian=degree*(pi/180)

    so

    Code:
    #define _USE_MATH_DEFINES //to define M_PI
    #include <math>
    #include <iostream>
    
    using namespace std;
    
    int main(){
    
    double z = 0;
    double z2 = 180;
    z2*=(M_PI/180.f) //z2 is now 180 deg in radians
    z = cos(z2);
    
    cout << z;
    
    return 0;
    }
    /btq
    ...viewlexx - julie lexx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird output of scanf and printf.
    By omnificient in forum C Programming
    Replies: 2
    Last Post: 12-05-2007, 01:28 PM
  2. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 04:56 AM
  3. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 03:18 AM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 07:46 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

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