Reputation: 1677
I observe that my tan(float)
function from the cmath
library is returning a negative value.
The following piece of code, when run :
#include <cmath>
....
// some calculation here gives me a value between 0.0 to 1.0.
float tempSpeed = 0.5;
float tanValue = tan(tempSpeed * 60);
__android_log_print(ANDROID_LOG_INFO, "Log Me", "speed: %f", tanValue);
Gives me this result in my Log file:
Log Me: speed `-6.4053311966`
As far as I remember
tan(0.5*60) = tan(30) = 1/squareroot(3);
Can someone help me here as in why I am seeing a negative value? Is it related to some floating point size error? Or am I doing something really dumb?
Upvotes: 13
Views: 8938
Reputation: 41
I guess in C the tan Function requires you to Input Radians as an argument and not The actual degree value.
so for Tan 30 , you would have to convert your 30 degrees to radian. Keep in Mind that 360 degrees is 2*Pi radian so 30 degress would be (1\6 * Pi)th of a radian.
so tan(1\6 * Pi) would give you the correct answer. where Pi is 3.142
Upvotes: 1
Reputation: 43472
That's the tangent of your angle (30 radians.) if you are looking for the tangent of 30 degrees, you must convert your angle to radians first.
Upvotes: 7
Reputation: 81379
In C, tan
and other trigonometric functions expect radians as their arguments, not degrees. You can convert degrees to radians:
tan( 30. * M_PI / 180. ) == 0.57735026918962576450914878050196
Upvotes: 46