I have a strange problem with the standard cos function of cmath/math.h. Apparently under some circumstances it returns a wrong or simply undefined value.
#i
cos expects radians, you are giving it degrees. Multiply your input value by 3.14159/180, and you will get the right answer.
cos(PI/2) = 0, not 1.
I don't know if you're passing radian or degrees... But your value of foo is near PI/2. So you get cos(foo) = 0 and sin(foo) = 1 (what you expected?)