← C++ from Scratch
Lesson 12 of 62 · 10 XP
Maths: <cmath>
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
#include <cmath>
std::sqrt(16.0); // 4 — square root
std::pow(2, 10); // 1024 — power (the result is a double)
std::abs(-7); // 7 — absolute value
std::round(2.5); // 3 — round to the nearest
std::floor(2.7); // 2 — down, std::ceil(2.1) — 3, up
std::hypot(3, 4); // 5 — the hypotenuse: sqrt(3² + 4²)
std::max(3, 9); std::min(3, 9); // from <algorithm>
Fractional numbers are stored approximately: 0.1 + 0.2 == 0.3 is false! Compare with a tolerance:
std::abs(a - b) < 1e-9.
Task. Read the coordinates of two points x1 y1 x2 y2 (integers). Print the distance between them
with two decimal places.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.