Is -x^2 positive or negative?

So, is -x2 a positive, negative, or undefined quantity for real-valued x?  Ask any physicist or mathematician and they will say that it is a negative number for real valued x making things like: exp(-x2) between 0 and 1.  That is why it came as a BIG surprise to me that computer scientists don’t think that, and a program like Excel will interpret:

=-5^2

as positive 25!  After taking quite a while debugging a student problem calculating the normal distribution in Excel, it got me on a quest (and an argument with a colleague) to figure out who else thought this way.  I checked Matlab, Mathematica, Python, and Google as well as a calculator on the computer.  All interpreted -5^2 (properly) as -25.  To do otherwise, I believe, is perverse for any application that is doing mathematical applications.  I was directed to this page, which outlines many languages.  Pretty much just Excel, COBOL, Chipmunk BASIC and a few small scripting languages take the “unary minus” approach, which makes “unary minus” have precedence over exponentiation.

I am not sure why anyone would consider this a good idea, for working with actual math equations.  Of course one could add parentheses, but which is clearer:

y=exp(-x^2)

or

y=exp(-(x^2))

The second is obviously not ambiguous, but less clear.  Anyway, that is the entire reason why we have order of operations, so we don’t have to do:

5+(3*4)-(2*3)+(2*(3^3))

So, Excel, come into at least the 20th century and figure out that exponentiation trumps “minus”, whatever you want to call it.

 

Advertisements

About brianblais

I am a professor of Science and Technology at Bryant University in Smithfield, RI, and a research professor in the Institute for Brain and Neural Systems, Brown University. My research is in computational neuroscience and statistics. I teach physics, meteorology, astonomy, theoretical neuroscience, systems dynamics, artificial intelligence and robotics. My book, "Theory of Cortical Plasticity" (World Scientific, 2004), details a theory of learning and memory in the cortex, and presents the consequences and predictions of the theory. I am an avid python enthusiast, and a Bayesian (a la E. T. Jaynes), and love music.
This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to Is -x^2 positive or negative?

  1. scottk says:

    I have to disagree with your generalization that computer scientists say +25. I imagine most of my colleagues would agree that it is -25 🙂

  2. bblais says:

    I love generalizations! Actually, you should ask them. You might be as surprised as I was. I do realize that not all computer scientists have this perspective, but it seems that *only* computer scientists have this perspective which is what I was really trying to say. My CS colleague claimed that mathematicians would agree with her, but a quick test showed that to be incorrect.

    I of course only have anecdotal information on all of these claims, not having done a systematic study.

  3. I must say, I am just loving your blog!!

    I am a “computer scientist” (or at least a “software engineer”), and yeah, I would say that -5 squared is +25. After all, -2 * -3 would be +6, right? So isn’t it true that -5 * -5 = +25? That would seem obvious to me. My Windows calculator agrees, but I suppose it was written by a computer scientist.

    I think I understand the confusion though. It’s in the operator precedence. Does -5^2 mean (-5)^2 or -(5^2)? In computer programming, if you have a value sitting in memory, like -5, and you pass that value to a function that multiplies the value by itself, you are fixing the operator precedence by design. In most computer languages (all that I know of) there is no operator for “to the power of”. One cannot write the statement “x = -5^2”, so that mere operator precendence comes into play. One must write either “x = sqr(-5)” or “x = -sqr(5)”.

  4. brianblais says:

    “I must say, I am just loving your blog!!” thanks!

    “I think I understand the confusion though. It’s in the operator precedence. “.

    Yes, that is it. In computer science that have an operator called “unary minus” which takes precedence over all other things (including exponentiation). Mathematics doesn’t have this. I think the problem then becomes one of implementation, and nearly the only implementation that keeps using unary minus is in spreadsheets. Not sure what the history of it is.

    Although C and Pascal have to exponentiation, Fortran does (**). So does Python, Matlab, R, and Mathematica – and all agree that -5**2 = -25 (or -5^2=-25).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s