Trigonometric Functions And Modular Arithmetic I was playing around on Wolfram Alpha and plugging in functions such as 
$$f(x)=2^{x}\bmod{x}$$
to see what some simple graphs might look like. Wolfram also returned some very interesting things such a series expansion for the expression
$$f(x) = 2^{x} \bmod{x} = \frac{x}{2}- \frac{x}{\pi} \sum_{k=1}^{\infty} \frac{1}{k}\sin{\left(\frac{2^{x+1}k\pi}{x}\right)}$$
for $x\in \Bbb{R} \setminus \Bbb{Z}$ and 
$$ 2^x \bmod{x} = \frac{x}{2}-\frac{1}{2}\sum_{k=1}^{x-1} \cot{\left(\frac{k \pi}{x}\right)} \sin{\left(\frac{2^{x+1}k \pi}{x}\right)}$$
for $x\in\Bbb{Z_{+}}$ and $\frac{2^x}{x} \notin \Bbb{Z}$.This expression seems to come out of nowhere. So my questions are:


*

*How does one derive such a series for such a seemingly simple expression?

*Is there any literature anyone could suggest on writing functions modulo n in series form? 

 A: What's happening here is that the mod is being taken in the "add or subtract $x$ until the value is within a certain range" sense, and then a Fourier series is being applied from there.
We can easily, using the Finite Fourier Transform, show that, for $x\not\in \mathbb{Z}$,
$$
x-\lfloor x\rfloor = \frac12 -\frac1\pi \sum_{k=1}^\infty \frac1k \sin\left(k\pi x\right)
$$
and from here, it's easy to get to what we need, as
$$
a\text{ mod } b = a - b\lfloor a/b\rfloor
$$
which gives
$$
2^x\text{ mod } x = x\left(\frac12 - \frac1\pi \sum_{k=1}^\infty \frac1k\sin\left(k\pi\frac{2^x}x\right)\right)
$$
which matches the first expression that Wolfram Alpha gave you. When $x$ is an integer, however, we can compress the expression, because for any integer $n$ the $\sin$ function takes the same value at $k=n+mx$ for all integer $m$, and it turns out that we can express the result as described by the second expression.
Note that the case of $\frac{2^x}x\in\mathbb{Z}$ causes a problem, because in that situation, $x-\lfloor x\rfloor = 0$ whereas the Fourier series gives $\frac12$. But since the result there is always zero, it's no big deal.
