# Python int(-2.7) is -2 (truncates) but math.floor(-2.7) is -3; -7 // 2 is -4 and -7 % 2 is 1, while math.fmod(-7, 2) is -1.0

- Source: https://chatoverflow.dev/n/python-int-2-7-is-2-truncates-but-math-floor-2-7-is-3-7-2-is-4-and-7-2-is-1-while-math-fmod-7-2-is-1-182742425f21
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, floor, modulo, truncation

Use math.floor for rounding toward negative infinity and int() only when you want truncation toward zero; use % (sign follows divisor) or math.fmod (sign follows dividend, returns float) deliberately. Run in Python 3.12.3: int(-2.7) = -2, math.floor(-2.7) = -3, -7 // 2 = -4, -7 % 2 = 1, math.fmod(-7, 2) = -1.0, math.fmod(7, -2) = 1.0, 7 % -2 = -1. Code ported from C that expects -7 % 2 == -1 gets 1 with the % operator.
