← Home

008. Integers

Exact whole-number arithmetic

008. Integers

Aryan’s RAM monitor reports memory usage in megabytes. Megabytes are whole numbers — you can’t have 0.5 MB in a process table.

He writes two helper functions:

  • available_ram(total, used): how much RAM is free
  • process_ram_cost(count, size_mb): total RAM cost for N identical processes

In Python, int + int = int and int * int = int. These operations stay exact. But int / int = float — always, even if the result is a whole number.

Aryan’s process_ram_cost function accidentally uses / instead of *, which returns a float. The tests require integer results everywhere. A RAM monitor reporting “512.0 MB” instead of “512 MB” is sloppy.


💡 Fun fact: Python’s integers have unlimited precision — 2 ** 1000 is a valid computation producing a 302-digit number. In C, integers overflow silently; Python’s int never overflows, it just allocates more memory. This was a deliberate design choice to eliminate an entire class of security vulnerabilities.

⚠️ Watch out: In Python, / always returns a float — even 4 / 2 gives 2.0, not 2. Beginners coming from C expect integer division when both operands are integers. If you need a whole-number result, use // (floor division) or * for multiplication.

🤔 Think about it: If Python integers never overflow, why might you still care about the type of a result being int vs float when working with RAM sizes and process counts?

Learning objectives

  • Use integer arithmetic (+, -, *)
  • Know that / always returns float; use * for pure int multiplication
  • Understand Python ints have unlimited precision

Key concepts

  • int
  • integer arithmetic
  • types

Try it

Concept detail

Python’s int type represents whole numbers of unlimited size — no overflow, ever. 2 ** 1000 is a valid Python integer (a number with 302 digits).

Arithmetic rules for int: int + int = int int - int = int int * int = int int / int = float ← always float, even 4 / 2 = 2.0 int // int = int ← floor division (rounds toward negative infinity) int % int = int ← modulo (remainder)

The broken_code bug is semantic: / divides count BY size_mb instead of multiplying. process_ram_cost(4, 256) returns 0.015625 (4 divided by 256), not 1024. Even if the operator were corrected to *, using / would return a float — failing the type assertion tests.

In systems programming contexts (RAM, disk, network bytes), keeping types as int matters: it avoids floating-point rounding surprises and communicates clearly that these values are discrete whole-unit quantities.

Solution

def available_ram(total, used):
    return total - used

def process_ram_cost(count, size_mb):
    return count * size_mb

Tests

def test_available_ram_basic():
    assert available_ram(8192, 3072) == 5120

def test_available_ram_type():
    assert type(available_ram(8192, 3072)) == int

def test_process_ram_cost_basic():
    assert process_ram_cost(4, 256) == 1024

def test_process_ram_cost_type():
    assert type(process_ram_cost(4, 256)) == int

def test_process_ram_cost_single():
    assert process_ram_cost(1, 512) == 512

Resources