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
@aiden-f-dsouza▲ 7 net votes
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.