Trigonometric math functions

Trigonometric functions are fundamental in various fields such as physics, engineering, and computer graphics. They describe the relationships between the angles and sides of triangles, making them crucial for solving problems involving periodic phenomena. The <cmath> module in C++ provides a robust set of trigonometric functions to facilitate these calculations. This article explores these functions in detail, showcasing their usage and practical applications.

Basic Trigonometric Functions

The basic trigonometric functions in <cmath> compute the sine, cosine, and tangent of an angle, where the angle is provided in radians.

sin(double x)

The sin(double x) function computes the sine of x, where x is an angle in radians.

#include <iostream>
#include <cmath>

int main() {
    double angle = M_PI / 6; // 30 degrees in radians
    std::cout << "sin(" << angle << ") = " << sin(angle) << std::endl; // Output: 0.5
    return 0;
}

cos(double x)

The cos(double x) function computes the cosine of x, where x is an angle in radians.

#include <iostream>
#include <cmath>

int main() {
    double angle = M_PI / 3; // 60 degrees in radians
    std::cout << "cos(" << angle << ") = " << cos(angle) << std::endl; // Output: 0.5
    return 0;
}

tan(double x)

The tan(double x) function computes the tangent of x, where x is an angle in radians.

#include <iostream>
#include <cmath>

int main() {
    double angle = M_PI / 4; // 45 degrees in radians
    std::cout << "tan(" << angle << ") = " << tan(angle) << std::endl; // Output: 1
    return 0;
}

Inverse Trigonometric Functions

Inverse trigonometric functions compute the angle in radians for a given trigonometric value.

asin(double x)

The asin(double x) function computes the arc sine of x. The result is an angle in radians.

#include <iostream>
#include <cmath>

int main() {
    double value = 0.5;
    std::cout << "asin(" << value << ") = " << asin(value) << " radians" << std::endl; // Output: 0.523599 radians
    return 0;
}

acos(double x)

The acos(double x) function computes the arc cosine of x. The result is an angle in radians.

#include <iostream>
#include <cmath>

int main() {
    double value = 0.5;
    std::cout << "acos(" << value << ") = " << acos(value) << " radians" << std::endl; // Output: 1.0472 radians
    return 0;
}

atan(double x)

The atan(double x) function computes the arc tangent of x. The result is an angle in radians.

#include <iostream>
#include <cmath>

int main() {
    double value = 1.0;
    std::cout << "atan(" << value << ") = " << atan(value) << " radians" << std::endl; // Output: 0.785398 radians
    return 0;
}

atan2(double y, double x)

The atan2(double y, double x) function computes the arc tangent of y / x, using the signs of both arguments to determine the correct quadrant of the angle. This is particularly useful for converting Cartesian coordinates to polar coordinates.

#include <iostream>
#include <cmath>

int main() {
    double y = 1.0;
    double x = 1.0;
    std::cout << "atan2(" << y << ", " << x << ") = " << atan2(y, x) << " radians" << std::endl; // Output: 0.785398 radians
    return 0;
}

Example: Calculating the Height of a Building

Consider an example where we need to calculate the height of a building using trigonometry. Suppose we measure the angle of elevation to the top of the building as 30 degrees from a point 50 meters away from the base.

#include <iostream>
#include <cmath>

int main() {
    double angle = 30.0 * M_PI / 180.0; // Convert angle to radians
    double distance = 50.0; // Distance from the point to the building
    double height = distance * tan(angle);
    std::cout << "The height of the building is " << height << " meters." << std::endl; // Output: 28.8675 meters
    return 0;
}