Consider the function $\psi: \Bbb{R} \to \Bbb{R}$ defined by $\psi(x) = e^{-|x|}$. Then, $\psi$ is clearly differentiable everywhere on $\Bbb{R}$ except at the origin (since the left and right hand derivatives do not agree).
Now, this function (ignoring some physical constants) came up as part of a wavefunction in a Quantum mechanics calculation. The question was to calculate the expectation value of the momentum squared. So, we'd have to compute \begin{align} \left \langle P^2 \right \rangle &= \int_{\Bbb{R}} \psi^*(x) \cdot \left(-i \hbar \dfrac{d}{dx} \right)^2 \psi(x) \, dx \end{align} Or, ignoring all the constants (and complex conjugation, since everything here is real), we have to compute \begin{align} \int_{\Bbb{R}} \psi(x) \cdot \psi''(x) \, dx \end{align} Of course, strictly speaking this is not proper, because $\psi$ is not even differentiable at the origin, let alone twice differentiable. As a result of this, I get an inconsistency in the integral calculation depending on how I do it.
The first method is to forget about the discontinuity completely, and compute \begin{align} \int_{- \infty}^0 \psi(x) \cdot \psi''(x) \, dx + \int_0^{\infty} \psi(x) \cdot \psi''(x) \, dx \end{align} (with the understanding that $\int_0^{\infty}$ means taking a limit of $\int_{\delta}^{R}$ as $\delta \to 0, R \to \infty$). By doing this computation, I found the answer to be $+1$.
If however I first perform integration by parts: \begin{align} \int_{\Bbb{R}} \psi(x) \cdot \psi''(x) \, dx = - \int_{\Bbb{R}} \left( \psi'(x) \right)^2 \, dx \end{align} (dropping the boundary term since it vanishes, and $\int_{\Bbb{R}}$ means the limit of $\int_{R_1}^0 + \int_0^{R_2}$ as $R_1 \to - \infty, R_2 \to \infty$), I find that the answer is $-1$.
So, I realize that this discrepancy is due to $\psi$ not being twice differentiable at the origin. However, based on physical reasons, it seems like we want the result to be $-1$, as in the second method's computation.
My question is: how do I properly interpret the integral to be calculated $\int_{\Bbb{R}} \psi(x) \cdot \psi''(x)\, dx$, when the function isn't smooth enough, and why is it that using integration by parts gives a "physically acceptable" answer... basically how do I deal with such a weird situation in a mathematically rigorous/precise manner/ what is the right approach?
Edit:
I also just noticed that the difference in the two answers is $(1)-(-1) = 2$, is exactly the discontinuity in the derivative: \begin{align} \lim_{x \to 0^+} \psi'(x) - \lim_{x \to 0^-} \psi'(x) = 1 - (-1) = 2 \end{align} Does this have anything to do with the discrepancy, or is this just a coincidence?
Your edit is basically the reason; the first derivative is discontinuous, so the second derivative has a dirac mass of weight 2.
Note that $\psi=e^{-|x|}$ is not even once differentiable at the origin. So you should compute weak or distributional derivatives instead. The first derivative is $$ \psi' = -(\operatorname{sgn} x)e^{-|x|} $$ and there are no issues in integrating $(\psi')^2$ which is even continuous. But the second derivative involves by product rule the derivative of $\operatorname{sgn} x$ which is $2\delta_0$,
$$\psi'' = e^{-|x|}(1-2\delta_0) = e^{-|x|}-2\delta_0$$ and this is the extra $-2$ needed to turn that $+1$ into $-1$: $$ \int\psi \psi'' = \int_{\mathbb R} e^{-2|x|} dx - 2\int_{\mathbb R} e^{-|x|} \delta_0(x) dx = +1 - 2 = -1.$$