float point works differently on different devices, there is no accuracy guarantees, so you should expect bad results when numbers are too big or too small.
as for your example when dot(MD,MD) is close to 0 sqrt moves it away from it thus stabilising further operation with this number. in the division this is especially critical.