| by Arround The Web | No comments

C++ Cmath Library

To make Mathematical problems stress-free for the programmer, C++ offers a library that contains all the functionalities we use in our routine to solve mathematical problems. The ‘cmath’ library contains logarithmic, exponential, hyperbolic, power, trigonometric, and many more. To access all the mathematical built-in methods, we must include one statement in the code:

#include<cmath>

Example no. 1:

In this case, let us observe how we can use the ‘cmath’ library to implement all the trigonometric functions.

#include <iostream>

#include <cmath>

using namespace std;

int main() {

double value_0=90;

cout<<"Cos of 90 is :"<<cos(value_0);

cout<<"\nSin of 90 is :"<<sin(value_0);

cout<<"\nTan of 90 is :"<<tan(value_0);

cout<<"\narcCos of 90 is :"<<acos(value_0);

cout<<"\narcSin of 90 is :"<<asin(value_0);

cout<<"\narcTan of 90 is :"<<atan(value_0);

return 0;

}

We first import two libraries, the ‘iostream’ library for calling the input and output methods and the ‘cmath’ library for calling the mathematical functions in our program. In the main()function, declare and define a double-type variable or integer-type variable because the initialized value is an integer. But if we want to store the output after performing the operations ‘double’ data type would be recommended because the answer of such functions is mostly a float value. Then, in this code, we implemented the inbuilt trigonometric functions. Cout a message and then find the cos of the value assigned to the variable ‘value_0’ by using the cos() trigonometric method. Copy this statement and paste it five times in the code. We have to change the message a little bit and the function name from cos() to sin() and tan(), arcsin(), arccos(), arctan(). We called all the trigonometric methods one by one. All these functions require only one parameter. Then, the compiler will execute the code line by line and will print the message and the value on the console. The compiler will run the code and it goes into the ‘cmath’ library and finds the trigonometric method that is called in the program and fetches the code for that method. Then, it puts our defined value in that method to get the required answer. \n is used to make code look presentable on the console.

Example no. 2:

Employ the ‘cmath’ library power methods that can be accessed by just importing the library.

#include <iostream>

#include <cmath>

using namespace std;

int main() {

int value_1=560;

cout<<"\nsqrt of 560 is :"<<sqrt(value_1);

cout<<"\ncbrt of 560 is :"<<cbrt(value_1);

cout<<"\npow of 560 is :"<<pow(value_1,6);

return 0;

}

The program is started by importing the two libraries ‘iostream’ and ‘cmath’. The ‘cmath’ header file is utilized to access Mathematical functions. Then, in the main() function, define a variable ‘value_1’ of type ‘int’ and initialize it with an integer number. After that, we need to print a message on the terminal so we call the ‘cout’ statement. Here we will employ the power functions. First, use ‘cout’ to display a message, then we will find the square of an integer value. For that purpose, sqrt() will be called and it is a method of the ‘cmath’ library. It takes only one parameter and it accepts the value whose square root we are trying to determine. Once again, employing the ‘cout’ statement to show the text and find the cube root of the same value by using cbrt() function. It also takes one parameter. Now, we will utilize the pow() method to find the power. This method contains two parameters: the first one will be the base value and the second is power. The compiler implements the program and prints a message on the console and evaluates the power methods.

Example no. 3:

We will discuss the exponential and logarithmic inbuilt methods of the ‘cmath’ library with the implementation.

#include <iostream>

#include <cmath>

using namespace std;

int main() {

int Number_0=60;

cout<<"\nlog of 60 is :"<<log(Number_0);

cout<<"\nlog2 of 60 is :"<<log2(Number_0);

cout<<"\nlog10 of 60 is :"<<log10(Number_0);

cout<<"\n\nexp of 60 is :"<<exp(Number_0);

cout<<"\nexp2 of 60 is :"<<exp2(Number_0);

return 0;

}

Now, we will discuss the logarithm and exponential methods of the ‘cmath’ library. For this, we have to integrate the libraries ‘iostream’ and ‘cmath’. After this, call the main() method to declare and initialize the integer type variable ‘Number_0’. Then, ‘cout’ a message and call the log(), log2(), and log10() to find the log of required value to the base 2 and 10. The log() function and log10() function work in the same way but there is a very minor difference between their outputs. Then to find the exponent of the value ‘60’ use exp() and exp2() functions. This code is executed by entering the ‘return 0’ command at the end.

Example no. 4:

In this example code, we will utilize the hyperbolic functions that are applied in differential equations.

#include <iostream>

#include <cmath>

using namespace std;

int main() {

double Val_0=30;

cout<<"Cosh of 30 is :"<<cosh(Val_0);

cout<<"\nSinh of 30 is :"<<sinh(Val_0);

cout<<"\nTanh of 30 is :"<<tanh(Val_0);

cout<<"\narcCosh of 30 is :"<<acosh(Val_0);

cout<<"\narcSinh of 30 is :"<<asinh(Val_0);

cout<<"\narcTanh of 30 is :"<<atanh(Val_0);

return 0;

}

After importing the libraries ‘iostream’ and ‘cmath’, we would invoke the main() method. In the next statement, a variable having a ‘double’ data type is initialized. Now, call the cosh(), sinh(), tanh(), acosh(), asinh(), and atanh() to find the hyperbolic functions of the specified value. Utilize the ‘cout’ command six times to represent the text on the terminal and then employ all the above-mentioned hyperbolic methods one by one. The hyperbolic solutions of the value ‘Val_0’ will be obtained after running the program. The compiler solves these methods because their code is already defined in C++ ‘cmath’ library. So, the compiler fetches the code and executes all these hyperbolic functions.

Conclusion

In this guide, we have explored the ‘cmath’ library that C++ provides to solve basic Mathematical problems and help programmers to stick to the actual problem they want to solve. We started with the very basics of the C++ ‘cmath’ library and then implemented its functions using various coding examples. The methods discussed in the above codes include logarithmic, exponential, power, trigonometric and hyperbolic functions of the ‘cmath’ library. Not all of the methods of the ‘cmath’ library are defined here, but most of them are. This library contains numerous functions which are used to deal with arithmetic problems.

Share Button

Source: linuxhint.com

Leave a Reply