Skip to main content

Floor Division Calculator

Free Floor division Calculator for arithmetic. Enter values to get step-by-step solutions with formulas and graphs. Get results you can export or share.

Share this calculator

Formula

floor(a / b) = largest integer less than or equal to a/b

Where a is the dividend, b is the divisor, and floor rounds the exact quotient toward negative infinity. The floor remainder is computed as a - b * floor(a/b), which always has the same sign as b.

Worked Examples

Example 1: Positive Floor Division

Problem: Compute 17 floor-divided by 5 and find the remainder.

Solution: Exact division: 17 / 5 = 3.4\nFloor division: floor(3.4) = 3\nRemainder: 17 - 5 * 3 = 17 - 15 = 2\n\nVerification: 5 * 3 + 2 = 17\nIn Python: 17 // 5 = 3, 17 % 5 = 2

Result: 17 // 5 = 3 remainder 2

Example 2: Negative Floor Division vs Truncation

Problem: Compare floor division and truncation for -17 divided by 5.

Solution: Exact division: -17 / 5 = -3.4\n\nFloor division: floor(-3.4) = -4\nFloor remainder: -17 - 5 * (-4) = -17 + 20 = 3\nVerify: 5 * (-4) + 3 = -20 + 3 = -17\n\nTruncation: trunc(-3.4) = -3\nTrunc remainder: -17 - 5 * (-3) = -17 + 15 = -2\nVerify: 5 * (-3) + (-2) = -15 - 2 = -17

Result: Floor: -17 // 5 = -4 R 3 | Truncation: trunc(-17/5) = -3 R -2

Frequently Asked Questions

What is floor division and how is it different from regular division?

Floor division divides two numbers and then rounds the quotient DOWN to the nearest integer (toward negative infinity). Regular division gives the exact decimal result, while floor division always produces a whole number. For positive numbers, floor division is the same as integer division: 17 floor-divided by 5 = 3 (since 17/5 = 3.4, and floor(3.4) = 3). The key difference emerges with negative numbers: -17/5 = -3.4, and floor(-3.4) = -4 (not -3). This is distinct from truncation, which would give -3 by rounding toward zero. In Python, the // operator performs floor division, making it one of the most commonly used integer division operations in programming.

How does floor division differ from truncation division?

Floor division rounds toward negative infinity, while truncation rounds toward zero. For positive numbers, both produce identical results: floor(7/2) = trunc(7/2) = 3. The difference appears with negative results: floor(-7/2) = floor(-3.5) = -4, but trunc(-7/2) = trunc(-3.5) = -3. Truncation chops off the decimal part regardless of sign, while floor always rounds down. This distinction matters because the remainder changes too. With floor division: -7 = 2 * (-4) + 1 (remainder is positive). With truncation: -7 = 2 * (-3) + (-1) (remainder is negative). Python uses floor division for // and floor-based modulo for %, while C and Java use truncation for integer division and truncation-based modulo.

Why does Python use floor division instead of truncation?

Python uses floor division because it produces mathematically cleaner behavior for the modulo operator. With floor division, the remainder always has the same sign as the divisor, which means n % m always returns a value in the range [0, m) for positive m. This property is extremely useful for cyclic operations like clock arithmetic, array indexing, and hash tables. For example, (-1) % 12 = 11 in Python (which makes sense for clock arithmetic: one hour before 12 is 11), but (-1) % 12 = -1 in C (which is less intuitive). Guido van Rossum, Python's creator, explained this choice in a blog post, noting that the mathematical definition of modulo works better with floor division for most practical applications.

What is the modulo operator and how does it relate to floor division?

The modulo operator gives the remainder after division, but its exact behavior depends on which type of division is used. With floor division, the modulo is defined as a mod b = a - b * floor(a/b), and the result always has the same sign as the divisor b. With truncation division, modulo is a mod b = a - b * trunc(a/b), and the result has the same sign as the dividend a. For example, -7 mod 3: with floor-based modulo, -7 mod 3 = -7 - 3*floor(-7/3) = -7 - 3*(-3) = -7 + 9 = 2. With truncation-based modulo: -7 mod 3 = -7 - 3*trunc(-7/3) = -7 - 3*(-2) = -7 + 6 = -1. Both satisfy the identity a = b * quotient + remainder.

Which programming languages use floor division vs truncation?

Different programming languages make different choices for integer division. Python uses floor division with its // operator and floor-based modulo with %. Ruby and Dart also use floor division for their integer division operators. On the other hand, C, C++, Java, JavaScript, and C# use truncation for integer division and truncation-based modulo. This difference is a common source of bugs when porting code between languages, especially when negative numbers are involved. In JavaScript, there is no built-in floor division operator, so you must use Math.floor(a/b) explicitly. Some languages like Haskell provide both options with different function names: div/mod for floor-based and quot/rem for truncation-based.

How is floor division used in array indexing and data structures?

Floor division is essential for converting between different coordinate systems and for partitioning data. In a 2D grid stored as a 1D array, the row of element at index i in a grid with w columns is floor(i/w), and the column is i mod w. For hash tables, floor division helps distribute keys across buckets. In tile-based games, floor division converts pixel coordinates to tile coordinates: tile_x = floor(pixel_x / tile_width). Calendar calculations use floor division extensively: the day of the week, the week number of the year, and Julian-to-Gregorian conversions all rely on floor division. The consistency of floor division with positive remainders makes these calculations correct even at boundary cases.

References