I'm not entirely sure I understand when I need to calculate a derivative using the definition and when I can do it normally. The following two examples confused me:
$$ g(x) = \begin{cases} x^2\cdot \sin(\frac {1}{x}) & x \neq 0 \\ 0 & x=0 \end{cases} $$
$$ f(x) = \begin{cases} e^{\frac {-1}{x}} & x > 0 \\ -x^2 & x\leq 0 \end{cases} $$
I understand that I can differentiate normally for any $x$ that's not 0 (in both of these examples). I'm confused because I saw an example with $f(x)$ in which they calculated the $f'(x)$ by differentiating:
$$ f'(x) = \begin{cases} \frac {1}{x^2}\cdot e^{\frac {-1}{x}} & x > 0 \\ -2x & x\leq0 \end{cases} $$
and then they calculated $f'(0)$ not using the definition but by $ \lim_{x\to0^-} f'(x)$ $\lim_{x\to0^+} f'(x)$
For $g(x)$ though I know that $g'(0)$ exists (using definition) but
$$g'(x) = \begin{cases} 2x\cdot \sin(\frac {1}{x})-\cos(\frac {1}{x}) & x \ne 0 \\ 0 & x=0 \end{cases}$$
and you can't calculate $\lim_{x\to0^-} g'(x)$ or $\lim_{x\to0^+} g'(x)$ since $\lim_{x\to0^{+/-}} 2x\cdot \sin(\frac {1}{x})-\cos(\frac {1}{x}) $ doesn't exist.
So what's the difference between these two? When can I just differentiate normally like in the first example ($f(x)$) and when do I have to use the definition like in the second example ($g(x)$)?
I'd appreciate the help.
Edit: When I"m referring to the definition I'm referring to the following:
$\lim_{h \rightarrow 0} \dfrac{f(x+h) - f(x)}{h}$
Given a function $f(x)$ and a point $x_0$, it's possible to take $f'(x_0)$ by differentiating normally only if $\lim\limits_{x \rightarrow x_0} f'(x)$ exists. However, this is not the case for the $g(x)$ that you gave.
As $x \rightarrow 0$, $-\cos\left(\frac{1}{x}\right)$ oscillates, and, in fact, in any small neighbourhood of $0$, there are points $y$ and $z$ such that $-\cos\left(\frac{1}{y}\right) = -1$ and $-\cos\left(\frac{1}{z}\right) = 1$. Thus, the limit as $x \rightarrow 0$ of this part of the function $g'(x)$ gotten by normal differentiation does not exist, and so the limit of the whole function does not exist, and therefore, it cannot be used as the derivative at that point. However, using the limit definition of the derivative, it is possible to find that $\frac{dg}{dx}(0) = 0$.