How is logarithmic differentiation of possibly negative functions justified? For example, take the common example $\frac{d}{dx}(\cos x) ^{\sin x}$. The usual method for this is
$$
y = (\cos x) ^{\sin x}\\
\ln y = \sin x \ln \cos x\\
\frac{d}{dx}\ln y = \frac{d}{dx} \sin x \ln \cos x\\
\frac{1}{y} \frac{dy}{dx} = \cos x \ln \cos x + \sin x \frac{1}{\cos x} \sin x\\
\frac{dy}{dx} = (\cos x) ^{\sin x} \left( \cos x \ln \cos x + \sin x \tan x \right)
$$
Now, of course, $\ln \cos x$ isn't valid for all $x$. Does it mean that wherever $\ln \cos x$ is undefined, the derivative does not exist? Or am I incorrectly pre-assuming that I can take the natural logarithm of both sides in the first place?
 A: Downvoters, could you please comment on the vote? If I made a mistake or the answer is not helpful, I'd like to learn from it and improve my future answers. I am trying to provide a bit more clarity for anyone who might still be confused by the comments.
As Hans Lundmark points out in his comment, you can consider $ln|f(x)|$, which works for negative and positive values of any function $f(x)$. Where $f(x) < 0$, (the case we are concerned about), $ln|f(x)| = ln(-f(x))$ and the derivative $\frac{d}{dx} ln(-f(x)) = \frac{1}{-f(x)} (-f'(x)) = \frac{f'(x)}{f(x)}$, which is the same as the derivative of $ln(f(x))$, but just also works for negative values of $f(x)$. You can think of logarithmic differentiation as taking the logarithm of the absolute value, and then differentiating.
What about when $f(x) = 0$? The function $ln(f(x))$ wouldn't be defined, but the derivative $\frac{f'(x)}{f(x)}$ wouldn't be defined either.
The other (perhaps more rigorous) answer involves properties of complex numbers. See the accepted answer here: Why does logarithmic differentiation work even if logs are not defined for negative numbers?
