# Taking the square root of the square of a number

Variable set to a real value

I'm wondering why these examples

a = -4.3;

Sqrt@a²
Sqrt@(a^2)
Sqrt[a^2]
Sqrt[a²]


each return

4.3

the FullForm of which is

%//FullForm


4.3

yet

Sqrt@a^2


returns the complex number,

-4.3 + 0. I

the FullForm of which is

%//FullForm


Complex[-4.299999999999999,0.]

Why is the last case output as a complex number (with a slightly different real component)?

With an unbound variable

ClearAll[b]

Sqrt@b²
Sqrt@(b^2)
Sqrt[b^2]
Sqrt[b²]


each of the above returns the FullForm of which is

Power[Power[b,2],Rational[1,2]]

yet

Sqrt@b^2


returns

b

the FullForm of which is

%//FullForm


b

Why is the last case treated differently?

They return different answers because they're actually different expressions that have the same value, but of course that equality isn't going to be preserved when working with inexact numbers like 4.3. In particular, the first four expressions are all interpreted by Mathematica as

Sqrt[(-4.3)^2]


while the last expression is interpreted as

Sqrt[-4.3]^2


Obviously, (-4.3)^2 == 4.3^2 == 18.49, which is a positive number, so the square root is just a real number. On the other hand, Sqrt[-4.3] is going to be imaginary, so an imaginary part can creep into the mix, and it's not going to go away unless you use Chop or something.

This is easy enough to verify if you clear the value of a, or use HoldForm.

• @David, Pillsy: They're not actually algebraically equivalent. You don't believe that $\sqrt{b^2}=b$, do you? (What if $b<0$?)
– user484
Dec 8 '13 at 4:26
• I think I understand that. (Although I failed to note its relevance to my question when I first raised it.) The inverse of the n^2 function is not a function because there are two outputs for the square root of n>0. But in order for Sqrt to be used as a function we ignore the negative root, which is a bit of slight of hand to keep things nice and pretty. Maybe Mathematica does this slight of hand when it returns b as the result of Sqrt[b]^2. Can you shed light on this? Dec 8 '13 at 4:42
• @RahulNarain You're right, that was terribly worded. I'll fix it. Dec 8 '13 at 13:35

Because it's performing the square root first? Same answer with:

(Sqrt@a)^2
`

With $a = -4.3$, the results of $\sqrt{a^2}$ and $(\sqrt a)^2$ are not "slightly different".

$\sqrt{a^2}$ is a positive number.

$(\sqrt a)^2$ is a negative number close to $a$.

For any $b$, $(\sqrt b)^2$ is always equal to $b$, because $\sqrt b$ is defined to be a number whose square is $b$. Multiple roots, branch cuts, and the like don't make a difference; any choice must still square to $b$. Of course, if you're working in machine precision, it may not be exactly equal, but it will always be close, and never of the opposite sign.

On the other hand, $\sqrt{b^2}$ is not always equal to $b$. This is where the issue of multiple roots comes in: $b^2 = (-b)^2$, so $\sqrt{b^2}$ must be the same as $\sqrt{(-b)^2}$, and it can't be both $b$ and $-b$. So in general, all you can say is that $\sqrt{b^2}$ is either $b$ or $-b$ (the usual choice is to pick the one with positive real part).