c++ - Find closest number from uniform grid -
I have an integer positive number I have written this function: It seems like it does not like how it looks at work. Can anybody suggest improvement? You can remove from I do not do this, because it will damage readability. n . For example, suppose that
n = 5 if we look at the polygon of
n , we look at these numbers (let's call it an N-grid) [ ... -15, -10, -5, 0, 5, 10, 15, ...] Now I need to write a function
F (n, n) , which is given an integer
N , showing the closest number from that n-grid. For,
F (n, 0) = 0 (for any N).
F (5, 4) = 5 ,
F (5, 7) = 5 ,
F (5, 8) = 10 ,
F (5, -13) = -15 and so on.
int constx = ((:: stomach (n) + (n / 2)) / n) * n; If (N> gt; {return x; } Other {return -x; }
if
(n / abs ( N)) By multiplying x by and without saving it in
x , the value calculated also immeadiatelly.
Comments
Post a Comment