Once invalid (#IND / #INF / #NAN) numbers have infested your simulation, it’s very difficult to get rid of it. It’s like a viral infection. The best way to avoid invalid floating point numbers are to prevent them happening in the first place.

What happened was that invalid floating point values were introduced while calculating the angle between two vectors. The acos method calculate the angle, and it’s domain is [-1, 1]. Due to rounding errors, the actual value passed to acos was slightly less or slightly above the domain, which resulted in an invalid number.

The way we caught the error was to modify the vector3d-class and insert breakpoints when the expression value != value is true. Only NAN and IND values behave like that.