- Home /
Mathf.Cos error
Why is that Mathf.cos is not accurate??
Here is my code:
function Update () { print(Mathf.Cos(135)); }
unity will print -0.9960 but is should be -0.7071?!
I try it on my calculator?
Is this a bug?
by the way I'm using unity 3.4.0f5
Answer by syclamoth · Jan 20, 2012 at 03:33 AM
Mathf.Cos uses radians, not degrees. Try
Mathf.Cos(135 * Mathf.Deg2Rad);
and see what you get!
jep, 135 modulo PI is 3.053...
converted to degree:
174.93...
cos(174,93...) = -0.9960...
Usually in most program$$anonymous$$g languages the trigonometric functions work with radians.
@syclamoth wow thank you very much now I get the right answer! I didn't know that $$anonymous$$athf.Cos uses radians, not degrees. I thought it is just the same as my calculator... lol
Your calculator probably uses radians, too- you just don't notice because it defaults to degrees!
Wow, thanks very much for pointing this out. I love Unity and I'd never switch engines, but man is the documentation incomplete. For $$anonymous$$athf.Cos, it reads: "static function Cos (f : float) : float Description
Returns the cosine of angle f in radians."
Nowhere does it specify that parameter "f" must be in radians - it just says the return type is radians! You basically just have to try them both and realize the values are bogus if you input degrees.
Haha, the return value isn't in radians, since that doesn't even make sense. Sine and Cosine do not return angles, they return a value between -1 and 1, so the only thing that the radians could be referring to is the input.
Your answer

Follow this Question
Related Questions
Can someone explain this MouseMove script to me :) 1 Answer
tangents from angle gives weird results 1 Answer
float precision, again 0 Answers
How to find inverse cos 1 Answer