Written by **Sagar Rabidas**February 10, 2022

7 min read

C++

Software Developer

C++ provides <math.h> library for math functions to perform complex mathematical functions like trigonometric function, algebraic equations easily. For example, the 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.

C++ provides a huge number of different types of math functions mentioned below with examples:

**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.

#include <iostream>

#include <math.h>

using namespace std;

int main() {

cout << max(16,18) << "\n";

cout << min(16,18) << "\n";

return 0;

}

**pow (m,n):**It will calculate m raised to the power n.**sqrt(m):**It will calculate the square root of m.**cart(n):**It will calculate the cube root of n.**hypot(m,n):**It will calculate the hypotenuse of the right-angled triangle.

#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;

}

**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.**log(p):**It will calculate the logarithm of p.

#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;

}

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.**the remainder(z,y):**It will calculate the remainder of z/y.

#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;

}

Functions are 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.

#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;

}

C

C++

Math Functions

Was this blog helpful?

Trending Technologies

15

Software91

DevOps48

Frontend Development24

Backend Development20

Server Administration17

Linux Administration28

Data Center24

Sentry24

Terraform23

Ansible83

Docker70

Penetration Testing16

Kubernetes21

NGINX20

JenkinsX17

Recommended Blogs

8

Recommended Threads

8

Anik Adhikary

Anik Adhikary

Anik Adhikary