Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C, tan(30) gives me a negative value! Why?

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?

like image 515
Hari Krishna Ganji Avatar asked Jun 25 '12 18:06

Hari Krishna Ganji


People also ask

Why is tan coming out negative?

for angles with their terminal arm in Quadrant II, since sine is positive and cosine is negative, tangent is negative.

Is tan 30 negative?

Explanation: For tan 30 degrees, the angle 30° lies between 0° and 90° (First Quadrant). Since tangent function is positive in the first quadrant, thus tan 30° value = 1/√3 or 0.5773502. . .

What if tan is negative?

The two equations disclose that tan of negative angle is always equal to negative of tangent of angle. It is called tangent of negative angle identity and used as a formula in trigonometric mathematics.

What is the value of tangent 30?

Tan 30 degree is equal to 1/√3 and its exact value is 0.57735.


1 Answers

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
like image 163
K-ballo Avatar answered Oct 04 '22 07:10

K-ballo