codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

78 / 87 topics
76<iostream> Reference77<fstream> Reference78<cmath> Reference79<string> Reference80<cstring> Reference81<ctime> Reference82<vector> Reference83<algorithm> Reference
Tutorials/C++ Programming/&lt;cmath&gt; Reference
⚡C++ Programming

&lt;cmath&gt; Reference

Updated 2026-05-12
30 min read

<cmath> Reference

The &lt;cmath&gt; library in C++ provides a wide range of mathematical functions that are essential for scientific calculations, engineering projects, and any application requiring precise numerical operations. Understanding these functions is crucial for writing efficient and accurate code.

In this tutorial, we will explore key mathematical functions provided by the &lt;cmath&gt; library, including abs, ceil, floor, round, sqrt, pow, log, sin, cos, tan, fmod, and remainder. Each function will be explained with real-world analogies, followed by code examples and expected outputs.

Introduction

The &lt;cmath&gt; library is part of the C++ Standard Library and includes functions for trigonometric calculations, logarithms, exponentiation, and more. These functions are optimized for performance and provide a consistent interface across different platforms.

Understanding these mathematical functions can greatly enhance your ability to solve complex problems in fields such as physics, engineering, finance, and data science.

Core Content

1. Absolute Value - abs

The abs function returns the absolute value of an integer. The absolute value is the non-negative value of a number without regard to its sign.

Example:

abs.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 int num = -10;
6 std::cout << "Absolute value of " << num << " is " << abs(num) << std::endl;
7 return 0;
8}
Output
Absolute value of -10 is 10

2. Ceiling - ceil

The ceil function returns the smallest integer greater than or equal to a given floating-point number.

Example:

ceil.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 double num = 3.14;
6 std::cout << "Ceiling of " << num << " is " << ceil(num) << std::endl;
7 return 0;
8}
Output
Ceiling of 3.14 is 4

3. Floor - floor

The floor function returns the largest integer less than or equal to a given floating-point number.

Example:

floor.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 double num = 3.14;
6 std::cout << "Floor of " << num << " is " << floor(num) << std::endl;
7 return 0;
8}
Output
Floor of 3.14 is 3

4. Rounding - round

The round function rounds a floating-point number to the nearest integer.

Example:

round.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 double num = 3.67;
6 std::cout << "Rounded value of " << num << " is " << round(num) << std::endl;
7 return 0;
8}
Output
Rounded value of 3.67 is 4

5. Square Root - sqrt

The sqrt function returns the square root of a non-negative floating-point number.

Example:

sqrt.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 double num = 16;
6 std::cout << "Square root of " << num << " is " << sqrt(num) << std::endl;
7 return 0;
8}
Output
Square root of 16 is 4

6. Power - pow

The pow function returns the result of raising a base number to an exponent.

Example:

pow.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 double base = 2;
6 double exponent = 3;
7 std::cout << base << " raised to the power of " << exponent << " is " << pow(base, exponent) << std::endl;
8 return 0;
9}
Output
2 raised to the power of 3 is 8

7. Logarithm - log

The log function returns the natural logarithm (base e) of a number.

Example:

log.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 double num = 2.718;
6 std::cout << "Natural logarithm of " << num << " is " << log(num) << std::endl;
7 return 0;
8}
Output
Natural logarithm of 2.718 is 1

8. Trigonometric Functions - sin, cos, tan

The &lt;cmath&gt; library provides trigonometric functions such as sin, cos, and tan. These functions take an angle in radians and return the sine, cosine, or tangent of that angle.

Example:

trig.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 double angle = M_PI / 4; // 45 degrees
6 std::cout << "Sine of " << angle << " is " << sin(angle) << std::endl;
7 std::cout << "Cosine of " << angle << " is " << cos(angle) << std::endl;
8 std::cout << "Tangent of " << angle << " is " << tan(angle) << std::endl;
9 return 0;
10}
Output
Sine of 0.785398 is 0.707107
Cosine of 0.785398 is 0.707107
Tangent of 0.785398 is 1

9. Modulo - fmod

The fmod function returns the floating-point remainder of dividing one number by another.

Example:

fmod.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 double numerator = 10;
6 double denominator = 3;
7 std::cout << "Remainder of " << numerator << " divided by " << denominator << " is " << fmod(numerator, denominator) << std::endl;
8 return 0;
9}
Output
Remainder of 10 divided by 3 is 1

10. Remainder - remainder

The remainder function returns the floating-point remainder of dividing one number by another, similar to fmod, but with different rounding behavior.

Example:

remainder.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 double numerator = 10;
6 double denominator = 3;
7 std::cout << "Remainder of " << numerator << " divided by " << denominator << " is " << remainder(numerator, denominator) << std::endl;
8 return 0;
9}
Output
Remainder of 10 divided by 3 is 1

Practical Example

Let's create a complete program that demonstrates the use of several &lt;cmath&gt; functions in a practical scenario. We'll calculate the distance between two points using the Euclidean distance formula and apply some trigonometric operations.

practical.cpp
1#include <iostream>
2#include <cmath>
3
4int main() {
5 double x1 = 3, y1 = 4;
6 double x2 = 6, y2 = 8;
7
8 // Calculate the distance between two points
9 double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
10 std::cout << "Distance between (" << x1 << ", " << y1 << ") and (" << x2 << ", " << y2 << ") is " << distance << std::endl;
11
12 // Calculate the angle using atan2
13 double angle = atan2(y2 - y1, x2 - x1);
14 std::cout << "Angle between points is " << angle * 180 / M_PI << " degrees" << std::endl;
15
16 return 0;
17}
Output
Distance between (3, 4) and (6, 8) is 5
Angle between points is 53.1301 degrees

Summary

FunctionDescription
absReturns the absolute value of an integer.
ceilReturns the smallest integer greater than or equal to a floating-point number.
floorReturns the largest integer less than or equal to a floating-point number.
roundRounds a floating-point number to the nearest integer.
sqrtReturns the square root of a non-negative floating-point number.
powReturns the result of raising a base number to an exponent.
logReturns the natural logarithm (base e) of a number.
sinReturns the sine of an angle in radians.
cosReturns the cosine of an angle in radians.
tanReturns the tangent of an angle in radians.
fmodReturns the floating-point remainder of dividing one number by another.
remainderReturns the floating-point remainder of dividing one number by another, with different rounding behavior.

What's Next?

Now that you have a solid understanding of the &lt;cmath&gt; library and its functions, it's time to explore string manipulation in C++ using the &lt;string&gt; library. This will allow you to handle text data more effectively in your programs.

Next Topic: <string> Reference


Previous<fstream> ReferenceNext <string> Reference

Recommended Gear

<fstream> Reference<string> Reference