cmath Header file in C++

C++ is a widely used programming language. Moreover, It is considered as the father of all programming languages and that is why it is taught in 90% of the colleges and universities to all CS/IT/SE undergraduate students. So, Once you have grip over this language, all other languages are child’s play for you to learn. Furthermore, This Language have a lot of libraries that when explored by students makes their coding journey easy. One of them is cmath header File in C++. In this Article, We will learn about the usage of this library and all its functions.

The “cmath” header file is an integral component of the C++ Standard Library, offering a collection of functions designed for mathematical computations and operations. These functions serve as vital tools for a wide range of mathematical tasks, including arithmetic operations, exponentiation, logarithmic calculations, trigonometry, and square root calculations. Furthermore, Integrating “cmath” functions into your C++ programs proves indispensable for tasks involving numerical calculations, scientific simulations, and mathematical modeling.

How to use cmath header file?

This library comes by default with all the Compiler and IDEs used for C++ development. So, you can Include this library in your project by using following code:

#include<cmath.h>

List of Functions of “cmath” Header file

1. Trigonometric Functions

Negative Trigonometric COS-1 , SIN-1 , TAN-1 builtin function

  • Syntax
    • Double acos(double);
    • Double asin(double);
    • Double atan(double);
    • Return type and parameters are double but it returns answer in radians so multply it by (180.0/pi) to convert it in degrees
#include <iostream>
#include <math.h>
using namespace std;
#define PI 3.14159
int main () 
{
double x, ans;

cout<<"\n\tEnter Value to find Its cos , sin, tan inverse : ";
cin>>x;
cout<<"\n\tCos Inverse = "<< acos(x) * 180.0/PI<<endl;
cout<<"\n\tSin Inverse = "<< asin(x) * 180.0/PI<<endl;
cout<<"\n\tTan Inverse = "<< atan(x) * 180.0/PI<<endl;

return(0);
}
code output

Positive Trigonometric COS , SIN, TAN builtin function

  • Syntax
    • Double cos(double);
    • Double sin(double);
    • Double tan(double);
    • Return type and parameters are double.. but it takes angle in radians so multiply your answer by (pi/180) to convert it in radians
#include <iostream>
#include <math.h>
using namespace std;
#define PI 3.14159
int main () 
{
double x;
cout<<"\n\tEnter Angle to find cos , sin, tan  : ";
cin>>x;
cout<<"\n\tCos"<<x<<" = "<< cos(x * PI/180.0) <<endl;
cout<<"\n\tSin"<<x<<" = "<< sin(x * PI/180.0)<<endl;
cout<<"\n\tTan"<<x<<" = "<< tan(x * PI/180.0)<<endl;
return(0);
}
code output

Hyperbolic Trigonometric coshx , sinhx, tanhx builtin function

  • Syntax
    • Double cosh(double);
    • Double sinh(double);
    • Double tanh(double);
    • Moreover, Return type and parameters are double..
#include <iostream>
using namespace std;
#include <math.h>
int main () {
double x;
cout<<"\n\tEnter value of x to find coshx, sinhx, tanhx : ";
cin>>x;
//coshx hyperbolic cos of x
cout<<"\n\tThe hyperbolic cosine of "<<x<<" = "<<cosh(x);
//sinhx hyperbolic sin of x
cout<<"\n\n\tThe hyperbolic sine of "<<x<<" = "<<sinh(x);
//tanhx hyperbolic tan of x
cout<<"\n\n\tThe hyperbolic tangent of"<<x<<" = "<<tanh(x);
return 0;
}
cmath headerfile example

2. EXP Function

  • SYNTAX
    • Double exp(double x);
    • This function double exp(double x) returns the value of e raised to the xth power.
#include <iostream>
using namespace std;
#include <math.h>
int main () {
double x;
cout<<"\n\tEnter power x : ";
cin>>x;
cout<<"\n\tThe exponential value of "<< x<<" = "<<exp(x)<<endl;
return(0);
}
cmath headerfile example
cmath headerfile example

