C++ Mathematical Functions: absolutevalue, sqrt, max, pow etc.

This Tutorial Explains Important C++ Mathematical Functions Included in <cmath> header file such as abs, max, pow, sqrt, etc. with Examples & C++ Constants like M_PI:

C++ provides a large number of mathematical functions that can be used directly in the program. Being a subset of C language, C++ derives most of these mathematical functions from math.h header of C.

In C++, the mathematical functions are included in the header <cmath>.

=> Check Complete C++ FREE Training Series Here.

C++ Mathematical functions

Mathematical Functions In C++

Table Of C++ Mathematical Functions

Given below is a list of the important mathematical functions in C++ along with their description, prototype, and example.

NoFunctionPrototypeDescriptionExample
Trigonometric Functions
1cosdouble cos (double x);Returns cosine of angle x in radians.cout<< cos ( 60.0 * PI / 180.0 );
(here PI = 3.142)
**returns 0.540302
2sindouble sin(double x);Returns sine of angle x in radians.cout<< sin ( 60.0 * PI / 180.0 );
(here PI = 3.142)
**returns 0.841471
3tandouble tan (double x);Returns tangent of angle x in radians.cout<< tan ( 45.0 * PI / 180.0 );
(here PI = 3.142)
**returns 0.931596
4acosdouble acos (double x);Returns arc cosine of angle x in radians.
**Arc cosine is the inverse cosine of cos operation.
double param = 0.5;
cout<< acos (param) *
180.0 / PI;
(here PI = 3.142)
**returns 62.8319
5asindouble asin(double x);Returns arc sine of angle x in radians.
**Arc sine is the inverse sine of sin operation.
double param = 0.5;
cout<< asin (param) *
180.0 / PI;
(here PI = 3.142)
**return 31.4159
6atandouble atan (double x);Returns arc tangent of angle x in radians. **Arc tangent is the inverse tangent of tan operation.double param = 1.0;
cout<< atan (param) *
180.0 / PI;
(here PI = 3.142)
**returns 47.1239
Power Functions
7powdouble pow (double base, double exponent);Returns the base raised to power exponent.cout<<”2^3 = “<< pow(2,3);
**returns 8
8sqrtdouble sqrt(double x);Returns square root of x.cout<< sqrt(49);
** returns 7
Rounding and Remainder Functions
9ceildouble ceil (double x);Returns smallest integer value that is not less than x;
Rounds x upward.
cout<< ceil(3.8);
**returns 4
10floordouble floor (double x);Returns larger integer value that is not greater than x;
Rounds x downward.
cout<< floor(2.3);
**returns 2
11fmoddouble fmod (double numer, double denom);Returns floating-point remainder of numer/denom.cout<< fmod(5.3,2);
**returns 1.3
12truncdouble trunc (double x);
**also provides variations for float and long double
Returns the nearest integral value not larger than x.
Rounds  x  toward zero.
cout<< trunc(2.3);
**returns 2
13rounddouble round (double x);
**also provides variations for float and long double
Returns integral value that is nearest to x.cout<< round(4.6);
**returns 5
14remainderdouble remainder (double numer, double denom);
**also provides variations for float and long double
Returns floating point remainder of numer/denom rounded to nearest value.cout<< remainder(18.5 ,4.2);
**returns 1.7
Minimum, Maximum, Difference and Absolute Functions
15fmaxdouble fmax (double x, double y).
**also provides variations for float and long double.
Returns larger value of the arguments x and y.
If one number is NaN, other is returned.
cout<< fmax(100.0,1.0);
**returns 100
16fmindouble fmin (double x, double y);
**also provides variations for float and long double.
Returns smaller value of the arguments x and y.
If one number is NaN, other is returned.
cout<< fmin(100.0,1.0);
**returns 1
17fdimdouble fdim (double x, double y);
**also provides variations for float and long double.
Returns the positive difference between x and y.
If x > y, returns x-y; otherwise returns zero.
cout<< fdim(2.0,1.0);
**returns 1
18fabsdouble fabs(double x);Returns absolute value of x.cout<< fabs(3.1416);
**returns 3.1416
19absdouble abs ( double x);
**also provides variations for float and long double.
Returns absolute value of x.cout<< abs(3.1416);
**returns 3.1416
Exponential and Logarithmic Functions
20expdouble exp (double x);Returns exponential value of x i.e. e x.cout<< exp(5.0);
**returns 148.413
21logdouble log (double x);Returns natural logarithm of x.(to the base e).cout<< log(5);
**returns 1.60944
22log10double log10 (double x);Returns common logarithm of x (to the base 10).cout<< log10(5);
**returns 0.69897

