I've come across this function:
$ f(x) = (-2)^n, x\in\left [ n, n+1 \right ), n\in \mathbb{Z} $
and I was trying to calculate its integral going from 0 to infinity. Since you can't just take the antiderivative and plug the values (I think at least), I decided to use a different approach. I basically considered the different rectangles between the different sections of the graph and the x-axis. I then summed them all up in an alternating fashion, since the rectangles alternate between being under and above the x-axis and thus their area contributes negatively and positively, respectively, to the integral. So: $$ \int_{0}^{\infty} f(x)dx = \sum_{n=0}^{\infty } 2^{2n} - \sum_{n=0}^{\infty } 2^{2n+1} = \sum_{n=0}^{\infty } 2^{2n} - \sum_{n=0}^{\infty } 2 \cdot 2^{2n} = \sum_{n=0}^{\infty } 2^{2n} - 2 \cdot \sum_{n=0}^{\infty } 2^{2n} = -1 \cdot \sum_{n=0}^{\infty } 2^{2n} $$ Since $ \sum_{n=0}^{\infty } 2^{2n} $ diverges, then: $$ \int_{0}^{\infty} f(x)dx = - \infty $$ Is there any flaw in this line of reasoning?
Suppose we're working with an improper Riemann integral (seems to be in the spirit of the question), then the integral doesn't converge. If you look at $t$'s where $t$ even, then $\int_0^t f(x)$ goes to $- \infty$ . If you look at $t$'s where $t$ odd, then $\int_0^t f(x)$ goes to $+ \infty$ .
If we're working with a Lebesgue integral, the integral doesn't converge either because both the positive and negative parts go to infinity.
In general, you cannot "pair up" terms in an infinite sum like that because the result will depend on how you pair them up. For example, what is $\sum_{i=0}^\infty (-1)^i$?
+1 -1 +1 -1 +1 -1 ... = ?
(+1 -1) (+1 -1) (+1 -1) ... = 0
+1 (-1 +1) (-1 +1) (-1 +1) ... = 1
So, in particular, the first equals sign in your solution does not hold.