# AI for Robotics, Udacity, Python, normalizing x,y error in toroidal world

def eval(r, p):
'''
param r: robot
param p: resampled probabilities array of particles
'''
sum = 0.0;
for i in range(len(p)): # calculate mean error
# dividing by world size ensures that toroidal world doesn't skew the resulting estimated error enclosed in
# the boundary
dx = (p[i].x - r.x) + (world_size/2.0)) % world_size - (world_size/2.0)
dy = (p[i].y - r.y) + (world_size/2.0)) % world_size - (world_size/2.0)
err = sqrt(dx * dx + dy * dy)
sum += err
return sum / float(len(p))


This is a robot localization problem in the Udacity AI for Robotics course, section on particle filters. The world is of size 100 *100 and p[i].x - r.x is the x coordinate of the particle - that of the robot) so it's an error calculation. The world is toroidal or cyclic in Professor Thrun's words, so he adds world_size/2.0 and then modulo world_size then subtracts (world_size/2.0) so that in his words, "so that the cyclic world does not skew the resulting estimated error enclosed in the boundary."
He gives examples of how if a robot is at 99.9 or 0.0, it should be basically next to each other but the error calculation would consider them far different without this "normalization" procedure.

The video is Lesson, number 24 in the Udacity course mentioned above.
Does anyone know how this works? I am not that strong in math.