Trigonometric Functions in C++

Trigonometric Functions in C++

  1. Use the std::sin Function to Calculate Sine in C++
  2. Use the std::cos Function to Calculate Cosine in C++
  3. Use std::tan Function to Calculate Tangent for Given Radian Value

This article will explain how to use trigonometric functions of STL in C++.

Use the std::sin Function to Calculate Sine in C++

Trigonometric functions in C++ are provided under the header <cmath>. Generally, common mathematical functions have been inherited from the C language, but most of them are overloaded in C++ to be interoperable with different argument types.

In this case, we represent the std::sin function to calculate the sine for the given argument. The argument should be a value in radians, and if the function succeeds, the return value is in the range of [-1 ; +1]. Note that std::sin returns unmodified arguments if their value is +-0.

The following example code computes sine values for common angles.

#include <iostream>
#include <cmath>

using std::cout; using std::endl;

const double pi = std::acos(-1);

int main() {

    cout << "sin(pi) = " << std::sin(pi) << '\n'
          << "sin(pi/6) = " << std::sin(pi/6) << '\n'
          << "sin(pi/4) = " << std::sin(pi/4) << '\n'
          << "sin(pi/3) = " << std::sin(pi/3) << '\n'
          << "sin(pi/2) = " << std::sin(pi/2) << '\n'
          << "sin(+0) = " << std::sin(0.0) << '\n'
          << "sin(-0) = " << std::sin(-0.0) << '\n';

    return EXIT_SUCCESS;
}
sin(pi) = 1.22465e-16
sin(pi/6) = 0.5
sin(pi/4) = 0.707107
sin(pi/3) = 0.866025
sin(pi/2) = 1
sin(+0) = 0
sin(-0) = -0

Use the std::cos Function to Calculate Cosine in C++

std::cos is another core trigonometric function, and it has similar characteristics as the std::sin except that of the returned values for the same arguments. Notice that all trigonometric functions can accept the value of angle as a floating-point number of integer, but the corresponding result is always returned as floating-point one.

#include <iostream>
#include <cmath>

using std::cout; using std::endl;

const double pi = std::acos(-1);

int main() {

    cout << "cos(pi) = " << std::cos(pi) << '\n'
         << "cos(pi/6) = " << std::cos(pi/6) << '\n'
         << "cos(pi/4) = " << std::cos(pi/4) << '\n'
         << "cos(pi/3) = " << std::cos(pi/3) << '\n'
         << "cos(pi/2) = " << std::cos(pi/2) << '\n'
         << "cos(+0) = " << std::cos(0.0) << '\n'
         << "cos(-0) = " << std::cos(-0.0) << '\n';

    return EXIT_SUCCESS;
}
cos(pi) = -1
cos(pi/6) = 0.866025
cos(pi/4) = 0.707107
cos(pi/3) = 0.5
cos(pi/2) = 6.12323e-17
cos(+0) = 1
cos(-0) = 1

Use std::tan Function to Calculate Tangent for Given Radian Value

On the other hand, we have the std::tan function to compute the tangent values for given arguments. Since these functions return floating-point values, there is a chance that some math error exceptions may be raised, which are described in detail here. Additionally, we have arc versions for each trigonometric function, and they have a prefix added to the original function names.

#include <iostream>
#include <cmath>

using std::cout; using std::endl;

const double pi = std::acos(-1);

int main() {

    cout << "tan(pi) = " << std::tan(pi) << '\n'
         << "tan(pi/6) = " << std::tan(pi/6) << '\n'
         << "tan(pi/4) = " << std::tan(pi/4) << '\n'
         << "tan(pi/3) = " << std::tan(pi/3) << '\n'
         << "tan(pi/2) = " << std::tan(pi/2) << '\n'
         << "tan(+0) = " << std::tan(0.0) << '\n'
         << "tan(-0) = " << std::tan(-0.0) << '\n';

    return EXIT_SUCCESS;
}
tan(pi) = -1.22465e-16
tan(pi/6) = 0.57735
tan(pi/4) = 1
tan(pi/3) = 1.73205
tan(pi/2) = 1.63312e+16
tan(+0) = 0
tan(-0) = -0

Related Article - C++ Math

  • Calculate Exponent Without Using pow() Function in C++
  • Intersection of Ray and Plane in C++
  • C++ Cube Root
  • Find Square Root Using Babylonian Method in C++
  • Magic Square Problem in C++
  • Division in C++