Math Functions in C++

C++ offers a lot of helpful mathematical functions that facilitate various mathematical calculations. Rather than implementing those calculations, we can directly use these built-in functions to make our programs simpler.

In order to use math functions in C++, we must include <math.h> or <cmath> header file in our program.

In this article, we will discuss various math functions included in the C++ standard library.

Math Function in C++

1. pow (base, exponent): We use pow() function to compute the value of base raised to exponent.

2. sqrt (parameter): It returns the square root of a number. The number entered as its argument cannot be negative.

3. abs (parameter): It takes an integer argument and returns its absolute value.

4. fabs (parameter): fabs() function, similar to abs(), returns absolute value of its argument. The difference is that we can also pass floating point values to it.

5. ceil (parameter): It returns the smallest integer which is greater than or equal to the argument passed.

6. floor (parameter): It returns the largest integer which is less than or equal to the argument passed.

7. log (parameter): It returns the base e logarithm value of the argument.

8. log10 (parameter): It returns the base 10 logarithm value of the argument.

9. exp (parameter): It returns the value of e (exponential) raised to the number entered as argument.

10. exp2 (parameter): It returns the base 2 exponential value i.e., 2 raised to the number entered as argument.

11. hypot (side1, side2): It takes two arguments as two sides of a right angled triangle and computes the value of hypotenuse.

12. fmod (numerator, denominator): It computes the floating point remainder when numerator / denominator.

13. modf (number, *ptr): modf() divides a number into integral and fractional parts. The function returns the fractional part, while the integer part is saved in the address pointed to by the pointer supplied as second argument to modf().

14. round (parameter): It returns the integral value that is closest to the argument, rounded away from zero in midway cases.

15. trunc (parameter): It returns the closest integral number that is not larger than the parameter, rounding it towards zero.

16. sin (parameter): It computes the sine of the angle in radians passed as argument.

17. cos (parameter): It computes the cosine of the angle in radians passed as argument.

18. tan (parameter): It computes the tangent of the angle in radians passed as argument.

19. asin (parameter): It computes the inverse sine of the argument. The range of argument is [-1,1] and the result is obtained in radians.

20. acos (parameter): It computes the inverse cosine of the argument. The range of argument is [-1,1] and the result is obtained in radians.

21. atan (parameter): It computes the inverse tangent of the argument. The argument can be any number and the result is obtained in radians.

22. atan2 (parameter1, parameter2): It computes inverse tangent of parameter1/parameter2

23. sinh (parameter): It computes the hyperbolic sine of the angle in radians passed as argument.

24. cosh (parameter): It computes the hyperbolic cosine of the angle in radians passed as argument.

25. tanh (parameter): It computes the hyperbolic tangent of the angle in radians passed as argument.

Example of math functions in C++

#include <iostream>
#include <math.h>
using namespace std;

int main() {
  double x = 4.6, y = 34;
    double result;
    result = pow(x, y);
    cout<<"4.6 raised to 34 = "<<result<<endl;
    
    result = sqrt(y);
    cout<<"square root of 34 = "<<result<<endl;
    
    cout<<"absolute value of 4.6 = "<<fabs(x)<<endl;
    
    cout<<"ceil value of 11.8 = "<<ceil(11.8)<<endl;
    cout<<"floor value of 11.8 = "<<floor(11.8)<<endl;
    
    cout<<"log of 1000 (base 10) = "<<log10(1000)<<endl;
    
    cout<<"exponential value of 4.6 = "<<exp(x)<<endl;
    
    double angle = 45;
    cout<<"sin 45 = "<<sin(angle)<<endl;
    
    int n = 1;
    cout<<"arc tan of 1 = "<<atan(n)<<endl;
    
    cout<<"cosh 45 = "<<cosh(angle);
  return 0;
}

Output

4.6 raised to 34 = 3.41795e+22
square root of 34 = 5.83095
absolute value of 4.6 = 4.6
ceil value of 11.8 = 12
floor value of 11.8 = 11
log of 1000 (base 10) = 3
exponential value of 4.6 = 99.4843
sin 45 = 0.850904
arc tan of 1 = 0.785398
cosh 45 = 1.74671e+19

Summary

In this article, we studied various useful mathematical functions offered by C++. We can directly use them in our program wherever required by including <math.h> or <cmath> header file. We also learnt how to use these functions with an example.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google | Facebook


Leave a Reply

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