C++ program that demonstrates all the functions discussed above.

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
  int PI = 3.142;
  cout<< "cos(60) = " << cos ( 60.0 * PI / 180.0 )<<endl;
  cout<< "sin(60) = " << sin ( 60.0 * PI / 180.0 )<<endl;
  cout<< "tan(45) = " << tan ( 45.0 * PI / 180.0 )<<endl;
  cout<< "acos(0.5) = " << acos (0.5) * 180.0 / PI<<endl;
  cout<< "asin(0.5) = " << asin (0.5) * 180.0 / PI<<endl;
  cout<< "atan(1.0) = " << atan (1.0) * 180.0 / PI<<endl;
  cout<< "2^3 = " << pow(2,3)<<endl;
  cout<< "sqrt(49) = " << sqrt(49)<<endl;
  cout<< "ceil(3.8) = " << ceil(3.8)<<endl;
  cout<< "floor(2.3) = " << floor(2.3)<<endl;
  cout<< "fmod(5.3,2) = " << fmod(5.3,2)<<endl;
  cout<< "trunc(5.3,2) = " << trunc(2.3)<<endl;
  cout<< "round(4.6) = " << round(4.6)<<endl;
  cout<< "remainder(18.5,4.2) = " << remainder(18.5 ,4.2)<<endl;
  cout<< "fmax(100.0,1.0) = " << fmax(100.0,1.0)<<endl;
  cout<< "fmin(100.0,1.0) = " << fmin(100.0,1.0)<<endl;
  cout<< "fdim(2.0,1.0) = " << fdim(2.0,1.0)<<endl;
  cout<< "fabs(3.1416) = " << fabs(3.1416)<<endl;
  cout<< "abs(3.1416) = " << abs(3.1416)<<endl;
  cout<< "log(5) = " << log(5)<<endl;
  cout<< "exp(5.0) = " << exp(5.0)<<endl;
  cout<< "log10(5) = " << log10(5)<<endl;
  return 0;
}

Output:

cos(60) = 0.540302
sin(60) = 0.841471
tan(45) = 0.931596
acos(0.5) = 62.8319
asin(0.5) = 31.4159
atan(1.0) = 47.1239
2^3 = 8
sqrt(49) = 7
ceil(3.8) = 4
floor(2.3) = 2
fmod(5.3,2) = 1.3
trunc(5.3,2) = 2
round(4.6) = 5
remainder(18.5,4.2) = 1.7
fmax(100.0,1.0) = 100
fmin(100.0,1.0) = 1
fdim(2.0,1.0) = 1
fabs(3.1416) = 3.1416
abs(3.1416) = 3.1416
log(5) = 1.60944
exp(5.0) = 148.413
log10(5) = 0.69897

program that demonstrates all the functions- output

In the above program, we have executed the mathematical functions that we tabularized above along with their respective results.

Next, we will discuss some of the important mathematical functions used in C++.

Abs => Computes the absolute value of a given number.
Sqrt => Used to find the square root of the given number.
Pow => Returns the result by raisin base to the given exponent.
Fmax => Finds the maximum of two given numbers.

We will discuss each function in detail along with C++ examples. We will also get to know more about the mathematical constant M_PI that is often used in quantitative programs.

C++ abs

Function prototype: return_type abs (data_type x);

Function Parameters: x=> value whose absolute value is to be returned.

x can be of the following types:

double
float
long double

Return value: Returns the absolute value of x.

As parameters, the return value can also be of the following types:

double
float
long double

Description: Function abs is used to return the absolute value of the parameter passed to the function.

Example:

#include <iostream>    
#include <cmath>  
using namespace std;
int main ()
{
  cout << "abs (10.57) = " << abs (10.57) << '\n';
  cout << "abs (-25.63)  = " << abs (-25.63) << '\n';
  return 0;
}

Output:

output 2

Here, we have used examples with a positive and negative number with the abs function for clarity purposes.

C++ sqrt

Function prototype: double sqrt (double x);

Function Parameters: x=>value whose square root is to be computed.

If x is negative, domain_error occurs.

Return value: A double value indicating the square root of x.

If x is negative, domain_error occurs.

Description: The sqrt function takes in the number as a parameter and computes their squares root. If the argument is negative, a domain error occurs. When domain error occurs, then the global variable errno is set EDOM.

Example:

#include <iostream>      
#include <cmath>       
using namespace std;
int main ()
{
  double param, result;
  param = 1024.0;
  result = sqrt (param);
  cout<<"Square root of "<<param<<"(sqrt("<<param<<")):"<<result<<endl;
  param = 25;
  result = sqrt (param);
  cout<<"Square root of "<<param<<"(sqrt("<<param<<")):"<<result<<endl;
  
  return 0;
}

Output:

output 3

In the above program, we have computed the square root of 1024 and 25 using the sqrt function.

C++ pow

Function prototype: double pow (double base, double exponent).

Function Parameters: base=> base value.

Exponent=> exponent value

Return value: The value obtained after raising the base to the exponent.

Description: The function pow takes in two arguments i.e. base and exponent and then raises the base to the power of the exponent.

If the base if finite negative and exponent is negative but not an integer value then the domain error occurs. Certain implementations may cause domain error when both base and exponent are zero and if the base is zero and exponent is negative.

If the function result is too small or too large for the return type, then it may result in a range error.

Example:

#include <iostream>      
#include <cmath>       
using namespace std;
int main ()
{
  cout<< "2 ^ 4 = "<<pow (2.0, 4.0)<<endl;
  cout<< "4 ^ 12 = "<< pow (4, 12.0)<<endl;
  cout<< "7 ^ 3 = "<< pow (7, 3);
  return 0;
}

Output:

output 4

The above program demonstrates the usage of the POW function in C++. We can see that it computes the value by raising a number to the specified power.

C++ max

Function prototype: double fmax (double x, double y);

Function Parameters: x, y=> two values to be compared to find the maximum.

Return value: Returns the maximum value of the two parameters.

If one of the parameters is Nan, the other value is returned.

Description: The function fmax takes in two numeric arguments and returns the maximum of the two values. Apart from the prototype mentioned above, this function also has overloads for other data types like float, long double, etc.

Example:

#include <iostream>  
#include <cmath>
using namespace std;
int main ()
{ 
  cout <<"fmax (100.0, 1.0) = " << fmax(100.0,1.0)<<endl;
  cout << "fmax (675, -675) = " << fmax(675.0, -675.0)<<endl;
  cout << "fmax (-100.0, -1.0) = " << fmax(-100.0,-1.0)<<endl;
  
  return 0;
} 

Output:

output 5

The above code shows the usage of the fmax function to find the maximum of two numbers. We see the cases where one of the numbers is negative, and both the numbers are negative.

Mathematical Constants In C++

The <cmath> header of C++ also includes several mathematical constants that can be used in mathematical and quantitative code.

To include mathematical constants in the program, we have to use a #define directive and specify a macro “_USE_MATH_DEFINES”. This macro is to be added to the program before we include the <cmath> library.

This is done as shown below:

#define _USE_MATH_DEFINES
 #include <iostream>
 #include <cmath>
 ….C++ Code…..

One of the constants that we use frequently while writing mathematical and quantitative applications is PI. The following program shows the usage of predefined constant PI in the C++ program.

#define _USE_MATH_DEFINES
 
#include <cmath>
#include <iostream>
using namespace std;
 
int main() {
  double area_circle, a_circle;
  int radius=5;
  double PI = 3.142;
  //using predefined PI constant
  area_circle = M_PI * radius * radius; 
  cout<<"Value of M_PI:"<<M_PI<<endl;
  cout << "Area of circle with M_PI : "<<area_circle << endl;
  //using variable PI
  a_circle = PI * radius * radius;
  cout<<"Value of variable PI:"<<PI<<endl;
  cout << "Area of circle with PI : "<<a_circle << endl;
  
  return 0;
}

Output:

output 6

The above program demonstrates the mathematical constant M_PI available in <cmath>. We have also provided a local variable PI initialized to the value 3.142. The output shows the area of circle computed using M_PI and local PI variable using the same radius value.

Though there is not much difference between the two area values calculated, it is often desirable to use PI as a locally defined variable or constant.

Conclusion

C++ uses various mathematical functions like abs, fmax, sqrt, POW, etc. as well as trigonometric and logarithmic functions that can be used to develop quantitative programs. We have seen some of the important functions in this tutorial along with their examples.

We have also seen the mathematical constant M_PI which defines the value of geometric constant PI that can be used to calculate various formulae.

C++ uses mathematical functions by including <cmath> header in the program. These functions are predefined and we need not define them in our program. We can directly use these functions in code which inturn makes coding more efficient.

=> Read Through The Extensive C++ Training Tutorial Series Here.