Floor Division Calculator

Calculate floor division (a // b), quotient, remainder, and verify the division relationship a = b × q + r.

Enter Dividend and Divisor

÷

Examples with Negative Numbers

aba // ba % bVerify
175325*3+2=17
-175-435*(-4)+3=-17
17-5-4-3-5*(-4)+(-3)=17
-17-53-2-5*3+(-2)=-17
7.5231.52*3+1.5=7.5

Result

Floor Division (a // b)
3
17 // 5 = 3
Dividend (a) 17
Divisor (b) 5
Exact division (a / b) 3.4
Floor quotient (q) 3
Remainder (r) 2
Verification: b*q + r 5*3 + 2 = 17
Truncated division 3

Step-by-Step Solution

a // b = floor(a / b) = floor(3.4) = 3

Understanding Floor Division

Floor division, denoted as a // b in Python and many other programming languages, divides two numbers and rounds the result down to the nearest integer (toward negative infinity). This is different from truncated division, which rounds toward zero. The distinction matters significantly when dealing with negative numbers.

Mathematically, floor division is defined as: a // b = ⌊a / b⌋, where ⌊ ⌋ denotes the floor function. The associated remainder satisfies the relationship a = b × q + r, where q is the floor quotient and r is the remainder.

Floor Division Concepts

Floor Division Definition

Divide and round down to the nearest integer (toward negative infinity).

a // b = floor(a / b)

Division Relationship

The dividend equals the divisor times the quotient plus the remainder.

a = b * q + r

Remainder (Modulo)

The remainder after floor division. The sign matches the divisor.

r = a - b * floor(a / b)

Floor vs Truncated

Floor rounds toward -infinity; truncated division rounds toward zero.

-7 // 2 = -4 vs trunc(-7/2) = -3

Remainder Sign Rule

In floor division, the remainder has the same sign as the divisor.

0 ≤ |r| < |b|, sign(r) = sign(b)

Python Operator

Python uses // for floor division and % for the corresponding modulo.

q = a // b, r = a % b

Floor Division with Negative Numbers

The behavior of floor division with negative numbers often surprises beginners. When both operands are positive, floor division and truncated division give the same result. But with negative numbers, they differ: floor division rounds toward negative infinity, while truncation rounds toward zero.

For example: -7 // 2 = -4 (floor), but trunc(-7/2) = -3. The floor remainder is -7 - 2*(-4) = 1 (positive, matching the divisor's sign), while the truncated remainder would be -7 - 2*(-3) = -1.

Programming Language Differences

  • Python: The // operator performs floor division. The % operator gives the floor remainder.
  • C/C++/Java: The / operator performs truncated division for integers. The % operator gives the truncated remainder.
  • JavaScript: Math.floor(a/b) for floor division. No built-in operator.
  • Ruby: The / operator for integers performs floor division (like Python).

Common Applications

  • Converting units (e.g., seconds to hours, minutes, seconds).
  • Array indexing and pagination calculations.
  • Hash table implementations and modular arithmetic.
  • Date and calendar calculations (day of week, etc.).
  • Dividing items into equal groups with leftovers.