Improved algorithm for floatToGrid