Take the 2-minute tour ×  I have a one dimensional gird. its
spacing is a floating point. I have a point with floating point
coordinate as well. I need to find its distance to the closest
grid point. For example:   0        0.1       0.2       0.3
0.4       0.5  The result would be -0.02 since the closest point
is behind it. However if it was  -1       -0.8      -0.6
-0.4      -0.2        0  The result will be 0.06. As you can see
its in floating point and can be negative. I tried the
following:  float spacing = ...; float point = ...;  while(point
>= spacing) point -= spacing; while(point < 0) point += spacing;
if(std::abs(point - spacing) < point) point -= spacing;  It
works, but I'm sure there is a way without loops  share|improve
this question Is the spacing linear? –  GWW Dec 1 '11 at 18:46
In his examples it is linear. –  GWW Dec 1 '11 at 18:56
@MooingDuck: its linear, just not constant (its parameter) – 
Dani Dec 1 '11 at 19:00 @GWW: its linear –  Dani Dec 1 '11 at
19:00 add comment  5 Answ