Calculate the remainder of an Euclidean division (modulo operation) in python

Published: March 08, 2019

DMCA.com Protection Status

To calculate the remainder of an Euclidean division in python, a solution is to use the modulo operator %, example:

>>> 10 % 2
0

since 10 = 2 * 5 + 0

>>> 10 % 3
1

since 10 = 3 * 3 + 1

Another solution is to use the python module "math" with the function math.fmod():

>>> import math
>>> math.fmod(10.0,2.0)
0.0
>>> math.fmod(10.0,3.0)
1.0
>>> math.fmod(11.0,3.2)
1.3999999999999995

since 11 = 3 * 3.2 + 1.3999999999999995

References