Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Floor Division: Behavior and Edge Cases

Understand how Python's // operator rounds toward negative infinity, how it relates to modulo, and when to prefer it over true division.

~6 min readPython
#floor division#integer division#modulo#negative numbers
Illustration of Python floor division showing a number line where -4.5 rounds down to -5, contrasting with truncation to -4.

Python's floor division operator // is easy to use with positive numbers, but its behavior with negative numbers surprises many developers. For positive operands, 7 // 2 returns 3, discarding the fractional part. With negative operands, // does not truncate toward zero; it rounds toward negative infinity. In other words, the result is the largest integer less than or equal to the exact quotient. This distinction matters in algorithms that depend on consistent rounding, especially when calculating indices, pagination, or intervals.

Floor Division Rounds Toward Negative Infinity

The name "floor" comes from the mathematical floor function, which maps a real number to the greatest integer less than or equal to it. In Python, a // b computes the floor of the exact quotient a / b. For positive operands, this matches truncation. For example, 9 // 2 is 4 because 4.5 floors to 4. But with negative operands, the result differs from truncation. Consider -9 // 2. The exact quotient is -4.5. Truncation would give -4, but floor division gives -5 because -5 is the largest integer less than or equal to -4.5. This is a common source of confusion, and it directly affects code that calculates offsets or partitions data.

The same rule applies when the divisor is negative. 9 // -2 yields -5 because -4.5 floors to -5. Similarly, -9 // -2 gives 4 because the quotient is 4.5, which floors to 4. The sign of the operands does not change the rounding rule; only the mathematical value of the quotient matters.

The Difference Between Floor and Truncation

Many programming languages, such as C and Java, use truncation for integer division when both operands are integers. In those languages, -9 / 2 evaluates to -4 because the fractional part is discarded. Python's // deliberately chooses floor semantics to maintain a useful invariant with the modulo operator. This design decision means that code ported from another language may behave differently when negative numbers are involved. If you need truncation toward zero in Python, int(a / b) can work for ordinary values, but because / returns a float, it can lose precision for very large integers. Python's built-in division operators do not provide a direct truncating integer division.

Understanding the difference is critical when implementing algorithms that assume a particular rounding mode. For example, binary search that calculates the midpoint with (low + high) // 2 relies on floor division; the midpoint is never greater than the true midpoint. If a language truncates toward zero, the midpoint can move above the true midpoint when the sum is negative, which can break the search invariant. Floor division keeps the midpoint within the expected range.

Python's % operator is defined in terms of floor division. The identity a == (a // b) * b + (a % b) holds for all integers a and nonzero b. This means the remainder always has the same sign as the divisor, not the dividend. For example, -9 % 2 returns 1 because -9 // 2 is -5, and -5 * 2 + 1 equals -9. This is different from languages that define modulo to follow the sign of the dividend. The consistency between // and % is a deliberate design choice that makes it easy to reason about cyclic operations, such as rotating through a list or computing array indices.

The relationship also means that a % b can be derived from a - (a // b) * b. If you implement a custom division algorithm, you must preserve this invariant to avoid subtle bugs. The sign of the remainder is often the source of off-by-one errors when porting code from a language that uses truncation. For instance, in Java, -9 % 2 is -1, but in Python it is 1. If you are converting an algorithm that relies on the sign of the remainder, adjust the logic accordingly.

Using Floor Division with Floats

Floor division is not limited to integers. In Python 3, // works with floats and returns a float, but the result is still the floor of the division. For example, 7.0 // 2.0 returns 3.0, and -7.0 // 2.0 returns -4.0. The result type is a float when either operand is a float, even if the value is mathematically an integer. This can lead to type-related bugs if you expect an integer result. For instance, len(items) // 2 returns an integer, but len(items) // 2.0 returns a float. If you later use that value as a list index, Python will raise a TypeError because list indices must be integers. This is a common mistake when mixing numeric types.

Another edge case is division by zero. Both // and / raise ZeroDivisionError when the divisor is zero. The operators themselves do not silently return inf or nan; those values can appear only when you use numeric routines that explicitly produce floating-point special values.

Practical Examples of Floor Division

Floor division is useful in many everyday programming tasks. Pagination is a classic example: to determine the zero-based page containing an item index, you use index // page_size. This also works for offsets before zero when you are working with integer ranges that include negative values. For instance, (-1) // 10 gives -1, meaning the offset falls in the group before the zero group. If you used truncation, you would get 0, which would incorrectly place it in the zero group.

Another common use is splitting a list into chunks. The chunk index for an element is i // chunk_size. This is straightforward for positive indices, and floor division also keeps negative offsets aligned with the same grouping rule.

Floor division also appears in algorithms that compute the number of full groups. For example, total_items // items_per_group gives the number of complete groups, and total_items % items_per_group gives the remainder. This pair of operations is fundamental to many data-processing pipelines.

Common Mistakes and Compatibility Notes

One common mistake is assuming that // is equivalent to int(a / b) for integers. For positive numbers, they agree, but for negative numbers, int(a / b) truncates toward zero, so the results differ. For example, int(-9 / 2) is -4, while -9 // 2 is -5. This discrepancy can cause subtle bugs if you use int() to perform integer division in a codebase that expects floor semantics. Always use // when you need floor division.

Another compatibility note concerns Python 2. In Python 2, the / operator performed floor division on integers, but in Python 3 it performs true division. If you maintain legacy code, note that a / b in Python 2 can be equivalent to a // b in Python 3 when both are integers. This change is a common source of migration issues. The // operator is explicit in both versions and avoids that ambiguity.

Finally, be careful when mixing // with very large integers. Python's integers are arbitrary precision, so there is no overflow risk, but the floor operation itself is exact. For floats, the result may lose precision if the operands are extremely large, but that is inherent to floating-point representation, not specific to floor division.

When to Prefer Floor Division Over True Division

Choose floor division when you need an integer result and the rounding direction matters. This includes indexing, counting, and any operation where you want to partition a range into discrete buckets. True division (/) is appropriate when you need the exact quotient as a float, such as in scientific calculations or when you need to preserve the fractional part. Using // on floats is often a mistake because it discards the fractional part, but there are cases where you intentionally want to round down a float to the nearest whole number. In those cases, math.floor() is clearer than // because it explicitly communicates the intent. However, // is more concise and works directly with integers.

The decision also depends on whether you need the modulo result. If you need both the quotient and the remainder, using // and % together is the idiomatic Python approach. If you only need the quotient and the operands are integers, // is the correct choice. If you need to mimic truncation for compatibility with another language, write a helper function and document the difference, because Python's built-in operators do not provide truncating division.

In summary, floor division is a fundamental operator that behaves predictably once you understand its rounding rule. It is not a bug or an inconsistency; it is a deliberate design that keeps modulo and division consistent. By using // intentionally and being aware of its behavior with negative numbers and floats, you can avoid a class of subtle errors that often appear in production code.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.