A while ago, a friend asked me "for which functions $f:\Bbb{R}\to\Bbb{R}$ does the sum $\sum_{x\in\Bbb{R}}f(x)$ converge?", and a few days ago I completed a sketch of a proof that the sum converges iff there exists a countable set $X\subset\Bbb{R}$ (hereafter, '$X$') such that...
$$(1)\qquad\sum_{x\in X} f(x)\in\Bbb{R}$$
...and...
$$(2)\qquad\sum_{x\in X^\complement} f(x)=0$$
(Note: the sole discrepancy between this and the answer provided by others is that I have not made the assertion that $\sum_{x\in X^\complement}f(x)=0\iff\forall x\in X^\complement.f(x)=0$)
Of course, this isn't very informative; what would an example of such a function even be?
To be a little bit more concrete, I considered two cases which, far as I can tell, exhaust the possibilities for such functions.
The first case consists of functions which evaluate to $0$ for all points outside of a countable subset of the domain, for example, suppose $X=\Bbb{N}$...
$$f(x)=\begin{cases}\frac{1}{x^2}&x\in\Bbb{N}\\0&\text{otherwise}\end{cases}$$
...for which...
$$\sum_{x\in\mathbb{R}}f(x)=\sum_{n=1}^\infty \frac{1}{n^2}=\frac{\pi^2}{6}$$
Such functions trivially satisfy $(2)$, because for all $x$ in the complement of $X$, $f(x)=0$.
The second potential case consists of functions whose sum over $X$ likewise converges, but which are not necessarily $0$ for all points in $X^\complement$.
Now, when I first wrote this question, I had used a nonstandard interpretation of the integral to show that cancellation of partial sums is possible. However, as it has been pointed out to me, the same result is not readily obtained using only the familiar tools of real analysis. As an alternative to nonstandard methods, I propose the following:
i. If there exists a countable set $X\subset \Bbb{R}$ such that $\sum_{x\in X}f(x)\in\Bbb{R}$ and $\forall x\in X^\complement.f(x)=0$ then $\sum_{x\in\Bbb{R}}f(x)\in\Bbb{R}$ (the sum converges).
ii. If there are disjoint sets $Y_1,Y_2\subset\Bbb{R}$ such that $Y_1\cup Y_2=X^\complement$ and $\sum_{x\in Y_1}f(x)=-\sum_{x\in Y_2}f(x)$ then $\sum_{x\in\Bbb{R}}f(x)\in\Bbb{R}$ (the sum converges).
The first of these is well known in real analysis, and provides an instance of conditions $(1)$ and $(2)$. This means that the convergence criteria I have established are at least inclusive of the usual ones.
In order to assess the second proposition using only standard methods, I would like to introduce a new function. For every real interval $p$, let $I(p)$ be the interval such that $\inf I(p)=0$, $\sup I(p)=\sup p-\inf p$, $\inf I(p)\in I(p)\iff \inf p\in p$, and $\sup I(p)\in I(p)\iff \sup p\in p$ - e.g. $I(x,y)=(0,y-x)$, $I[x,y)=[0,y-x)$, etc.
For every real interval $p$ let $I_p(x)=x-\inf p$.
Now, let $Y_1$ and $Y_2$ be disjoint intervals in $X^\complement$. If $I(Y_1)=I(Y_2)=Y_0$, then we may show that...
$$\sum_{x\in Y_1}f(x)+\sum_{x\in Y_2}f(x)=0$$
...if...
$$\forall x\in Y_0.(f\circ {I_{Y_1}}^{-1}+f\circ {I_{Y_2}}^{-1})(x)=0$$
For example, the sum...
$$\sum_{x\in\Bbb{R}}f(x)\quad:\quad f(x)=\begin{cases}1&x\in(-1,0]\\-1&x\in(0,1)\\0&\text{otherwise}\end{cases}$$
...converges to $1$ because for each $x\in I(-1,0)$, $(f\circ{I_{(-1,0)}}^{-1})(x))=1$ and $(f\circ{I_{(0,1)}}^{-1})(x))=-1$. Thus...
$$\sum_{x\in(-1,0)\cup(0,1)}f(x)=\sum_{x\in I(0,1)}f({I_{(-1,0)}}^{-1}(x))+f({I_{(0,1)}}^{-1}(x))=\sum_{x\in I(0,1)}0$$
...and...
$$\sum_{x\in\Bbb{R}}f(x)=\sum_{x\in\{0\}}f(x)+\sum_{x\in I(0,1)}f({I_{(-1,0)}}^{-1}(x))+f({I_{(0,1)}}^{-1}(x))+\sum_{x\in(-\infty,-1]\cup[1,\infty)}f(x)$$
$$=\sum_{x\in\{0\}}1+\sum_{x\in I(0,1)}0+\sum_{x\in(-\infty,-1]\cup[1,\infty)}0=1+0+0=1$$
Of course, what we really want is a non-piecewise function definable in $(\Bbb{R},+,\times,\cos,\ldots)$ [with iterates thereof].
Anyway, I'm at a loss as to how I should proceed beyond this point. Are there additional cases besides these? How should I go about looking for functions whose sum converges?
Note: It might seem unusual that there are 'neatly' expressible functions which are nowhere continuous, but this is indeed the case. Consider the function...
$$f(x)=\sum_{n=0}^\infty \frac{\text{sgn}\left(\cos(nx)\right)}{k}$$
Edit: For the sake of evaluating the sum, it can be assumed that every subset of $\Bbb{R}$ inherits the natural ordering from $\Bbb{R}$. Additionally, it can be assumed that if $A\cup B=C$ for disjoint sets $A$ and $B$, then...
$$\sum_{x\in C}f(x)=\sum_{x\in A\cup B}f(x)=\sum_{x\in A}f(x)+\sum_{x\in B}f(x)$$
Edit: I made a mistake in the original post. I had said that the sum cannot converge if the function is differentiable. After going back over my work, I found that the sum over a particular subset does not converge if the function is differintiable everywhere in that subset and there is no point $x$ in the subset such that $f'(x)=0$. The converse does not hold.
Edit: it should be possible to extend the above method by considering the reverse order of an interval in addition to the standard ordering. I haven't quite figured out how to do this yet, though.
Edit: Added proof verification tag since I'm unsure of my attempt to frame cancellation in a purely standard context. I would appreciate it if someone could verify it for me.
A sum over an uncountable set, $\sum_{x \in A} f(x)$, can't converge unless there are only countably many $x \in A$ for which $f(x)\ne 0$. Note that the notation $\sum_{x \in A}$ does not specify any order relation on $A$, so there is no possibility of some sort of "conditional convergence" where positive and negative values cancel out.