Why am I getting the wrong answer when I factor an $i$ out of the integrand?

I had difficulty understanding the previous answer so am offering an expanded version.

Taking your first step, you write $\sqrt{-x} = i\sqrt{x}$. Now try that with $x=-1$. It gives a contradiction, $$1 = \sqrt{1} = i \sqrt{-1} = i^2 = -1.$$

It is not really fixed if you use the alternative sign for $\sqrt{-1}$ because you obtain $$ 1 = \sqrt{1} = -i \sqrt{-1} = (-i) \times (-i) = -1 $$

Only if you take different signs for the imaginary part at each square root do you get the answer you want.

Underlying this is a general point about complex valued functions. By convention for real $ x \geqslant 0$, $\sqrt{x}$ is always taken to be the positive root. When $x < 0$ there is no natural convention and $\sqrt{x} $ could be either one of $\pm i\sqrt{-x}$. The difficulty arises because there cannot be a consistent choice for the root of a negative number that at the same time satisfies the desirable identity $\sqrt{xy} = \sqrt{x}\sqrt{y}$. That is because in complex analysis the square root $\sqrt{z}$ has a branch point (that is, it is badly behaved) at $z=0$ and it cannot be extended to a well behaved function across the whole complex plane.


Fundamentally, your error amounts to the following (mis)calculation:

$$1=\sqrt1=\sqrt{-(-1)}=i\sqrt{-1}=i^2\sqrt1=-\sqrt1=-1$$

It's just that the second minus sign doesn't appear in what you're doing until after the first one was converted to an $i$. I.e., you converted $\sqrt{-x}$ to $i\sqrt x$ before doing the integration, and only later substituted the lower limit $x=-1$.


If $x\in[-1,\,0)$ then $\Im\sqrt{x}=\sqrt{-x}$, so $\sqrt{-x}=\sqrt{x}/i=-i\sqrt{x}$.