It also avoids potential overflow while computing median position (found by Frédéric Delanoy).
Attach a file by drag & drop or click to upload