R.5 — cmath: number tools

📘 Appendix R⏱ about 1 min read✍️ Beginner friendly
Read one box at a time. No rush.

All need #include <cmath>.

CodeWhat it doesExample
std::sqrt(x)square rootstd::sqrt(25) → 5
std::pow(a, b)a to the power of bstd::pow(2, 3) → 8
std::abs(x)remove the minus signstd::abs(-7) → 7
std::round(x)round to neareststd::round(2.5) → 3
std::floor(x)round downstd::floor(2.9) → 2
std::ceil(x)round upstd::ceil(2.1) → 3
std::sin(x), std::cos(x)trig (x in radians)

Random numbers#

Needs #include <random>.

C++
std::mt19937 rng { std::random_device{}() };std::uniform_int_distribution<int> dist { 1, 6 };int roll { dist(rng) };
Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.