C++ tutorial-Mathematical calculation using cmath library


C++ cmath Library

C++ provides a cmath library that comes with useful mathematical methods. In C++ you don't need to create a math object by using new keyword. After including the cmath library in your C++ code (#include <cmath),  to call any method of the math object you simply write the method an supply its argument(s). In this C++ math topic, we will discuss some useful methods that are use commonly.

abs() method

The first method of math method that you should know is abs(number) method. It returns the absolute value of the number argument.

Example:

#include <cstdlib>
#include <iostream>
#include <conio.h>

#include <cmath>


using namespace std;
void mathtest(){
int a=-12;
cout<<abs(a);

}

//Start main
int main(){
mathtest();

getch();
return 0;


}
//End main

ceil() method

The ceil(number) method is used to get the nearest larger integer number of an input decimal number. For example, by using cell() method you will get 3, 5, 8 from 2.9, 4.1, 7.5 respectively.

Example:

#include <cstdlib>
#include <iostream>
#include <conio.h>

#include <cmath>

using namespace std;


void mathtest(){
int a=2.9;
cout<<ceil(a);

}

//Start main
int main(){
mathtest();

getch();
return 0;


}
//End main

floor() method

You can use the floor(number) method to get the nearest smaller integer number of an input number. For example, you will get 2,3,6 from 2.1, 3.8, 6.5 respectively.

Example:

#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <cmath>

using namespace std;

void mathtest(){
int a=2.1;
cout<<floor(a);

}

//Start main
int main(){
mathtest();

getch();
return 0;


}
//End main

pow() method

The pow(number, power) method is used to raise the power of a number.

Example:

#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <cmath>

using namespace std;

void mathtest(){

float num=10;
float power=2;
cout<<pow(num,power);

}

//Start main
int main(){
mathtest();

getch();
return 0;


}
//End main

srqt() method

The sqrt(number, root) method returns a decimal number that is a square root of a number. The number argument must be not a negative number.

Example:

#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <cmath>

using namespace std;


void mathtest(){

ffloat num=16;
cout<<sqrt(num);

}

//Start main
int main(){
mathtest();

getch();
return 0;


}
//End main

Note: The arguments of the mathematical methods are of  the type float or double.




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.