Numeric Types: Integers & Floats
Variables & Data Types · Coding Fundamentals track
5 min read
In this module
Numbers in the Machine
You already know computers store everything as binary. How a program interprets those bits depends on the variable's type. The same 32 bits can represent wildly different values depending on whether you treat them as an integer or a floating-point number.
This lesson explores how numbers are represented in programming languages, and why the choice of numeric type matters more than most beginners realise.
Integers
An integer represents a whole number with no fractional part. Languages typically offer several sizes:
| Type | Bits | Unsigned Range | Signed Range |
|---|---|---|---|
| int8 | 8 | 0 to 255 | -128 to 127 |
| int16 | 16 | 0 to 65,535 | -32,768 to 32,767 |
| int32 | 32 | 0 to ~4.3 billion | ~-2.1 billion to ~2.1 billion |
| int64 | 64 | 0 to ~18.4 quintillion | ~-9.2 quintillion to ~9.2 quintillion |
Signed integers use two's complement (you met it in the binary module) to represent negative numbers. Unsigned integers use every bit for magnitude, doubling the positive range at the cost of being unable to go negative.
let score = 42 // integerlet population = 8000000000 // needs at least 33 bits, so use int64Integer arithmetic is exact. 7 + 3 is always 10, 100 / 4 is always 25. The one catch is integer division. Dividing two integers typically truncates toward zero, discarding the remainder: 7 / 2 gives 3, not 3.5. If you need the remainder, most languages provide a modulo operator (%): 7 % 2 gives 1.
Integers store exact whole numbers. The number of bits determines the range of values that can be represented. Choose too small a type and you risk overflow, where the value wraps around and produces incorrect results. This isn't theoretical. Integer overflow has caused real-world bugs in everything from video games to rocket guidance systems.
Floating-Point Numbers
When you need fractions or very large numbers, you reach for floating-point types. These follow the IEEE 754 standard, which represents numbers in a form akin to scientific notation:
value = sign * mantissa * 2^exponentThe most common sizes:
- float32 (single precision): 32 bits, broken into 1 sign bit, 8 exponent bits, 23 mantissa bits.
- float64 (double precision): 64 bits, broken into 1 sign bit, 11 exponent bits, 52 mantissa bits.
let pi = 3.14159let avogadro = 6.022e23 // 6.022 times 10 to the 23rdlet tiny = 1.0e-10 // 0.0000000001Floating-point arithmetic trades exactness for range. A float64 can represent numbers from about 10^-308 to 10^308, but it only has about 15-17 significant decimal digits of precision. The range is enormous, far wider than any integer type. The catch is that not every value within that range can be represented exactly.
The Precision Problem
Because floats use binary fractions, many ordinary decimal values can't be represented exactly. Take the fraction 1/10. In binary, it's a repeating fraction (just like 1/3 = 0.333... in decimal). The computer has to round, and those tiny rounding errors can accumulate.
result = 0.1 + 0.2// result is 0.30000000000000004, not exactly 0.3Here are the 64 bits of a float64 holding 0.1. The "exact value stored" line shows what the machine really keeps, a shade above 0.1. Load the 0.3 preset, then the 0.1 + 0.2 preset: they differ only in the last bit of the mantissa, and that one bit is the whole story. Type 0.5 or 0.25 to see values that binary stores exactly.
| Field | Bits | Hex | Decimal |
|---|---|---|---|
| sign | 63 | 0x0 | 0 |
| exponent (bias 1023) | 62-52 | 0x3FB | 1019 |
| mantissa | 51-0 | 0x999999999999A | 2702159776422298 |
This isn't a bug. It's a fundamental consequence of representing decimal fractions in binary. For most applications the error is negligible. For financial calculations or other domains demanding exact decimal arithmetic, special types like fixed-point or arbitrary-precision decimals get used instead.
Here's the same drift in C. Adding 0.1 ten times ought to give exactly 1:
The == test fails because the stored total sits a hair under 1. The tolerant test asks a better question, "are these close enough?", and how close counts as enough depends on your data.
Floating-point precision quirks have caused real problems in production systems. Currency calculations that accumulate rounding errors, scientific simulations that drift from reality, comparison operations that fail because two "equal" values differ by a tiny epsilon. All of them downstream consequences of floating-point representation.
Special Floating-Point Values
IEEE 754 defines several special values you'll run into:
- Positive and negative infinity: the result of dividing by zero or of overflow.
1.0 / 0.0gives+Infinity. - NaN (Not a Number): the result of undefined operations.
0.0 / 0.0givesNaN. NaN has a peculiar property: it isn't equal to anything, including itself.NaN == NaNisfalse. - Negative zero:
-0.0exists and is equal to+0.0for comparison purposes, but they're stored differently and can behave differently in certain operations.
These special values propagate through calculations. Any arithmetic involving NaN produces NaN. One invalid operation can "infect" an entire chain of calculations with NaN values.
Overflow and Underflow
Integer overflow happens when a calculation produces a result larger than the type can hold. In an 8-bit unsigned integer, 255 + 1 wraps around to 0. In signed integers the hardware does the same wrap, so the maximum positive int32 plus one comes out as the most negative int32. C doesn't promise you that, though. Signed overflow in C is undefined behaviour: the compiler may assume it never happens, and is entitled to delete a check like if (x + 1 < x) as dead code. Unsigned arithmetic, on the other hand, is defined to wrap.
Look closely and the program never computes INT_MAX + 1 in a plain int. It does the sum in a wider type, or in unsigned arithmetic where wrapping is legal, and asks __builtin_add_overflow (a GCC and Clang extension) whether the int version would have overflowed. The int8_t line is safe for a subtler reason: s + 1 is computed as an int, and squeezing 128 back into 8 bits is implementation-defined rather than undefined. GCC wraps it to −128.
Floating-point underflow happens when a number is too close to zero to be represented normally. IEEE 754 doesn't drop straight to zero. Below the smallest normal value it switches to subnormal numbers, which shed precision bit by bit as they shrink, and only past the smallest subnormal (about 4.9 × 10⁻³²⁴ for a float64) does the result round to zero. That zero can then cause division-by-zero errors downstream.
Both are real sources of bugs in production software. Knowing your numeric limits is part of writing correct programs.
Choosing the Right Numeric Type
The choice between integer and float, and between different sizes, depends on what you're modelling:
- Counting discrete things (users, items, loop iterations): use integers.
- Measuring continuous quantities (temperature, distance, probability): use floats.
- Need exact decimal arithmetic (money, financial calculations): use fixed-point or decimal types.
- Working with very large whole numbers (cryptography, combinatorics): use arbitrary-precision integers (big integers).
Every numeric type is a tradeoff between range, precision, memory usage, and performance. Understanding the tradeoffs lets you make informed decisions instead of leaning on defaults. Next lesson we move on to text and boolean types.