 # Mathematical functions in C++

May 29, 2023, Learn eTutorial
219

As it is a superset of C, C++ provides a wide range of beneficial mathematical functions. Standard C++ and C provide these functions to support a variety of mathematical operations. These functions can be used directly to simplify code as well as programs rather than focusing on implementation. The list of mathematical functions that C++ offers is as follows:

You must include either the `<math.h>` or the `<cmath>` header files in order to utilize these functions. You may conduct mathematical operations on numbers using a variety of functions in C++.

We will learn what a mathematical function is in this tutorial, and we will use examples to demonstrate how to apply them in a C++ program.

## What do you mean by a mathematical function in C++?

A math function is defined as a connection that will map elements from one set (A) to another (B), with each element of the first set mapped to only one element of the second set. This is also true for C/C++ (as well as other programming languages), where you have an input set as well as an output set.

The mathematical functions that are part of the math or cmath package can be used to do computations in the C++ programming language. These mathematical functions are mainly designed in order to do difficult mathematical calculations.

The library in C++ makes it simple to conduct difficult mathematical operations like solving algebraic equations and trigonometric functions. For example, the sin() function is used in order to determine the value of sin, the pow() function determines the value's power, as well as the sqrt function, determines the value's square root.

## Different Types of Mathematical Functions C++ supports a wide range of arithmetic functions, which are listed below with examples:

## 1. The maximum and minimum functions

Function Description
max (p,q) max (p,q) returns the highest/ or maximum number that can be found between p and q.
min (p,q) It will provide the lowest value/or minimum number between p and q if you enter min (p,q).

### C++ Code in order to Implement the Functionality Above

``````
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << max(19,22) << "\n";
cout << min(19,22) << "\n";
return 0;
}
```
```

Output:

```
22
19
```

## 2. Power functions

Function Description
pow (m,n) this function will compute m raised to the power of n.
sqrt(m) this function will compute the square root of m.
cbrt(n) this function will compute the cube root of n.
hypot(m,n) this function will compute the hypotenuse of a right-angled triangle.

### C++ Code in order to Implement the Functionality Above

``````
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << pow(2,9) << "\n";
cout << sqrt(20) << "\n";
cout << cbrt(3) << "\n";
cout << hypot(8,10) << "\n";
return 0;
}
```
```

Output:

```
512
4.47214
1.44225
12.8062
```

## 3. Exponential functions

Function Description
log(p) this function is used to calculate the logarithm of p.
log10(p) this function is used to calculate the common logarithm of p.
log10(p) this function is used to calculate the common logarithm of p.
exp2(p) this function is used to calculate the base 2 exponential of p.
log2(p) this function is used to calculate the base 2 logarithm of p.
logb(p) this function is used to calculate the logarithm of p.

### C++ Code in order to Implement the Functionality Above

``````
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << exp(10) << "\n";
cout << log(6) << "\n";
cout << log10(6) << "\n";
cout << exp2(50) << "\n";
cout << log2(4) << "\n";
cout << logb(4) << "\n";
return 0;
}
```
```

Output:

```
22026.5
1.79176
0.778151
1.1259e+15
2
2
```

## 4. Integer functions

The nearest integer value can be found with its assistance.

Function Description
ceil(z) this function mainly rounds up the value of z.
floor(z) this function will round down the value of z.
round(z) this function mainly rounds off the value of z.
fmod(z,y) By dividing z by y, this function determines the remainder.
trunc(z) The z value will be rounded off by this function to zero.
rint(z) The z value will be rounded off using the rounding mode.
remainder(z,y) this function will mainly calculate the remainder of z/y.
nearbyint(z) The z value will be rounded off to the nearest integral value.

### C++ Code in order to Implement the Functionality Above

``````
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << ceil(8450.01) << "\n";
cout << floor(952.777) <<< "\n";
cout << round(918.5) << "\n";
cout << fmod(9,71) << "\n";
cout << trunc(70.95) << "\n";
cout << rint(21.25) << "\n";
cout << nearbyint(189.22) << "\n";
cout << remainder(18,56) << "\n";
return 0;
}
```
```

Output:

```
8451
952
919
9
70
21
189
18
```

## 5. Comparison functions

It makes no difference how long the number is if it can help you compare it in a short period of time. Several comparison function examples are provided below:

Function Description
isgreater(p,q) It determines if p is greater than q or not.
islessequal(p,q) It determines if p is smaller than or equal to q or not.
isgreaterequal(p,q ) It determines whether p is bigger than or equal to q.
islessgreater(p,q) It determines if p is equal to or larger than y.
isunordered(p,q) It determines if p has been compared or not.

### C++ Code in order to Implement the Functionality Above

``````
#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:

```
1
0
0
1
0
```

## 6. Using Trigonometric Function

Functions specifically used in geometric computations. The right-angled triangle provides a relationship between the angle and the ratio of the lengths of the two sides.

Function Description
sin(y) the value of sine y is calculated by this function.
cos(y) the value of cosine y is calculated by this function.
tan(y) the value of tangent y is calculated by this function.
asin(y) the value of inverse sine y is calculated by this function.
acos(y) the value of inverse cosine y is calculated by this function.
atan(y) the value of inverse tangent y is calculated by this function.
atan2(y,x) the value of the inverse tangent of y as well as the x coordinates is calculated by this function.

### C++ Code in order to Implement the Functionality Above

``````
#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:

```
0
1
1.55741
1.5708
1.5708
0.785398
0
```

## 7. Hyperbolic functions.

Here are a few more fascinating functions known as "hyperbolic functions" that can be used in order to calculate the values of hyperbolic trigonometric functions.

Function Description
sinh(x) the value of the hyperbolic sine of x is calculated by this function.
cosh(x) the value of the hyperbolic cosine of x is calculated by this function.
tanh(x) the value of the hyperbolic tangent of x is calculated by this function.
asinh(x) the value of the hyperbolic arc sine of x is calculated by this function.
acosh(x) the value of the hyperbolic arc cosine of x is calculated by this function.
atanh(x) the value of the hyperbolic arc sine of x will be calculated by this function.

### C++ Code in order to Implement the Functionality Above

``````
#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:

```
0
1
0.761594
0.881374
0
0
```

Conclusion

Math functions are essential for making significant time and memory savings. There is no need to create any math functions directly because they are all built-in. Instead, just add a header file to have access to the entire math class library.

VIEW ALL