"Integer average" of two integer numbers Suppose two arbitrary integer numbers $a$ and $b$. I'm looking for some function $f(a,b)$ with the following properties:


*

*$f(a,b)\in\mathbb{Z}$.

*$f(a,a)=a$.

*$f(a,b)=f(b,a)$.

*$\min\{a,b\}< f(a,b)< \max\{a,b\}$, accepting "$\leq$" if $a$ and $b$ are consecutive ($a=b\pm 1$).


Any example of such a function? 
Improvement: Any solution avoiding "floor/ceiling-like" functions?

How about
$$
\frac{a + b + ((a + b)\ \text{mod}\ 2)}{2}
$$
This works because $a + b + ((a + b)\ \text{mod}\ 2)$ is guaranteed to be a multiple of 2. Note that you can also use $(a - b)\ \text{mod}\ 2$, if you prefer, with no change in behavior.
Both of the modulo expressions are more straight-forward descriptions (IMO) of what Peter was doing in his answer where he used $−sin^2(π(a+b)/2)$, except that he rounds down where I round up, and of course my solution remains completely in the integer domain. (I'm not familiar with any implementation of sine that is integer specific.)
A more complex answer avoiding floor and round that includes the concept of scientific rounding in user198082's answer is:
$$
\frac{a + b + ((a + b)\ \text{mod}\ 4)((a + b)\ \text{mod}\ 2) - 2((a + b)\ \text{mod}\ 2)}{2}
$$
Examining the four cases:


*

*If a + b is a multiple of 4, this is just dividing their sum by 2,
since all modulos are zero.

*If a + b is a multiple of 2, this is still just dividing their sum by 2, since modulo 2 we get zero and that is multiplied by the modulo 4 (which is 2).

*If a + b modulo 4 is 1 (e.g., a = 2, b = 3), then we want to round down to the even result (2), which happens because the product of the modulo 4 and modulo 2 test is just 1, and the product of 2 and the modulo 2 test is -2, resulting in a net of -1.

*If a + b modulo 4 is 3 (e.g., a = 1, b = 2), then we want to round up to the even result (again 2), which happens because the product of the module 4 and module 2 test is now 3, and the product of 2 and the modulo 2 test is still -2, resulting in a net of +1.

