Welcome to The Coding College! This tutorial will introduce the cmath library in C++, which provides essential mathematical functions. Understanding and using the cmath library is crucial for performing various calculations like trigonometry, logarithms, powers, and more.
What is the cmath Library?
The cmath library in C++ includes a collection of mathematical functions that allow you to perform complex calculations. These functions operate on floating-point numbers and provide accurate and efficient results.
To use the cmath library, include it in your program with:
#include <cmath>
Common cmath Functions
Here’s a quick overview of the most commonly used functions in the cmath library:
Function | Description | Example |
---|---|---|
sqrt(x) | Calculates the square root of x . | sqrt(16) → 4 |
pow(x, y) | Raises x to the power of y . | pow(2, 3) → 8 |
abs(x) | Returns the absolute value of x . | abs(-5) → 5 |
ceil(x) | Rounds x up to the nearest integer. | ceil(4.2) → 5 |
floor(x) | Rounds x down to the nearest integer. | floor(4.8) → 4 |
round(x) | Rounds x to the nearest integer. | round(4.5) → 5 |
log(x) | Returns the natural logarithm of x . | log(2.71828) → 1 |
log10(x) | Returns the base-10 logarithm of x . | log10(100) → 2 |
sin(x) | Calculates the sine of x (in radians). | sin(3.14159/2) → 1 |
cos(x) | Calculates the cosine of x (in radians). | cos(3.14159) → -1 |
tan(x) | Calculates the tangent of x (in radians). | tan(0) → 0 |
Examples of Using cmath Functions
Example 1: Basic Math Functions
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = 25.0;
cout << "Square root of " << x << " is " << sqrt(x) << endl;
cout << "2 raised to the power 3 is " << pow(2, 3) << endl;
cout << "Absolute value of -7 is " << abs(-7) << endl;
return 0;
}
Output:
Square root of 25 is 5
2 raised to the power 3 is 8
Absolute value of -7 is 7
Example 2: Trigonometric Functions
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double angle = 3.14159 / 4; // 45 degrees in radians
cout << "Sine of 45 degrees: " << sin(angle) << endl;
cout << "Cosine of 45 degrees: " << cos(angle) << endl;
cout << "Tangent of 45 degrees: " << tan(angle) << endl;
return 0;
}
Output:
Sine of 45 degrees: 0.707107
Cosine of 45 degrees: 0.707107
Tangent of 45 degrees: 1
Example 3: Rounding Numbers
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num = 4.5678;
cout << "Original number: " << num << endl;
cout << "Rounded to nearest integer: " << round(num) << endl;
cout << "Ceil of number: " << ceil(num) << endl;
cout << "Floor of number: " << floor(num) << endl;
return 0;
}
Output:
Original number: 4.5678
Rounded to nearest integer: 5
Ceil of number: 5
Floor of number: 4
Example 4: Logarithmic Functions
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num = 100.0;
cout << "Natural log of 100: " << log(num) << endl;
cout << "Base-10 log of 100: " << log10(num) << endl;
return 0;
}
Output:
Natural log of 100: 4.60517
Base-10 log of 100: 2
Example 5: Combined Calculations
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = -8.4;
double y = 2.3;
double result = pow(abs(x), y) + ceil(sqrt(y));
cout << "Result of the calculation: " << result << endl;
return 0;
}
Output:
Result of the calculation: 92.2116
Working with Angles in Degrees
Most trigonometric functions in cmath operate with radians. To work with degrees:

Example: Converting Degrees to Radians
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14159;
int main() {
double degrees = 90.0;
double radians = degrees * (PI / 180);
cout << "Sine of " << degrees << " degrees: " << sin(radians) << endl;
return 0;
}
Advanced cmath Functions
Function | Description | Example |
---|---|---|
exp(x) | Returns exe^x. | exp(1) → 2.71828 |
hypot(x, y) | Returns the hypotenuse of a right triangle. | hypot(3, 4) → 5 |
fmod(x, y) | Returns the remainder of x/yx / y. | fmod(7.5, 2) → 1.5 |
isnan(x) | Checks if x is NaN (Not a Number). | isnan(NaN) → true |
isinf(x) | Checks if x is infinity. | isinf(INFINITY) → true |
Summary
- The cmath library provides functions for complex mathematical operations.
- Supports arithmetic, trigonometric, logarithmic, and power functions.
- Useful for scientific, engineering, and game development applications.
- Functions are efficient, accurate, and easy to use.
Explore More at The Coding College
Discover more about advanced C++ programming topics at The Coding College. Learn, code, and build with us!