HOWTO · C++
Trigonometric Functions in C++
This article demonstrates how to utilize trigonometric functions of STL in C++
On this page
C++ provides sine, cosine, tangent, inverse trigonometric, and hyperbolic functions in the standard <cmath> header. The ordinary trigonometric functions take angles in radians and return floating-point values. Use the std::-qualified names so the code clearly refers to the standard library.
Use std::sin, std::cos, and std::tan for angles in radians
Call std::sin(angle), std::cos(angle), and std::tan(angle) to calculate the sine, cosine, and tangent of an angle. Include <cmath> for their declarations. These functions have overloads for floating-point types; the result uses the corresponding floating-point type (integer arguments are converted to a floating-point overload).
Run this example:
#include <cmath>
#include <iomanip>
#include <iostream>
int main() {
const double angle = 1.0; // radians
std::cout << std::fixed << std::setprecision(6)
<< "sin(" << angle << ") = " << std::sin(angle) << '\n'
<< "cos(" << angle << ") = " << std::cos(angle) << '\n'
<< "tan(" << angle << ") = " << std::tan(angle) << '\n';
}
The observed output for this run is:
sin(1.000000) = 0.841471
cos(1.000000) = 0.540302
tan(1.000000) = 1.557408
The argument is one radian. std::fixed and std::setprecision(6) format each returned value with six digits after the decimal point.
Use inverse trigonometric functions to recover angles
Use std::asin(x) to recover an angle from a sine value, std::acos(x) from a cosine value, and std::atan(x) from a tangent value. Their results are in radians. asin accepts values from -1 through 1 and returns an angle in [-π/2, π/2]; acos has the same input range and returns an angle in [0, π]. atan accepts any finite real value and returns an angle in (-π/2, π/2).
For a direction described by both coordinates, use std::atan2(y, x). Unlike atan(y / x), it uses the signs of both arguments to select the quadrant and does not require you to divide the coordinates first. Its result is an angle in radians, conventionally in [-π, π] (the exact endpoint representation can depend on the signed zero input).
Inverse functions return a principal angle, not every angle that could have produced the original value. For example, several angles share a sine value, so asin alone cannot recover the original angle outside its principal range. Use the geometry and quadrant information from your problem when choosing a final angle.
Use hyperbolic functions for hyperbolic values
The functions std::sinh, std::cosh, and std::tanh calculate the hyperbolic sine, cosine, and tangent. They are defined from exponential functions and are not substitutes for std::sin, std::cos, and std::tan: their inputs are real values, not angles on a circle, and their ranges differ. For example, cosh(x) is at least 1, while ordinary cosine stays between -1 and 1.
The inverse hyperbolic functions are std::asinh, std::acosh, and std::atanh. Their real-valued domains differ: asinh accepts any real input, acosh requires an input at least 1, and atanh requires an input strictly between -1 and 1. Choose these functions for hyperbolic relationships rather than adding an a to an ordinary trigonometric function name.
Handle angle units, domains, and floating-point results
C++ <cmath> trigonometric functions expect radians. Convert degrees with radians = degrees * π / 180; for example, 90 degrees is π/2 radians. In portable C++, obtain π with std::acos(-1.0) or define the constant you need, since M_PI is not a standard C++ name.
Inverse-function inputs must be within their real domains. Check or clamp a value only when that matches the mathematics of your application: clamping a value just outside [-1, 1] can account for accumulated rounding error, but it should not conceal a genuinely out-of-range input. A domain error is handled according to the C++ math library’s error behavior and implementation; it is not undefined behavior. If your application needs to distinguish a valid result from a math error, consult math_errhandling and check the relevant error indicators supported by the implementation.
std::tan grows very large near odd multiples of π/2 and has no finite real value at those mathematical poles. A floating-point approximation to a pole may produce a large finite result rather than a convenient error signal, so test the input against the needs and tolerance of your application if near-pole behavior matters.
Floating-point calculations are approximations. Values such as std::sin(std::acos(-1.0)) may be close to, but not exactly, zero. Compare computed values using an application-appropriate tolerance rather than exact equality. Use double for ordinary calculations and choose a wider type only when its precision and support are needed. Prefer the standard library over hand-written approximations, and store a computed value when you reuse it.