I have a list of numbers {$x_i$} and I want to shift them (add a constant $\delta$) so that they are as close as possible to integer numbers in the sense that the summed distance to integer numbers gets minimal.
$D=\sum |y_i - round(y_i)|$ minimal with $y_i=x_i+\delta$
Possible solutions:
1) Brute force: Just sample the interval [0, 1] for $\delta$ with a small enough step size. This sounds rather inefficient.
2) Use any generic minimizer (bracketing, ..) to find a local minimum hoping it's also the global one.
So my questions are: Is there a more efficient method (maybe an explicit formula)? Is there at most one local minimum for $\delta$ in [0, 1]? Or is brute force the only practical way in the general case?
For better understanding an example:
- [-3.5, -1.5, 2.5, 7.5] would benefit from $\delta=0.5$
- [0, 0.2, 0.4, 0.6, 0.8] has minimal D for any $\delta$