See, in theory, I didn't make typos when coding. Explanation: iloc*loc will be negative when the sign changes between them, so we can snap if an element cross the limit.