-
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
-
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 :)