Is a negative number squared negative? [duplicate]

Here's the issue that the other comments have been missing:

$-3^2$ does not mean "the square of negative three". The exponent takes priority over the negative: it means "the negative of $3^2$". If you want to say "the square of negative three" you write $(-3)^2$. (This also explains the issues with your programming languages - the ones that say $-9$ write it without the function notation doing the grouping for you, so the negative is applied after.)


It is not an opinion but a convention (accepted in all the world as far as I know) :

$$ -3^2=(-1)\cdot 3^2= (-1) \cdot 9 = -9 $$


$ -3^2 = -9 $ now, if you have parenthesis, like this:
$(-3)^2$ , then the answer will be $ 9 $.


IMO it helps a lot to understand how syntax of programming languages, and in a less straighforward way also maths notation, always correspends to a tree data structure. For instance, $f(g(x), h(y,z))$ is really a character-string encoding for something like $$ \begin{matrix} & & f & & \\& \nearrow & & \nwarrow & \\ g & & & & h \\ \uparrow & & & \nearrow & \uparrow \\ x & & y & & z \end{matrix} $$ The term $-3^2$, or the Python expression -3**2, means $$ \begin{matrix} & & -\square\quad & & \\ & & \uparrow & & \\ & & ** & & \\& \nearrow & & \nwarrow & \\ 3 & & & & 2 \end{matrix} $$ It does not mean $$ \begin{matrix} & & ** & & \\& \nearrow & & \nwarrow & \\ -\square & & & & 2 \\ \uparrow\!\!\!\!\! & & & & \\ 3 \!\!\!\!\! & & & & \end{matrix} $$ Why not? Well, these are just the conventions for how expressions are parsed: exponentiation binds more tightly than negation (which is, kinda reasonably, on the same level as addition).

OTOH, if you write in C# Math.pow(-3, 2), then this clearly is parsed as $$ \begin{matrix} & & \mathrm{pow} & & \\& \nearrow & & \nwarrow & \\ -3 & & & & 2 \end{matrix} $$ which is a different calculation and gives the result $9$. To express $-3^2$ in C#, use - Math.pow(3,2).

In programming languages, the parsing rules are generally these:

  • Parentheses group a subtree together, no matter what happens around them. Function application is typically connected to parenthesis, so this also binds tighly.
  • Commata always separate independent subtrees. Hence the -3 in pow(-3,2) is independent of the 2 and the pow function.
  • All other infix operators, like + and **, have some predefined fixity. For instance, in C and C++ the operator-precendence hierarchy includes the following:

    1. <, <=, >, >=
    2. <<, >>
    3. +, -
    4. *, /, %

    so when the expression pow(0+(-1)*3, 2) is encountered, the parser first splits it up at the comma, then at the +, then at the *, before considering the inner parenthesis.
    But in languages with an exponentiation operator, this should, as in maths notation, have a higher fixity than the other operators.

These parsing rules may subtly vary between different programming languages, but at least for a single language they must always be well-specified.

Alas, in maths it's often not so clear-cut – for some expressions it is indeed up to interpretation what they mean! For instance, does $\sin x^2$ mean $(\sin x)^2$ or rather $\sin(x^2)$? IMO it should be the former (because function application binds tightly), but I think the majority of mathematicians and scientist don't agree, and hence the completely ridiculous notation $\sin^2 x$ is used for that.

Oh well...