Updated March 17, 2023
Introduction to Math Functions in C++
C++ provides <math.h> library for math functions to perform the complex mathematical functions like trigonometric function, algebraic equations easily. For example, sin() function is used to calculate the value of sin, pow() the function is used to calculate the power of the value, sqrt is used to calculate the square root of the value.
Different Types of Math Functions
C++ provides a huge number of different types of math functions mentioned below with examples:
1. Maximum & Minimum function
- max (p,q): It will return a maximum number between p and q.
- min (p,q): It will return a minimum number between p and q.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << max(16,18) << "\n";
cout << min(16,18) << "\n";
return 0;
}
Output:
2. Power functions
- pow (m,n): It will calculate m raised to the power n.
- sqrt(m): It will calculate the square root of m.
- cbrt(n): It will calculate the cube root of n.
- hypot(m,n): It will calculate the hypotenuse of the right-angled triangle.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << pow(2,3) << "\n";
cout << sqrt(16) << "\n";
cout << cbrt(27) << "\n";
cout << hypot(3,4) << "\n";
return 0;
}
Output:
3. Exponential functions
- exp(p): It will calculate the exponential e raised to power p.
- log(p): It will calculate the logarithm of p.
- log10(p): It will calculate the common logarithm of p.
- exp2(p): It will calculate the base 2 exponential of p.
- log2(p): It will calculate the base 2 logarithm of p.
- logb(p): It will calculate the logarithm of p.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << exp(5) << "\n";
cout << log(8) << "\n";
cout << log10(8) << "\n";
cout << exp2(5) << "\n";
cout << log2(8) << "\n";
cout << logb(8) << "\n";
return 0;
}
Output:
4. Integer functions
It helps in finding the nearest integer value.
- ceil(z): it rounds up the value of z.
- floor(z): it rounds down the value of z.
- round(z): It rounds off the value of z.
- fmod(z,y): It calculates the remainder of division z/y.
- trunc(z): It will round off the z value towards zero.
- rint(z): It will round off the z value using rounding mode.
- nearbyint(z): It will round off the z value to a nearby integral value.
- remainder(z,y): It will calculate the remainder of z/y.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << ceil(4580.01) << "\n";
cout << floor(151.999) << "\n";
cout << round(518.5) << "\n";
cout << fmod(5,21) << "\n";
cout << trunc(20.25) << "\n";
cout << rint(21.25) << "\n";
cout << nearbyint(182.55) << "\n";
cout << remainder(12,36) << "\n";
return 0;
}
Output:
5. Comparison functions
Help in comparing numbers in a quick span doesn’t matter how long the number is. Below are a few examples of Comparison functions:
- isgreater(p,q): It checks whether p is greater than q or not.
- islessequal(p,q): It checks whether p is less than or equal to q or not.
- isgreaterequal(p,q): It checks whether p is greater than or equal to q or not.
- islessgreater(p,q): It checks whether p is less or greater than y or not.
- isunordered(p,q): It checks whether p compared or not.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
// cout << less(22,29) << "\n";
cout << isgreater(48,47)<< "\n";
cout << islessequal(11,5)<< "\n";
cout << isgreaterequal(19,72)<< "\n";
cout << islessgreater(59,84)<< "\n";
cout << isunordered(62,84)<< "\n";
return 0;
}
Output:
6. Using Trigonometric Function
Functions specially used in geometric calculations. The right-angled triangle gives a relation between angle to the ratio of the length of the two sides.
- sin(y): It will calculate the value of sine y.
- cos(y): It will calculate the value of cosine y.
- tan(y): It will calculate the value of tangent y.
- asin(y): It will calculate the value of inverse sine y.
- acos(y): It will calculate the value of inverse cosine y.
- atan(y): It will calculate the value of inverse tangent y.
- atan2(y,x): It will calculate the value of the inverse tangent of y and x coordinates.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << sin(0) << "\n";
cout << cos(0) << "\n";
cout << tan(1) << "\n";
cout << asin(1)<< "\n";
cout << acos(0)<< "\n";
cout << atan(1)<< "\n";
cout << atan2(0,1)<< "\n";
return 0;
}
Output:
Here are some more interesting functions that will help in calculating values of hyperbolic trigonometric functions and they are called Hyperbolic functions.
- sinh(x): It will calculate the value of the hyperbolic sine of x.
- cosh(x): It will calculate the value of the hyperbolic cosine of x.
- tanh(x): It will calculate the value of the hyperbolic tangent of x.
- asinh(x): It will calculate the value of the hyperbolic arc sine of x.
- acosh(x): It will calculate the value of the hyperbolic arc cosine of x.
- atanh(x): It will calculate the value of the hyperbolic arc sine of x.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << sinh(0)<< "\n";
cout << cosh(0)<< "\n";
cout << tanh(1)<< "\n";
cout << asinh(1)<< "\n";
cout << acosh(1)<< "\n";
cout << atanh(0)<< "\n";
return 0;
}
Output:
Conclusion
Math functions play an important role in saving a huge amount of time and space in memory. All the functions are built-in, no need to implement directly use any math function just by adding a header file which will give the option to use the whole library of math class.
Recommended Articles
This is a guide to Math Functions in C++. Here we discuss the C++ provides a huge number of different types of math functions with examples. You can also go through our other suggested articles –