R.5 — cmath: number tools
Read one box at a time. No rush.
All need #include <cmath>.
| Code | What it does | Example |
|---|---|---|
std::sqrt(x) | square root | std::sqrt(25) → 5 |
std::pow(a, b) | a to the power of b | std::pow(2, 3) → 8 |
std::abs(x) | remove the minus sign | std::abs(-7) → 7 |
std::round(x) | round to nearest | std::round(2.5) → 3 |
std::floor(x) | round down | std::floor(2.9) → 2 |
std::ceil(x) | round up | std::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) };Tip: press ← / → to move between lessons, / to search.