How does a continuous sum turn to an integral? Suppose we have wave packets with different wave numbers. They're in the form of $y=A\cos(kx)$. We can add them together so that
$$
y(x)=\sum A_i \cos k_i x
$$
for a set of discrete wave numbers.
Now ff we have a continuous set of them, the sum becomes an integral:
$$
y(x)= \int A(k) \cos kx \, \mathrm{d}k
$$
But the sum seems to be infinite since for example at point x=0, an infinite number of wave peaks are summing.
I don't really undrestand how does it work. Why is it like this?
 A: I think your integral should be with respect to $k$. Then, the integral is finite because each wave contributes with an infinitesimally small amplitude $A_i\mapsto A(k)dk$.
A: Its hidden in the notation. Just because you've called both quantities ($A_i$ and $A(k)$) by the same name $A$, doesn't mean they are the same. In the first, it is the amplitude of a particular wave number $k_i$, and in the second, $A(k)$ is an amplitude density associated with the neighbourhood $dk$ about $k$. 
A similar problem also shows up for example in discrete/continuous quantum mechanics, where a probability becomes a probability density. It no longer makes sense to ask, for a wave $y(x)$, what is the amplitude associated with the wave vector $k$, since it is zero. However, the total amplitude of wave vectors in the small range $[k, k+dk)$ is $A(k)dk$. Hence, $A(k)$ is a density, and $A_i$ is not. 
The Amplitude for each $k$ is infinitesimal, but the density is finite, so the integral is finite.
A: Matematically, the expression gives for each $x$ the value of the area under a curve. If there was not the factor $A(k)$, the area would not be defined, because the cosine oscillates between $-1$ and $1$ for all $k$'s.
Choosing $A(k)$ like a bell shaped curve for example, the product will go infinitely close to zero below and above an interval, allowing a defined area as a result.
When $x \approx 0$, $cos(kx) \approx 1$ for a range of $k$'s, and the integral tends to the area under $A(k)$ alone.
When $x>>0$ or $x<<0$, $\;cos(kx)$ oscillates quickly between -1 and 1, and the area tends to zero, because the product $A(k)cos(kx)dk$ will be a sum of negative and positive areas cancelling out the sum.
