
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 −
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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#include <iostream> #include <math.h> using namespace std; int main(){ int a = -345 ; cout << "abs( "<<a<<" ) = " << abs(a) << endl; }
Output
abs( -345 ) = 345
- Related Articles
- Python Mathematical Functions
- Mathematical Functions in Python?
- Mathematical Functions in Java
- Mathematical Functions using Python
- Mathematical Functions in C#
- Mathematical Functions in SQL
- Mathematical statistics functions in Python
- Mathematical Functions in Python - Special Functions and Constants
- Program to evaluate one mathematical expression without built-in functions in python
- HTML5 Mathematical operators
- Mathematical Logical Connectives
- Mathematical Foundation Introduction
- Mathematical Constants in Python
- PHP Predefined Mathematical Constants
- Mathematical Logical Terms and Definitions
