Reading — step 1 of 5
Learn
Numbers and Math
C++ arithmetic is C arithmetic — same operators, same types, same two traps (integer division, silent conversions) — plus stream-based output that changes how you format results. If you've done the C track this is review with new clothes; if not, the traps are worth full attention because they're identical in half the languages ever made.
The two integer traps
7 / 2 // 3 — int ÷ int truncates toward zero
7 % 2 // 1 — remainder
99 / 100 // 0 — the percentage killer
7.0 / 2 // 3.5 — one double lifts the whole expression
Division's result type is decided by the operands — assigning to a double afterward changes nothing (double d = 7 / 2; is 3.0; the truncation already happened). Fix at an operand: static_cast<double>(a) / b. That static_cast<double>(…) is C++'s deliberately-noisy cast syntax — uglier than C's (double)a on purpose, so conversions are greppable and reviewers see every place precision changes. Use it; it marks you as someone writing C++ rather than C-with-cout.
And conversions narrow silently, C-style: int n = 3.99; compiles, n is 3, no complaint (a warning with -Wall, which is one more reason it stays on). C++ trusts you meant it; write the static_cast so readers know you did.
Types
Same menu as C: int (32-bit, ±2.1 billion), long long (64-bit — reach for it whenever sums or products can run big), double (the floating default), bool (real type: true/false — C++ fixed that piece of C). Floats carry the universal caveats: binary representation (0.1 + 0.2 != 0.3), so never == two doubles (compare std::abs(a-b) < 1e-9), never floats for money.
Overflow: unsigned wraps, signed overflow is undefined behavior — same sharp edge as C, same cure: pick long long before multiplying big inputs.
The math toolbox
#include <cmath>
std::sqrt(2.0) // 1.41421…
std::pow(2, 10) // 1024.0 — returns double
std::abs(-3.5) // 3.5 — <cmath>'s handles doubles
std::max(a, b) // <algorithm> — works on any comparable type
std::min(a, b)
std::max/std::min are tiny previews of the Algorithms lesson: generic functions that work on ints, doubles, strings — anything ordered — because they're templates. You'll write your own in chapter 6.
Output precision (preview)
std::cout << 3.14159 prints 3.14159, but std::cout << 2.0/3 prints 0.666667 — six significant digits by default, which is rarely what a grader's 0.67 wants. Controlling it takes the <iomanip> header and two manipulators (std::fixed, std::setprecision) — the Output Formatting lesson makes them systematic; if your exercise's expected output shows decimals, peek ahead.
Your exercise: Rectangle Area
Read dimensions, multiply, print. The decision tree from every track applies verbatim: examples show integers → int (or long long if big) and plain <<; examples show decimals → double in, fixed << setprecision(2) out to match the shown format. The multiplication is one *; the grade is in the types around it, which is why this exercise appears in every language on the platform — the answer is portable, the type discipline is the local dialect.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…