Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;


each return


the FullForm of which is





returns the complex number,

-4.3 + 0. I

the FullForm of which is



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

With an unbound variable



each of the above returns


the FullForm of which is






the FullForm of which is



Why is the last case treated differently?

share|improve this question
up vote 7 down vote accepted

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


while the last expression is interpreted as


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.

share|improve this answer
Your answer addresses the bound case, but what about the unbound case? – DavidC Dec 8 '13 at 1:43
@David, Pillsy: They're not actually algebraically equivalent. You don't believe that $\sqrt{b^2}=b$, do you? (What if $b<0$?) – Rahul 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? – DavidC Dec 8 '13 at 4:42
@RahulNarain You're right, that was terribly worded. I'll fix it. – Pillsy Dec 8 '13 at 13:35

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

share|improve this answer

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).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.