Math
A wide range of mathematical functions can be found in the standard C++ math library, cmath. These capabilities are executed as capabilities or layouts and can be utilized in any C++ program.
Give some type in below:
- sqrt()
- pow()
- sin(),cos(),tan()
Sqrt():
The square root of the argument is returned by this function.
Example:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
double x = 25.0;
double y = sqrt(x);
cout << "Square root of " << x << " is " << y << endl;
return 0;
}
Output:
Square root of 25 is 5
pow():
The result of raising the base to the exponent's power is returned by this function, which takes the base and the exponent as arguments.
Example:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
double x = 2.0;
double y = 3.0;
double z = pow(x, y);
cout << x << " raised to the power of " << y << " is " << z << endl;
return 0;
}
Output:
2 raised to the power of 3 is 8
Sin() ,cos() ,tan():
The argument's sine, cosine, and tangent are returned by these functions, respectively. Radians are used in the argument.
Example:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
double x = M_PI / 6.0; // 30 degrees in radians
double y = sin(x);
double z = cos(x);
double w = tan(x);
cout << "Sine of " << x << " radians is " << y << endl;
cout << "Cosine of " << x << " radians is " << z << endl;
cout << "Tangent of " << x << " radians is " << w << endl;
return 0;
}
Output:
Sine of 0.523599 radians is 0.5
Cosine of 0.523599 radians is 0.866025
Tangent of 0.523599 radians is 0.57735