The cmath Header: pow sqrt abs round
Use the standard math library for common mathematical operations.
The cmath Header: pow sqrt abs round is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The <cmath> Header
Standard mathematical functions live in <cmath>. Most accept and return double; overloads for float and long double exist too.
Powers: pow
std::pow(base, exp) raises base to the power exp. Returns double.
#include <cmath>
double x = std::pow(2.0, 10); // 1024.0
double y = std::pow(9.0, 0.5); // 3.0Square Roots: sqrt cbrt
sqrt returns the square root; cbrt returns the cube root.
double s = std::sqrt(2.0); // 1.414...
double c = std::cbrt(27.0); // 3.0Absolute Value: abs
std::abs works on both integer and floating types. For integers use <cstdlib> or just std::abs.
#include <cmath>
#include <cstdlib>
int i = std::abs(-5); // 5
double d = std::abs(-3.14); // 3.14Rounding: floor ceil round trunc
Four rounding modes:
floor— toward minus infinityceil— toward plus infinityround— to nearest, halves away from zerotrunc— toward zero
Rounding Examples
See how they differ on positive and negative numbers.
std::cout << std::floor(3.7) << "\n"; // 3
std::cout << std::ceil(3.2) << "\n"; // 4
std::cout << std::round(3.5) << "\n"; // 4
std::cout << std::trunc(-3.7) << "\n"; // -3
std::cout << std::floor(-3.7) << "\n"; // -4Modulo for Floats: fmod
The % operator only works on integers. Use std::fmod for floats.
double r = std::fmod(10.5, 3.0); // 1.5Logarithms
Three flavors of log:
std::log— natural log (base e)std::log10— base 10std::log2— base 2 (C++11)
Exponential: exp
std::exp(x) computes e^x. The inverse of natural log.
double e = std::exp(1.0); // 2.718...
double x = std::log(e); // 1.0Trigonometry
sin, cos, tan and their inverses (asin, acos, atan). All take angles in radians.
double y = std::sin(M_PI / 2); // 1.0
// Convert degrees to radians: deg * M_PI / 180Useful Constants (C++20)
C++20 added named math constants in <numbers>: std::numbers::pi, e, sqrt2, and more.
#include <numbers>
std::cout << std::numbers::pi; // 3.14159...
std::cout << std::numbers::e; // 2.71828...Quick Check
Which function rounds -3.7 to -4?
Recap
<cmath> covers powers, roots, absolute value, rounding, modulo, logs, exponentials, and trig. Trig works in radians. C++20 added <numbers> for named constants like std::numbers::pi.
Frequently asked questions
Is the “The cmath Header: pow sqrt abs round” lesson free?
Yes — the full text of “The cmath Header: pow sqrt abs round” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “The cmath Header: pow sqrt abs round”?
Use the standard math library for common mathematical operations. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The cmath Header: pow sqrt abs round” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Integer vs Floating Point Types
- The cmath Header: pow sqrt abs round
- Random Numbers with the random Header
- Numeric Limits and Overflow Behaviour