c++ - Find closest number from uniform grid -


I have an integer positive number 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.

I have written this function:

  int constx = ((:: stomach (n) + (n / 2)) / n) * n; If (N> gt; {return x; } Other {return -x; }   

It seems like it does not like how it looks at work. Can anybody suggest improvement?

You can remove from if (n / abs ( N)) By multiplying x by and without saving it in x , the value calculated also immeadiatelly.

I do not do this, because it will damage readability.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -