Math Functions in C++

Math is a very important part of programming, and C++ is no exception.

In this chapter, we will learn basic Math operations in C++.

C++ provides several built-in mathematical functions through the <cmath> library.

In C++ you will have many built-in functions that help you perform simple and complex mathematical operations like power, square root, logarithms, trigonometry, and rounding.

Max and Min

The max(a,b) function is used to find the highest value between the a and b.

// Math functions
#include <iostream>
#include <cmath>  // Required for math functions
using namespace std;

int main(){
  int a = 10;
  int b = 12;

  cout << "The Max Value is : " << max(a, b) << endl;

  return 0;
}

The min(a,b) function is used to find the lowest value between the a and b.

// Math functions
#include <iostream>
#include <cmath>  // Required for math functions
using namespace std;

int main(){
  int a = 10;
  int b = 12;

  cout << "The Min Value is : " << min(a, b) << endl;

  return 0;
}

C++ <cmath> Library

The <cmath> library provides many functions to used for performing mathematical operations, here are some of them:

Function Description Example
sqrt(x) Returns the square root of x. sqrt(16) // 4
pow(x, y) Returns x raised to the power of y. pow(2, 3) // 8
abs(x) Returns the absolute value of x. abs(-5) // 5
ceil(x) Returns the smallest integer greater than or equal to x. ceil(4.2) // 5
floor(x) Returns the largest integer less than or equal to x. floor(4.8) // 4
round(x) Rounds x to the nearest integer. round(4.5) // 5
log(x) Returns the natural logarithm (base e) of x. log(2.71828) // 1
log10(x) Returns the base-10 logarithm of x. log10(100) // 2
sin(x) Returns the sine of x (in radians). sin(3.14159 / 2) // 1
cos(x) Returns the cosine of x (in radians). cos(0) // 1
tan(x) Returns the tangent of x (in radians). tan(3.14159 / 4) // 1
min(x, y) Returns the smaller of x and y. min(3, 7) // 3
max(x, y) Returns the larger of x and y. max(3, 7) // 7

The <cmath> library also provides the mathematical constants M_PI and M_E which represent the value of π and e respectively.

The <cmath> library is included in the <cmath> header file.

observed the below example to understand the math functions in C++.

// Math functions
#include <iostream>
#include <cmath>  // Required for math functions
using namespace std;

int main(){
  double num = 25.0, base = 2.0, exponent = 3.0;

  cout << "Square Root of " << num << " is: " << sqrt(num) << endl;
  cout << base << " raised to the power " << exponent << " is: " << pow(base, exponent) << endl;
  cout << "Absolute value of -10 is: " << abs(-10) << endl;
  cout << "Ceiling of 4.2 is: " << ceil(4.2) << endl;
  cout << "Floor of 4.9 is: " << floor(4.9) << endl;
  cout << "Rounded value of 4.5 is: " << round(4.5) << endl;
  cout << "Natural log of 2.718 is: " << log(2.718) << endl;
  cout << "Sine of 3.14 radians: " << sin(3.14) << endl;
  cout << "Cosine of 0 radians: " << cos(0) << endl;
  cout << "Tangent of 0.785 radians: " << tan(0.785) << endl;

  return 0;
}

This demonstrates how math functions in C++ can be used for various calculations.

Now that you have learned how to use math functions in C++ programming. Lets learn something about Boolean in C++ in the next section.