Integer vs Floating Point Types
Pick the right numeric type by understanding sizes, ranges, and precision trade-offs.
Integer vs Floating Point Types is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Two Number Worlds
C++ has two families of numeric types: integers (no fractions, exact) and floating point (approximate, with fractions). Pick the right family for the job.
Integer Types
Built-in integer types and typical sizes on 64-bit systems:
char— 1 byteshort— 2 bytesint— 4 byteslong— 4 or 8 byteslong long— 8 bytes
Signed vs Unsigned
Add unsigned to drop the negative range. Unsigned arithmetic wraps around modulo 2^N — sometimes useful, often a footgun.
unsigned int u = 0;
u -= 1; // wraps around to 4,294,967,295Fixed-Width Integers
Use the types in <cstdint> when you need exact sizes — for protocols, file formats, or platform portability.
#include <cstdint>
int8_t tiny = 100;
int32_t word = 100000;
int64_t big = 1LL << 40;
uint64_t huge = 18446744073709551615ULL;Floating Point Types
Three floating types of increasing precision:
float— 32-bit, about 7 decimal digitsdouble— 64-bit, about 15 decimal digitslong double— typically 80 or 128 bits
IEEE 754 Format
Floats follow the IEEE 754 standard: sign, exponent, mantissa. They can represent very large and very small values, but not exactly.
Floating Point Pitfalls
Most decimal numbers cannot be represented exactly in binary. 0.1 + 0.2 is not exactly 0.3.
double sum = 0.1 + 0.2;
std::cout << (sum == 0.3); // false on most systems
std::cout << sum; // 0.3 but printed approximatelyComparing Floats Safely
Never compare floats with ==. Compare within a small tolerance (epsilon).
bool approxEqual(double a, double b, double eps = 1e-9) {
return std::abs(a - b) < eps;
}Special Float Values
Floats have three special values: NaN (not a number), +inf, and -inf. Use the predicates in <cmath> to check for them.
#include <cmath>
double x = std::sqrt(-1.0);
if (std::isnan(x)) std::cout << "Not a number";
if (std::isinf(x)) std::cout << "Infinity";When to Use Each
Choose by domain:
- Counts and indices →
intorsize_t - Scientific or graphical math →
double - Money → never
double! Use scaled integers (cents) or a decimal library
Type Sizes vary by Platform
Standard guarantees only minimum sizes. int may be 16, 32, or 64 bits. For portability use <cstdint> fixed-width types in data structures.
Quick Check
Which type should you use to represent monetary values precisely?
Recap
Integers are exact; floats are approximate. Pick int or fixed-width integers for counts, double for scientific math, and never use floats for money. Compare floats with a tolerance, never ==.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Integer vs Floating Point Types” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Integer vs Floating Point Types”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Integer vs Floating Point Types”?
Pick the right numeric type by understanding sizes, ranges, and precision trade-offs. 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Integer vs Floating Point Types” 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