C++ Trigonometric functions
C++ comes with handy Trigonometric functions. They are in math.h header file. During coding contests they are very useful whenver, we need to do some trigonometric calclculations.
Here is a list of C++ Trigonometric functions.
- sin
It takes an angle (in radians) and returns its sine value. Here is an example of it.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 90.0;
cout << "Sine(90) = "
<< sin(x) << endl;
return 0;
}
Sine(90) = 0.893997
- cos
It takes angle (in radians) as an argument and return its cosine value.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 2.1;
cout << "cos(2.1) "
<< cos(x) << endl;
return 0;
}
cos(2.1) -0.504846
- Tan
It takes angle (in radians) as an argument and return its tangent value.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 2.1;
cout << "Tan(2.1) "
<< tan(x) << endl;
return 0;
}
Tan(2.1) -1.70985
- acos: It returns arc cosine of argument given. The argument to acos must be in the range -1 to 1; otherwise, a domain error occurs.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 1.0;
cout << "Arc Cosine(1.0) "
<< acos(x) << endl;
return 0;
}
Arc Cosine(1.0) 0
- asin: It returns the arcsine of argument. The argument to asin must be in the range -1 to 1; otherwise, a domain error occurs.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 1.0;
cout << "Arc Sine (1.0) = "
<< asin(x) << endl;
return 0;
}
Arc Sine (1.0) = 1.5708
- atan: It returns the arc tangent of passed argument.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 1.0;
cout << "Arc Tangent (1.0) = "
<< atan(x) << endl;
return 0;
}
Arc Tangent (1.0) = 0.785398
- atan2: This function returns the arc tangent of (a)/(b)
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double a = 2.1, b = 1.0;
cout << "Arc Tangent 2 a = 2.3 and b = 1.0: "
<< atan2(a, b) << endl;
return 0;
}
Arc Tangent 2 a = 2.3 and b = 1.0: 1.12638
- cosh: This function returns the hyperbolic cosine of argument provided. The value of the argument provided must be in degrees.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 54.2; // in degrees
cout << "Hyperbolic Cosine (54.2) = "
<< cosh(x) << endl;
return 0;
}
Hyperbolic Cosine (54.2) = 1.72874e+23
- tanh: This function returns the hyperbolic tangent of argument provided. The value of argument provided must be in degrees.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 54.2; // in degrees
cout << "Hyperbolic Tangent (54.2) = "
<< tanh(x) << endl;
return 0;
}
Hyperbolic Tangent (54.2) = 1