Skip to main content
Be Bitwise

Numeric Types: Integers & Floats

Variables & Data Types · Coding Fundamentals track

5 min read

Part ofSystems Programming Companion, week 2

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:

TypeBitsUnsigned RangeSigned Range
int880 to 255-128 to 127
int16160 to 65,535-32,768 to 32,767
int32320 to ~4.3 billion~-2.1 billion to ~2.1 billion
int64640 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.

text
let score = 42 // integer
let population = 8000000000 // needs at least 33 bits, so use int64

Integer 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.

Key Concept

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:

text
value = sign * mantissa * 2^exponent

The 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.
text
let pi = 3.14159
let avogadro = 6.022e23 // 6.022 times 10 to the 23rd
let tiny = 1.0e-10 // 0.0000000001

Floating-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.

text
result = 0.1 + 0.2
// result is 0.30000000000000004, not exactly 0.3

Here 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.

263
262
261
260
259
258
257
256
255
254
253
252
251
250
249
248
247
246
245
244
243
242
241
240
239
238
237
236
235
234
233
232
231
230
229
228
227
226
225
224
223
222
221
220
219
218
217
216
215
214
213
212
211
210
29
28
27
26
25
24
23
22
21
20
Decimal4591870180066957722
Hex0x3FB999999999999A
Binary00111111 10111001 10011001 10011001 10011001 10011001 10011001 10011010
FieldBitsHexDecimal
sign630x00
exponent (bias 1023)62-520x3FB1019
mantissa51-00x999999999999A2702159776422298
As float64: 0.1Kind: normalScale: 1.fraction × 2-4 (stored exponent 1019)
Exact value stored: 0.1000000000000000055511151231257827021181583404541015625

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:

Try itSet steps to 100 and target to 10.0, then shrink epsilon to 1e-20 and watch the tolerant test fail too.
C (GCC 9.2.0)

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.0 gives +Infinity.
  • NaN (Not a Number): the result of undefined operations. 0.0 / 0.0 gives NaN. NaN has a peculiar property: it isn't equal to anything, including itself. NaN == NaN is false.
  • Negative zero: -0.0 exists and is equal to +0.0 for 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.

Try itStart u at 250 and add 10 instead, and predict the output before you run it.
C (GCC 9.2.0)

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.