Python Glossary Русский

Python 3.7+Numbers & mathfunction

math.remainder

remainder

The IEEE 754 remainder: x − round(x/y)·y, the result lies in [−y/2, y/2].

Syntax

math.remainder(...)

The quotient is rounded to the nearest integer (ties to even), so the sign of the result follows neither x nor y and its magnitude never exceeds half of y: remainder(11, 3) is -1.0 even though both arguments are positive. That is exactly what you want for folding angles or phases into a symmetric range, but it is not the everyday "remainder" — use % or math.fmod for that. A zero divisor raises ValueError.

Examples

import math
print(math.remainder(10, 3))   # → 1.0
print(math.remainder(11, 3))   # → -1.0
print(11 % 3)   # → 2
print(math.remainder(7, 2))   # → -1.0
print(math.remainder(1, 0))   # → ValueError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy