I was considering the following function $$f_a(x)=\sum_{n=0}^{\lfloor x\rfloor}\sin^2(an)$$ and, as expected, $f_a(x)\approx x/2$ for every $a$ (except $2\pi$ and similar). This is because the function $\sin^2(ax)$ is "on average" equal to $1/2$. That is: we are adding $x$ numbers all close to $1/2$, and if one of them is bigger (than $1/2$) then not much later we are going to find another that is smaller (than $1/2$), cancelling the effect. At first I thought that the behaviour of $f_a$ would depend on how good of an approximation is $a$ to a rational multiple of $\pi$ (problems of convergence and similar always take this into consideration), but it is not the case.
The problem begins when one considers (I found this before the $f_a$) the functions $$g_a(x)=\sum_{n=0}^{\lfloor x\rfloor}\sin(an)$$ I'm puzzled by these: they are (almost always) approximately equal to $A\sin^2\left(Bx\right)$, where $A$ and $B$ are, I think, constants (or almost constant) which depend on $a$. First of all, why does this happen? If $\sin^2(ax)$ is, on average equal to $1/2$, then $\sin(an)$ is equal to $0$ on average, so we should oberve $g_a\approx0$ always. Instead, $g_a$ is either positive (and oscillating) or negative (also oscillating), except for very few values where it becomes negative (psitive) but very small. So they are, in a sense, "very positive" or "very negative". Either way, very not zero.
Maybe it has to do with the fact that $$\frac{1}{x}\int_0^x\sin^2(at)\,dt$$ converges to $1/2$ but oscillating and $$\frac{1}{x}\int_0^x\sin(at)\,dt$$ converges to $0$ but is always postivie or negative so that, while it is $0$ on average, it is more positive somehow. Can you expalin this phenomenon?
Also, this is not the whole story: there are some values of $a$ (for example $a=2.8$) for which $g_a$ is "made" of two sine waves (like a standing wave), but this time the "very positive or very negative" doesn't show up. For some other values ($a=23$), $g_a$ is made up of three sine waves. I suspect this can be explained with the rational approximation thing.
Note: (you may want to graph $g_a$ times a big constant like 10 and zoom out to see the various sine waves)
Thanks!!
Edit: We have the following identity: $$\sin(\varphi)+\sin(\varphi+\alpha)+\sin(\varphi+2\alpha)+\dots+\sin(\varphi+n\alpha)=$$ $$=\frac{\sin\left(\frac{(n+1)\alpha}{2}\right)\sin\left(\frac{n\alpha}{2}\right)}{\sin\left(\frac{\alpha}{2}\right)}$$ So making $\varphi=0$ and $\alpha=a$ we have a precise expression $$g_a(x)=\frac{\sin\left(\frac{(\lfloor x\rfloor+1) a}{2}\right)\sin\left(\frac{\lfloor x\rfloor a}{2}\right)}{\sin\left(\frac{a}{2}\right)}$$
Which looks similar enough to $A\sin^2(Bx)$.
This does not solve the "$\sin$'s average is $0$ so $g_a$ should be $0$". Also, and I find this very curious, if we substitute $\lfloor x\rfloor$ for $x$ in the formula we get a completely different function, and the "made up of various sine waves for some choices of $a$" is far from clear from the formula.
I'm guessing its a "how synched" the two waves in the numerator are: if they are very very similar then their product is very similar to $\sin^2$. This explains the "very positive" or "very negative", but no more, because the periods are $a/2$ for both waves.
Another observation: if we use $\cos$ instead, the opposite phenomenon occurs. For most values of $a$, the sum is "made up" of various waves
The function has a closed form, found with a sum formula similar to the sine-sum formula you cite $$\sum_{n=0}^k\cos(k \varphi) = \frac{\sin(k\varphi/2)\sin((k+1)\varphi/2)}{\sin(\varphi/2)}$$ thus $$ f_a(x) = \sum_{n=0}^{\lfloor x \rfloor}\sin^2(2an) = \sum_{n=0}^{\lfloor x \rfloor}\frac12 - \sum_{n=0}^{\lfloor x \rfloor}\frac12 \cos(2an)=\\$$ $$ = \frac{1+\lfloor x \rfloor}2 - \frac{\sin(\lfloor x \rfloor a)\sin\left((\lfloor x \rfloor + 1)a\right)}{\sin(a)} $$ which indeed satisfies $$ f_a(x) = \frac x 2 + O(1) $$