Mathematical functions in C++

In some problems it is required to find values of some mathematical functions like logarithm value, sinh value, tanh value, exponential value calculating such values manually is not easy.

In this cpp tutorial, we are going to discuss some in-built cpp mathematical functions in C++.

sinh() function – Mathematical function in C++

It is an in-built function in C++ which calculates hyperbolic sine angle value given in radians.

Syntax of sinh() function in C++

sinh ( data_type value )

The function accepts a parameter ( value ) which is a hyperbolic angle in radians. The data type can be of double or long etc. It returns hyperbolic sine of the argument.

C++ code to implement sinh() function

#include <bits/stdc++.h> 

    using namespace std; 

  int main() 
{ 
  double x = 13.2; 

  double result = sinh(x); 
  
  cout << "sinh(13.2) = " << result << endl; 

   
  double y = 90; 

  cout << "sinh(90) = " << sinh(y) << endl; 

  return 0; 
}
 

OUTPUT

sinh(13.2) = 270182

sinh(90) = 6.10202e+38

Similarly we can use in-built tanh() and cosh() function in cpp .

exp() function – Mathematical function in C++

The exp() function in cpp returns exponential(e)(2.71828) raised to the given passed argument to exp().

Syntax :- exp (value );

This function returns e^value .

Note :- for large value exp() returns inf .

C++ code to implement in-built exp() function

#include <bits/stdc++.h> 

  using namespace std; 

  int main() 
{ 
   int x = 12;
     
      cout<<exp(x)<<endl;
      
     long y= 1234;
      
       cout<<exp(y)<<endl;
}

OUTPUT 

162755
inf

log() function – Mathematical function in C++

This function returns the natural logarithm ( base – e) to the value passes in argument to log() function in C++. To get logarithm base 10 value we use log10() function . This function accepts parameter of any type int , float , long , double .

Syntax :- log ( value ) ; // returns base e logarithm value of argument ( value ) .

log10 ( value ) ; // returns base 10 logarithm value of argument ( value ) .

This function returns the output based on the given argument

  • If argument is negative it returns, it returns nan ( not a number).
  • If argument is zero, it returns inf ( infinity).
  • If argument is 1, it returns zero.
  • If argument is between 0 and 1, it returns a negative value.
  • If argument is positive greater than 1, it returns a positive value.

C++ Code to implement in-built log () function

#include <bits/stdc++.h> 

   using namespace std; 

  int main() 
{ 
   int a = -12;
     
      cout<<log(a)<<endl;
      
   int b = 0 ; 
   
      cout << log(b) <<endl;
      
    float c = 0.5;
      
       cout<< log(c)<<endl;
       
     int d = 1;
      
        cout<< log (d)<<endl;
        
    int e = 2;
      
        cout<<log(e)<<endl;
      
}

OUTPUT

nan
-inf
-0.693147
0
0.693147

fma() function – Mathematical function in C++

fma() function in C++ accepts three arguments a,b,c and return value a*b+c  without losing precision .

Syntax :- fma ( a ,b , c ) .

It returns the value a*b+c.

C++ code to implement fma() function

#include <bits/stdc++.h> 

  using namespace std; 

  int main() 
{ 
  long a=1;
  
   long b=2;
  
   long c=3;
  
    cout<<fma(a,b,c)<<endl;
  
  return 0;
}

OUTPUT

5

Also, read,

Leave a Reply

Your email address will not be published. Required fields are marked *