C++ Math Header File

Today, we will learn about C++ Math – Header File and its all function and methods. So, the c++ has many functions that allow you to perform the mathematical task on numbers.

Max and min Function in C++

  • The max(x, y) function is using to find the highest value between x and y, So, the example below, You can see the explanation.

So, the Example:

#include <iostream>
using namespace std;

int main() {
  cout << man(10, 20);
  return 0;
}
Output:
20
  • the min(x, y) function is using to find the lowest values between x and y.

Example:

#include <iostream>
using namespace std;

int main() {
  cout << min(10, 20);
  return 0;
}

Then, see the Output:

10

Usa C++ <cmath> Header File

Another function, such as sqrt (square root), and round function round (rounds a number) and get log (natural logarithm), can be found in the <cmath> header file.

So, Example:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  cout << sqrt(25) << endl;
  cout << round(10.7) << endl;
  cout << log(3) << endl;
  return 0;
}

Output:

5
11
1.09861

Now! I am explaining the abs() function of <cmath>. So, this function takes one argument and returns the value of x and the example below.

So, Example:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
	int x = 10;
	cout << abs(x);
	return 0;
}

the function acos() is a part of C++ Math – Header File. So, it performs its own role in c++, that it takes one argument in radians and returns the inverse cosine value of x.

So, See the Example:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
	double x =  -0.5;
	cout << acos(x);
	return 0;
}

Output:
2.0944

the function asin() is a part of C++ Math – Header File. So, it performs its own role in c++, that it takes one argument in radians and returns the inverse sine value of y.

So, look at Example with ‘y’ variable:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
	double y =  -0.5;
	cout << asin(y);
	return 0;
}

Output:
-0.523599

the function atan() is a part of C++ Math – Header File. So, it performs its own role in c++, that it takes one argument in radians and returns the inverse tangent value of x.

Example:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
	double x =  -0.5;
	cout << atan(x);
	return 0;
}

Output:
-0.463648

So, in the second image, I give you all functions which use in <cmath> header file. I want to tell you that on this Website, previously I uploaded all topics click to check these chapter topics, Intro, C++ Comments, Variables, data types, char, Output, input, operators, and many more…

Share this:

By Usama Arshad

It's me Usama Arshad working as a Lecturer C++. I am a student of BSIT.

Leave a Reply

Your email address will not be published. Required fields are marked *