3. Logarithm function

  • SYNTAX
    • double log(double x)
    • this function double log(double x) returns the natural logarithm (base-e logarithm) of x.
    • double log10(double x)
    • This function double log10(double x) returns the common logarithm (base-10 logarithm) of x.
#include <iostream>
using namespace std;
#include <math.h>
int main () {
double x;
cout<<"\n\tEnter x : ";
cin>>x;
cout<<"\n\tln ( "<< x<<" ) = "<<log(x)<<endl;
cout<<"\n\tlog ( "<< x<<" ) = "<<log10(x)<<endl;
return(0);
}
cmath headerfile example

4. Modf functions in cmath header file

  • SYNTAX:
  • The C library function double modf(double x, double *integer) returns the fraction component (part after the decimal), and sets integer to the integer component.
  • double modf(double x, double *integer)
    • x − This is the floating point value.
    • integer − This is the pointer to an object where the integral part is to be stored.
#include <iostream>
using namespace std;
#include<math.h>
int main () {
double x, fpart, intpart;
cout<<"\n\tEnter x : ";
cin>>x;
fpart = modf(x, &intpart);
cout<<"\n\tIntegral part = "<< intpart<<endl;
cout<<"\n\tFraction Part = "<< fpart<<endl;
return(0);
}
cmath headerfile example

5. Power function

  • SYNTAX:
    • The C library function double pow(double x, double y) returns x raised to the power of y i.e. xy.


#include <iostream>
using namespace std;
#include<math.h>
int main () 
{
double x,y;
cout<<"\n\tEnter x : ";
cin>>x;
cout<<"\n\tEnter y : ";
cin>>y;
cout<<"\n\t"<<x<<" ^ "<<y<<" ="<<pow(x,y)<<endl;
return 0;
}
cmath headerfile example

6. SQRT function

  • SYNTAX:
    • The C library function double sqrt(double x) returns the square root of x.
#include <iostream>
using namespace std;
#include<math.h>

int main () {
double x;
cout<<"\n\tEnter x : ";
cin>>x;
cout<<"\n\t "<<char(251)<<x<<" = "<<sqrt(x)<<endl;

return(0);
}
cmath headerfile example
cmath headerfile example

7. CEIL and FLOOR functions

  • SYNTAX:
    • This function double ceil(double x) returns the smallest integer value greater than or equal to x.
    • This function double floor(double x) returns the largest integer value less than or equal to x.
#include <iostream>
using namespace std;
#include<math.h>
int main () 
{
double x;
cout<<"\n\tEnter x : ";
cin>>x;
cout<<"\n\t smaller integer value nearest to "<<x<<" = "<<floor(x)<<endl;
cout<<"\n\t  larger integer value nearest to "<<x<<" = "<<ceil(x)<<endl;
return(0);
}
cmath headerfile example
cmath headerfile example

8. fmod function

  • SYNTAX:
    • The C library function double fmod(double x, double y) returns the remainder of x divided by y.
#include <iostream>
using namespace std;
#include<math.h>
int main () 
{
double x,y;
cout<<"\n\tEnter x : ";
cin>>x;
cout<<"\n\tEnter y : ";
cin>>y;	
cout<<"\n\tModulus of "<<x<<" / "<<y<<" = "<<fmod(x,y)<<endl;  
return(0);
}
cmath headerfile example

9. ABS/FABS function

  • SYNTAX:
    • The C library function double fabs(double x,)  returns the absolute value of x
#include <iostream>
using namespace std;
#include<math.h>

int main () 
{
double x;
cout<<"\n\tEnter x : ";
cin>>x;
cout<<"\n\tAbsolute of "<<x<<" = "<<fabs(x)<<endl;  
return(0);
}
cmath headerfile example
cmath headerfile example

In summary, the <cmath> header file in C++ provides a range of mathematical functions, and you can use it by including the header and applying the appropriate functions to perform mathematical calculations in your C++ programs.

ctype header file in C++ Learn More.

1 thought on “cmath Header file in C++”

Leave a Comment