Cos returns incorrect values?

I have a strange problem with the standard cos cmath / math.h function. Apparently, in some circumstances, it returns an incorrect or just an undefined value.

#include <cmath>
#include <iostream>

int main()
{
 double foo = 8.0 * 0.19634955; // 1.5707964
 double bla = std::cos(foo);    // should be 0.9996242168245
 std::cout << bla << std::endl; // cos returns -7.32051e-008

 return 0;
}

If the input value for cos is 1.5707964, for example, cos returns -7.32051e-008 (doubles when using, with floats -4.XYZe-009).

I missed something really simple and simple here ...?

+3
source share
3 answers

cos expects radians, you give him degrees. Multiply your input value by 3.14159 / 180 and you will get the correct answer.

+23
source

cos (PI / 2) = 0, not 1.

+2
source

I don’t know if you pass radians or degrees ... But your foo value is close to PI / 2. So you get cos (foo) = 0 and sin (foo) = 1 (what did you expect?)

+1
source

Source: https://habr.com/ru/post/1724865/


All Articles