About a week ago, I was working on some code that was moving two entities along the x-axis. Inside the library, their x value was a double, but the public interface only allowed for meters in the form of an integer. I used Math.Round to translate the double value into that integer.
One of the requirements for the code was that the entities could never be closer than one meter to each other. I used the rounded value to check this.
As you might have guessed by now, the code kept failing. Even though both entities started out exactly one meter apart, had the same starting speed and were accelerating with the same speed, using the same algorithm, the code kept failing.
At first, I thought it was some kind of double precision error. But this wouldn’t make much sense, as the algorithm would result in the same precision loss. After some debugging, I eventually found out it was not the double but Math.Round that was behaving differently than I had anticipated.
Read my new publication to find out what was actually happening: