Why isn't $f(x) = x\cos\frac{\pi}{x}$ differentiable at $x=0$, and how do we foresee it? Consider $$f(x)=\begin{cases}
x\cos\frac{\pi}{x} & \text{for} \ x\ne0 \\ 
0 & \text{for} \ x=0.
\end{cases}
$$
Its difference quotient $\frac{\Delta\left(f(x)\right)}{\Delta(x)}$ approaches $\cos\frac{\pi}{h}$ as $x$ gets closer to $0$, and thus $f$ is not differentiable in the origin because $\lim\limits_{h\to0}\cos\frac{\pi}{h}$ does not exist. This is the plot of $y=x \cos \frac{\pi}{x}$:

But here's how my book goes on:

Examining the figure we can foresee that the tangent line in a generic point $P$ of the graph doesn't tend to any limiting position as $P$ tends to the origin along the curve itself. One may think this happens because the graph of the function completes infinitely many oscillations in any neighbourhood of the origin. In fact, no: indeed the function thus defined: $$g(x)=\begin{cases}
x^2\cos\frac{\pi}{x} & \text{for} \ x\ne0 \\ 
0 & \text{for} \ x=0
\end{cases}
$$
  has a graph that completes infinitely many oscillations in any neighbourhood of the origin, but, as you can verify, it is differentiable at $x=0$ and we have $g'(0)=0$.

This is the plot of $y=x^2 \cos \frac{\pi}{x}$:

So, I have two questions related to what I quoted from the book: how do we foresee the non-differentiability of $f$, given that, correctly, the infinitude of the oscillations is not an argument for it? And then, why isn't $f$ differentiable, instead of $g$?
I shall emphasise that I know that, simply, the limit as $h\to 0$ of the difference ratio of $f$ doesn't exist, while that of $g$ does, but I've been wondering about an other kind of reason after reading that excerpt. Or is my book wrong in mentioning other reasons?
 A: Normally the rules of differentiation show that any elementary function is continuous and differentiable wherever it is defined. The problems arise when the function's formula is undefined at some point and then we need to check the differentiability via the definition of derivative. Another case is when we define functions via multiple formulas in different parts of the domain. Then we need to check for existence of derivative at the boundary points.
The point which your book has highlighted is very important but perhaps been left out by other answers. It says that most common functions are differentiable except for exceptional points. Thus $f(x) = x\cos (\pi/x)$ is an example where $f$ is differentiable at all $x \neq 0$ and $$f'(x) = \cos (\pi/x) + \frac{\pi}{x}\sin(\pi/x)$$ When we define $f(0) = 0$ then we get continuity. But $f$ is still not differentiable at $0$.
Now your book mentions a very deep idea. Normally people try to look at the formula for $f'(x)$ and try to see if it tends to a particular value as $x \to 0$. In the above example the limit of $f'(x)$ does not exist and hence we conclude that $f$ is not differentiable at $0$.
The book says that this is not the right way to go and gives another example which sort of is a failure case for above technique. The example is $f(x) = x^{2}\cos (\pi/x), f(0) = 0$. We have $$f'(x) = 2x\cos(\pi/x) + \pi\sin(\pi/x)$$ and again we see that $f'(x)$ does not tend to a limit as $x \to 0$. And hence we conclude that $f'(0)$ does not exist. This is wrong.
We have the following theorem:
Theorem: If $f$ is continuous at $a$ and $\lim_{x \to a}f'(x) = L$ then $f'(a) = L$.
But the converse of the theorem does not hold. Thus if $f'(a) = L$ it does not mean that $\lim_{x \to a}f'(x)$ exists necessarily.
Thus the method of using limit of $f'(x)$ works only when the limit exists. Better not to try this approach and rather use the definition of derivative. I must say that your book has done a great job to highlight this fact about limit of derivative at a point and its relation to existence of derivative at that point (although not in the formal manner which I have given in my answer).
A: The enveloping curves define differentiability. During infinite oscillations first curve tangent cannot decide between two slopes. But for the second, slopes are both equal to zero, makes it differentiable with its coinciding slope of tangent.
A: One way to "foresee" it is that there are clearly two lines in the first image you posted that serve as an envelope to $f(x)$. These two lines crossing at the origin make it impossible to approximate $f$ near $x=0$ as a linear function. This is the criterion of differentiability you want to keep in mind when trying to make this kind of judgement.
On the other hand, in the second image, the envelope is two parabolas touching at the origin. Since the parabolas are tangent at the origin, they force $y=0\cdot x$ to be the only way to approximate $f(x)$ as a linear function near $x=0$.
In the end, the criterion for differentiability of functions squeezed inside an envelope $$e_-(x)\leq f(x)\leq e_+(x)$$ is: no matter how wildly $f(x)$ oscillates inside the envelope, $f(x)$ will be differentiable at $x=0$ if (i) the envelopes touch each other: $$e_-(0)=e_+(0)$$ that is, they do squeeze $f(x)$ appropriately; and (ii) they are both differentiable with equal derivatives: $${e'}_{\!-}(0)={e'}_{\!+}(0)$$ thus forcing $f(x)$ to be differentiable with the same derivative.
A: The book is giving a WARNING: "One $may$ think this (i.e. non-differentiable at $0$) happens because ( of the oscillations)...." and then shows that the same type of pattern of oscillations  also exists for $g(x)=x^2\cos (\pi /x),$ so the occurrence of such an oscillatory pattern is insufficient to  determine whether the function is differentiable at $0$.
Perhaps a different phrasing would have made this clearer. And I think it would have  been more emphatic to have said that the oscillations in $g(x)$ near  $0$ are large enough that $\lim_{x\to 0}g'(x)$ does not exist, but $g'(0)$ does exist, so the non-existence of $\lim_{x\to 0}f'(x)$ is, by itself, insufficient to determine whether $f'(0)$ exists.  
