C++

# C++ Cmath Library

C++ programming language supports many mathematical functions. Instead of focusing on the implementation of large source codes, these built-in functions are used directly to avoid space and time consumption, hence simplifying the code and program. C++ has a large set of categories of these default functions. These functions are present in the header file of mathematics that must be included while using any C++ math operation.

#include <cmath>

We will explain some common functions of the cmath library through examples. Use Ubuntu 20.04 for the implementation.

## Sin()

In this example, we will calculate the size of the angle of the argument in degrees. For this purpose, it is essential to add the math library in the program and the input-output stream library must also be included as a header file. In the main program, a double value is initialized that will be used in the parameter as an argument of the sin, built-in function.

A simple syntax for the sin method is declared as follows:

Double  sin (double)

A double value is taken, and the return data type is also a double value. Save the code and execute it in the Linux terminal through a C++ compiler that is needed to compile the code in the Ubuntu system, so we have used G++ here along with –o that will store the resultant output of the file in another output file.

\$ g++ -o file file.c

After compilation, execute it with the name of the file.

\$ ./file

You will see that the decimal value will be obtained through the sin function.

## Cos()

A similar method is applicable for the cos function. As the cos methodology is declared to calculate the cos of the angle given as a parameter in degrees. The function will take a double value as a parameter and the return type will also be of double data type.

Double cos (double)

We have applied the built-in function in the cout statement.

We can also calculate the value of tangent by following the same approach.

## Power

This example relates to the exponents in C++ and their use. This power function is used in the program to calculate the power of the number or base that is raised to the power of the exponent. In this source code, both the values are of double data types; the base and exponents are written in the form of base power. In other words, the superscripts are the exponents in the mathematics language. This function returns a single integer value. The power function works in a way that the base number is multiplied by itself by the nth number of times the exponent has. For instance, the base is 3 and the power is 4, so 3 will be multiplied 4 times (i.e, 3*3*3*3).

Save the file and run it. You will see that the answer for 3 multiplied 4 times is 81.

## Absolute ()

The return value of the absolute function is the same in magnitude as the input number is, but the number has a positive sign. The input number is either a positive number or negative, but the returned number is always positive. The function is represented by ‘abs’.

For instance, we have taken a negative number here. The resultant value will be a positive value of the same number.

The result of the number absolute is given as follows:

## Examples

This example is a collection of all the exponential functions. One of which is explained earlier. This will contain log types and exponents as well.

• Exp(e): This is used to calculate the power exponential that is raised to the power “p”.
• Log(p): This is used to calculate the logarithm of the given value that is used in the parenthesis. The log values are manually calculated through the log table, but it consumes time, so it is calculated through a built-in function in the programming.
• Log10(p): It will calculate the logarithm again but the type is the common logarithm of the power “p”.
• Exp2(p): This exp function is used to calculate the base value 2 of exponential value “p”. This means that the power of any value will always have a base 2. The number 2 will multiply the number of times that is given as a power.
• Log2(p): Just like the exponent of 2, the base logarithm of 2 is calculated in a specified power value.
• Log b (p): It calculates the logarithm of a given value “p”.

The terminal will display the resultant value of each mathematic function.

We have already explained some commonly used mathematic functions through the examples above. Now, we will describe some other remaining features that are included in the mathematics library. At the end of this article, all of the below-mentioned features will be implemented collectively in the program and we will execute them to see the results.

• Acos (): It accepts the double value and returns the arc of cosin that is related to the angle x in the radians. The calculations are done in such a way that the input number is multiplied by the value that is obtained by dividing 18 by the value of pi.
• Asin and atan are both the reverse of sin and tan as we have described at the start. These calculations are done like the calculations of cos.
• Ceil (): Ceil is the type of calculating the remainder and rounding off the number. It takes a double value and then returns the smallest integer value that is not smaller than the given value. It rounds the value of x upwards.
• Floor(): It is the opposite of ceil function. It returns the larger value that is not greater than the given value. It rounds the value in a downward direction.
• Fmod (): It returns the value that is a floating-point remainder obtained from dividing two numbers given to it as numerator and denominator.
• Trunk (): It accepts the value as float or double and then returns the value that is the nearest integral value not larger than x and round the value of x towards zero digit number.
• Round (): It is different from the trunk () value as it returns only the integral value, not the decimal. The value is nearest to the x value given to it.
• Fmax (), fmin (): These functions are opposite to each other. They are the comparison functions; both take two double variables. Fmax() returns the larger value of both the parameters whereas the fmin() function returns the smaller value of arguments.

Now we will use the above functions in the code.

Execute them in the Ubuntu terminal to see the results of each function.

## Conclusion

Cmath library is an essential part of the C++ programming language as it provides many operations and functions of mathematics when we use the programs to execute source codes. All of these functions are built-in functions. These functions take inputs in different data types, but both variables must have the same data type while performing the same operation on them. Each elementary example describes the working of the function.