C++ Mathematical Functions

C++Server Side ProgrammingProgramming

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 −

sine

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)

Calling syntax

double x = sin(23.4);

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double x = 45.3;
cout << "sin ( "<<x<<" ) = " << sin(x) << endl;
}

Output

sin( 45.3 ) = 0.968142

Cosine

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)

Calling syntax

double x = cos(23.4);

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double x = 45.3;
cout << "cos ( "<<x<<" ) = " << cos(x) << endl;
}

Output

cos( 45.3 ) = 0.2504

tangent

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)

Calling syntax

double x = tan(23.4);

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double x = 45.3;
cout << "tan ( "<<x<<" ) = " << tan(x) << endl;
}

Output

tan( 45.3 ) = 3.86638

asin

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)

Calling syntax

double x = asin(0.3232);

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double x = 0.3232;
cout << "asin ( "<<x<<" ) = " << asin(x) << endl;
}

Output

asin( 0.3232 ) = 0.3291

acos

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)

Calling syntax

double x = acos(0.3232);

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double x = 0.3232;
cout << "acos ( "<<x<<" ) = " << acos(x) << endl;
}

Output

acos( 0.3232 ) = 1.24169

atan

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)

Calling syntax

double x = atan(0.3232);

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double x = 0.3232;
cout << "atan ( "<<x<<" ) = " << atan(x) << endl;
}

Output

atan( 0.3232 ) = 0.312603

cosh

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)

Calling syntax

double x = cosh(0.342);

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double x = 0.342;
cout << "cosh ( "<<x<<" ) = " << cosh(x) << endl;
}

Output

cosh( 0.342 ) = 1.05905

sinh

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)

Calling syntax

double x = sinh(0.342);

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double x = 0.342;
cout << "sinh ( "<<x<<" ) = " << sinh(x) << endl;
}

Output

sinh( 0.342 ) = 0.348706

tanh

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)

Calling syntax

double x = tanh(0.342);

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double x = 0.342;
cout << "tanh ( "<<x<<" ) = " << tanh(x) << endl;
}

Output

tanh( 0.342 ) = 0.329262

Power

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)

Calling syntax

double x = pow(2, 4)

Example

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

Output

pow( 2 , 4 ) = 16

Sqrt ( square root)

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)

Calling syntax

double x = sqrt(25.00)

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double a =25 ;
cout << "sqrt( "<<a<<" ) = " << sqrt(a) << endl;
}

Output

sqrt( 25 ) = 5.00

Log

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)

Calling Syntax

double x = log(1.35)

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double a =1.35 ;
cout << "sqrt( "<<a<<" ) = " << sqrt(a) << endl;
}

Output

sqrt( 1.35 ) = 0.300105

Floor

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)

Calling Syntax

double x = floor(5.24)

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double a =6.24 ;
cout << "floor( "<<a<<" ) = " << floor(a) << endl;
}

Output

floor( 6.24 ) = 6

ceil

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)

Calling Syntax

double x = ceil(5.24)

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
double a =6.64 ;
cout << "ceil( "<<a<<" ) = " << ceil(a) << endl;
}

Output

ceil( 6.64 ) = 7

abs

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)

Calling Syntax

double x = abs(-512)

Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int main(){
int a = -345 ;
cout << "abs( "<<a<<" ) = " << abs(a) << endl;
}

Output

abs( -345 ) = 345
Published on 19-Sep-2019 07:05:50