C++ Mathematical Functions

Mathematical calculations can be done in C++ programming language using the mathematical functions which are included in math or cmath library. These mathematical functions are defined to do complex mathematical calculations. Let’s learn each of them one by one −
The sin method is used to calculate the sin of the angle given as an argument in degrees. This function accepts one double integer as an argument and returns a double integer which is the value of sin(x°).
double sin(double)
double x = sin(23.4);
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 45.3;
   cout << "sin ( "<<x<<" ) = " << sin(x) << endl;
}
sin( 45.3 ) = 0.968142
The cosin or cos method is used to calculate the cos of the angle given as an argument in degrees. This function accepts one double integer as an argument and returns a double integer which is the value of cos(x°).
double cos(double)
double x = cos(23.4);
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 45.3;
   cout << "cos ( "<<x<<" ) = " << cos(x) << endl;
}
cos( 45.3 ) = 0.2504
The tangent or tan method is used to calculate the tan of the angle given as an argument in degrees. This function accepts one double integer as an argument and returns a double integer which is the value of tan(xo) = sin(x°)∕cos(x°).
double tan(double)
double x = tan(23.4);
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 45.3;
   cout << "tan ( "<<x<<" ) = " << tan(x) << endl;
}
tan( 45.3 ) = 3.86638
The asin function is used to find the arc sine of the given argument. It returns the asin value for the given input set which can be any double integer within the range -1 to 1 otherwise it would give an error. The function accepts double integer between -1 and 1 and returns a double value as a result of asin().
double asin(double)
double x = asin(0.3232);
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.3232;
   cout << "asin ( "<<x<<" ) = " << asin(x) << endl;
}
asin( 0.3232 ) = 0.3291
The acos function is used to find the arc cosine of the given argument. It returns the acos value for the given input set which can be any double integer within the range -1 to 1 otherwise it would give an error. The function accepts double integer between -1 and 1 and returns a double value as a result of acos().
double acos(double)
double x = acos(0.3232);
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.3232;
   cout << "acos ( "<<x<<" ) = " << acos(x) << endl;
}
acos( 0.3232 ) = 1.24169
The atan function is used to calculate the value of arc tangent of the given argument. It returns a double value of atan for the double input value in the parameter.
double atan(double)
double x = atan(0.3232);
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.3232;
   cout << "atan ( "<<x<<" ) = " << atan(x) << endl;
}
atan( 0.3232 ) = 0.312603
The cosh function is used to calculate the value of hyperbolic cosine of the given argument. It returns a double value of cosh for the double input value in the parameter.
double cosh(double)
double x = cosh(0.342);
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.342;
   cout << "cosh ( "<<x<<" ) = " << cosh(x) << endl;
}
cosh( 0.342 ) = 1.05905
The sinh function is used to calculate the value of hyperbolic sin of the given argument. It returns a double value of sinh for the double input value in the parameter.
double sinh(double)
double x = sinh(0.342);
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.342;
   cout << "sinh ( "<<x<<" ) = " << sinh(x) << endl;
}
sinh( 0.342 ) = 0.348706
The tanh function is used to calculate the value of hyperbolic tangent of the given argument. It returns a double value of sinh for the double input value in the parameter.
double tanh(double)
double x = tanh(0.342);
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.342;
   cout << "tanh ( "<<x<<" ) = " << tanh(x) << endl;
}
tanh( 0.342 ) = 0.329262
The pow function is used to calculate the power of the base raised to the power of exponent. It accepts two double values as arguments which are the base and exponents numbers and it returns a single double integer which is base to the power of exponent.
double pow( double, double)
double x = pow(2, 4)
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double base = 2 , power = 4;
   cout << "pow( "<<base<<" , "<<power<<" ) = " << pow(base, power) << endl;
}
pow( 2 , 4 ) = 16
sqrt function in C++ returns the square root of the double integer inside the parameter list. The method accept a double integer value as input find square root and returns a double integer as output.
double sqrt( double)
double x = sqrt(25.00)
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double a =25 ;
   cout << "sqrt( "<<a<<" ) = " << sqrt(a) << endl;
}
sqrt( 25 ) = 5.00
The lock function is used to find the natural log of the given number. this method accepts single double integer value finds logarithmic value and returns a double integer result of log().
double log( double)
double x = log(1.35)
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double a =1.35 ;
   cout << "sqrt( "<<a<<" ) = " << sqrt(a) << endl;
}
sqrt( 1.35 ) = 0.300105
The floor function is used to return the floor value of the given double integer. floor value means rounded down value. the function accepts a double value as input and Returns double integer value calculated using floor().
double floor( double)
double x = floor(5.24)
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double a =6.24 ;
   cout << "floor( "<<a<<" ) = " << floor(a) << endl;
}
floor( 6.24 ) = 6
The ceil function is used to return the ceil value of the given double integer. ceil value means rounded up value. the function accepts a double value as input and Returns double integer value calculated using ceil().
double ceil( double)
double x = ceil(5.24)
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double a =6.64 ;
   cout << "ceil( "<<a<<" ) = " << ceil(a) << endl;
}
ceil( 6.64 ) = 7
The abs function returns the absolute value of the integer value. The function accepts an integer value and returns an integer value that has the same magnitude but positive sign.
double abs( double)
double x = abs(-512)
 Live Demo
#include <iostream>
#include <math.h>
using namespace std;
int main(){
   int a = -345 ;
   cout << "abs( "<<a<<" ) = " << abs(a) << endl;
}
abs( -345 ) = 345