Different types of Math Functions in C++

Different types of Math Functions in C++
Techiio-author
Written by Sagar RabidasFebruary 10, 2022
7 min read
C++
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Let's start the discussion on Different types of Math Functions in C++

Math Functions in C++

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.

Different Types of Math Functions

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

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.

C++ Code to Implement Above Functionality

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

Power functions

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

C++ code to implement above functionality

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

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

C++ code to implement above functionality

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

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

C++ code to implement above functionality

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

Using Trigonometric Function

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.

C++ code to implement above functionality

#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
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance