Programming Search⌘K

Featurecore

Integer division and modulo

Also called floor division, Python // operator, Python % operator, divmod, negative modulo in Python, math.fmod.

Python's // rounds the quotient toward minus infinity, and % takes the sign of the divisor, so x == (x//y)*y + x%y. Go truncates instead. math.fmod() follows C for floats.

Language
Python

Synopsis

print(7 // 2, -7 // 2, 7 // -2, -7 // -2)
print(7 % 3, -7 % 3, 7 % -3, -7 % -3)
print(divmod(-7, 3), divmod(7.5, 2))
Output
3 -4 -4 3
1 2 -2 -1
(-3, 2) (3.0, 1.5)

-7 // 2 is -4, not -3, and -7 % 3 is 2, not -1. The remainder has the sign of the divisor.

Semantics

r1. // gives the result of mathematical division "with the 'floor' function applied to the result". For integers the result is an integer.1 The result "is always rounded towards minus infinity": 1//2 is 0, (-1)//2 is -1, 1//(-2) is -1 and (-1)//(-2) is 0.2 Example: floor-and-mod.

r2. % gives the remainder of the division of the first argument by the second. The result has "the same sign as its second operand (or zero)", and its absolute value is smaller than the absolute value of the second operand.1 Example: floor-and-mod.

r3. The two operators are connected by the identity x == (x//y)*y + (x%y), and divmod(x, y) == (x//y, x%y).1 For floats, divmod() returns (q, a % b) where q is usually math.floor(a / b) but may be 1 less, and q * b + a % b is very close to a.3 Example: identity.

r4. The numeric arguments are converted to a common type first. A zero right operand raises ZeroDivisionError. The arguments can be floats: 3.14 % 0.7 equals 0.34.1 //, % and divmod() are not defined for complex numbers.1

r5. For floats, abs(x % y) < abs(y) can fail because of rounding. -1e-100 % 1e100 must have the sign of 1e100, so it is 1e100 - 1e-100, which rounds to 1e100. math.fmod() gives a result with the sign of the first argument, so math.fmod(-1e-100, 1e100) is -1e-100.1 The math documentation prefers fmod() for floats and % for integers.4 Example: fmod-vs-mod.

r6. decimal.Decimal follows a different rule. Its % gives a result with the sign of the dividend, and its // truncates toward zero, which keeps the identity x == (x // y) * y + x % y.5 Example: decimal-truncates.

Examples

The identity of rule r3 holds for every sign combination.

pairs = [(17, 5), (-17, 5), (17, -5), (-17, -5)]
print(all(x == (x // y) * y + x % y for x, y in pairs))
print([divmod(x, y) for x, y in pairs])
Output
True
[(3, 2), (-4, 3), (-4, -3), (3, -2)]

A floored % is useful for wrap-around arithmetic, such as hours on a clock. The result is never negative for a positive divisor (rule r2).

hour = 1
print((hour - 3) % 12, (hour - 3) % 24, (hour + 25) % 24)
Output
10 22 2

% and math.fmod() differ for operands with different signs (rule r5).

import math

print(-7 % 3, math.fmod(-7, 3))
print(-1e-100 % 1e100, math.fmod(-1e-100, 1e100))
Output
2 -1.0
1e+100 -1e-100

Pitfalls

int(a / b) is not a // b

int() truncates toward zero, but // rounds toward minus infinity (rule r1). For a negative quotient the two differ.

This code runs but gives a wrong answer on purpose.

a, b = -7, 2
print(int(a / b), a // b)
Output
-3 -4

Decimal does not floor

Code that works with int can change its result when the values become Decimal (rule r6).

This code runs but gives a wrong answer on purpose.

from decimal import Decimal

print(-7 % 4, Decimal(-7) % Decimal(4))
print(-7 // 4, Decimal(-7) // Decimal(4))
Output
1 -3
-2 -1

Division by zero

// and % with a zero right operand raise ZeroDivisionError, for floats too (rule r4).

This code raises ZeroDivisionError, on purpose.

print(5.0 % 0)

Comparison

Go truncates: for integers, x / y is "truncated towards zero", and x % y has the sign of x. In Go -5 / 3 is -1 and -5 % 3 is -2. In Python -5 // 3 is -2 and -5 % 3 is 1 (rules r1 and r2). Both languages keep an identity of the form x = q*y + r.6

References

  1. The Python Language Reference, Python 3.15, 6.7. Binary arithmetic operations, and footnotes 1 and 2
  2. Built-in Types, Python 3.15 documentation, Numeric Types - int, float, complex, notes 1 and 2
  3. Built-in Functions, Python 3.15 documentation, divmod()
  4. math, Mathematical functions, Python 3.15 documentation, math.fmod()
  5. decimal, Decimal fixed-point and floating-point arithmetic, Python 3.15 documentation, Decimal objects
  6. The Go Programming Language Specification, Integer operators

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source