tan() Function

The tan() function computes the tangent of an angle expressed in radians. This function is part of the C standard math library and is used to determine the ratio of the sine to the cosine of a given angle. It is important to note that if the cosine of the angle is zero, the tangent is undefined.


Syntax of tan()

</>
Copy
double tan(double x);
float tanf(float x);
long double tanl(long double x);

Parameters

ParameterDescription
xAngle in radians. (One radian equals 180/PI degrees.)

After specifying the parameters, it is worth mentioning that the tangent function may result in undefined values when the cosine of the angle is zero. Developers should ensure that the input values are within a range that avoids such singularities.

Return Value

The function returns the tangent of the given angle in radians.


Examples for tan()

Example 1: Calculating the Tangent of 45 Degrees

This example demonstrates how to compute the tangent of 45 degrees (which is π/4 radians) using tan().

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double angle = 3.14159 / 4; // Approximately 45 degrees in radians
    double result = tan(angle);
    printf("Tangent of 45 degrees: %f\n", result);
    return 0;
}

Explanation:

  1. The program includes the required headers stdio.h and math.h.
  2. The angle is set to π/4 radians, which corresponds to 45 degrees.
  3. The tan() function computes the tangent of the angle.
  4. The result is printed to the console.

Program Output:

Tangent of 45 degrees: 0.999999

Example 2: Evaluating the Tangent of a Negative Angle

This example demonstrates the use of tan() with a negative angle to show that the function correctly computes negative tangents.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double angle = -3.14159 / 6; // Approximately -30 degrees in radians
    double result = tan(angle);
    printf("Tangent of -30 degrees: %f\n", result);
    return 0;
}

Explanation:

  1. The program sets a negative angle (approximately -30 degrees in radians).
  2. The tan() function computes the tangent of this negative angle.
  3. The result is displayed, demonstrating that the tangent value is negative.

Program Output:

Tangent of -30 degrees: -0.577350

Example 3: Using tan() for a Small Angle Approximation

This example demonstrates the computation of the tangent of a small angle, which can be useful in approximations where tan(x) is nearly equal to x.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double angle = 0.1; // Small angle in radians
    double result = tan(angle);
    printf("Tangent of 0.1 radians: %f\n", result);
    return 0;
}

Explanation:

  1. The program defines a small angle (0.1 radians).
  2. The tan() function is used to compute the tangent of the small angle.
  3. The result is printed, which should be very close to the angle value itself for small angles.

Program Output:

Tangent of 0.1 radians: 0.100335

Example 4: Comparing tan(), tanf(), and tanl()

This example illustrates the use of different variants of the tangent function: tan() for double, tanf() for float, and tanl() for long double.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double angle_d = 0.785398;      // Approximately 45 degrees in radians (double)
    float angle_f = 0.785398f;       // Float version
    long double angle_ld = 0.785398L; // Long double version

    double result_d = tan(angle_d);
    float result_f = tanf(angle_f);
    long double result_ld = tanl(angle_ld);

    printf("tan(45°) using double: %f\n", result_d);
    printf("tan(45°) using float: %f\n", result_f);
    printf("tan(45°) using long double: %Lf\n", result_ld);

    return 0;
}

Explanation:

  1. The program defines an angle of approximately 45 degrees in three different precision types: double, float, and long double.
  2. It computes the tangent of the angle using tan(), tanf(), and tanl() respectively.
  3. The results are printed to compare the output across different data types.

Program Output:

tan(45°) using double: 1.000000
tan(45°) using float: 1.000000
tan(45°) using long double: 1.000000