format_list_bulleted
[C++] Explanation of how to use trigonometric functions (sin, cos, etc.) using math.h
最終更新日時:2022-07-01 06:19:07



This article explains how to use trigonometric functions using the C++ math.h library. In addition, for an explanation of basic functions using math.h other than trigonometric functions, please refer to "[C++] Functions for basic calculations(power, absolute value, square root) using math.h".

math.h

math.h is a library that enables calculations such as those described in the title. In addition to these, calculations of trigonometric functions such as sin, cos, etc. are also possible with this library. To use it, first load the header file as follows

#include<math.h>

Introduction to Trigonometric Functions

The following table shows the functions corresponding to each calculation. The table shows that for all functions, the return type is double.

Calculation Methods
functionreturn
sinsin(x)
double
coscos(x)
double
tantan(x)
double

Sample code

#include <iostream>
#include<math.h>
using namespace std;

int main(){

    cout << "calculate sin" << endl;
    cout << sin(2*M_PI) << endl; //Calculate sin
    cout << sin(1.5*M_PI) << endl; //calculate sin
    cout << "calculate cos" << endl;
    cout << cos(1.5*M_PI) << endl; //calculate cos
    cout << cos(1.5*M_PI) << endl; //calculate cos
    cout << "calculate tan" << endl;
    cout << tan(1.5*M_PI) << endl; //calculate tan
    cout << tan(1.5*M_PI) << endl; //calculate tan
    return 0;
}

Result

Calculate sin
-2.44929e-16
-1
Calculate cos
-1.83697e-16
-1.83697e-16
Calculate tan
5.44375e+15
5.44375e+15

Conclusion

This article explained how to calculate trigonometric functions in C++. The contents are summarized at the end.

  • Trigonometric functions can be calculated by using math.h
  • When performing arithmetic operations, the return value is of type double

In addition to these calculations, there are other calculations that can be performed with math.h, which will also be introduced in the future.