The Math Forum

Ask Dr. Math - Questions and Answers from our Archives
Associated Topics || Dr. Math Home || Search Dr. Math

Precedence of Unary Operators

Date: 09/01/99 at 22:51:05
From: Tyler Longman
Subject: Unary operator precedence / examples

My Dad, who is a programmer, brought up the question of the precedence 
of unary operators with respect to what I'm learning in math. The 
PEMDAS rule for order of operations ignores unary operators, and my 
Dad, despite his trying for several hours, was unable to come up with 
a problem that displayed the effects of not giving unary operators 
their proper precedence. His knowledge of unary operator precedence 
comes from the world of computer programming, not necessarily the same 
as in the mathematics world (computer software vendors occasionally 
develop their own precedence schemes). Anyway, one list I found showed 
unary operator precedence below that of multiplication/division and 
above addition/subtraction. But this seems to disagree with what my 
Dad believes he was taught in college, namely that unary operators 
have a precedence right after that of parentheses. Can you help 
explain all of this and maybe give an example showing how ignorance of 
the unary operator precedence can cause an incorrect result?

Many thanks,

Date: 09/02/99 at 11:59:50
From: Doctor Peterson
Subject: Re: unary operator precedence / examples

Hi, Tyler. Good question. I'm a programmer too, so I appreciate what 
you're talking about.

I can easily think of an example, one that we are asked about all the 


The negation operator properly has precedence below the exponential, 
so that this means

     -(3^2) = -9

rather than

     (-3)^2 = 9

We don't usually list unary operators in PEMDAS because they're 
thought of as being implied by the rules for binary operations. You 
can think of the minus sign as either subtraction

     -3^2 = 0 - 3^2 = 0 - 9 = -9

or multiplication

     -3^2 = -1 * 3^2 = -1 * 9 = -9

and in either case it has lower precedence than exponentiation, so it 
gives the same result. I can't think of any case where it would matter 
which of these two latter interpretations we give, that is, any case 
where giving "-" an additive precedence or a multiplicative precedence 
will make a difference in the result; commutativity and distributivity 
seem to take care of that. For example,

     (-2) * 3

(putting negation at the same level as multiplication) is the same as

     -(2 * 3)

(putting it after multiplication). But certainly negation must not 
have a lower precedence than addition, because then

     -2 + 3

would mean

    -(2 + 3)

which is so contrary to sense that it's hard to imagine doing it.

When negation is listed in the order of operations, it's commonly put 
with or just above the multiplicative operators. After all, it is 
essentially a form of multiplication.

You're right that each programming language defines its own 
precedence; they generally try to follow tradition, but there can be 
special issues that force them to modify it. Here's the list for C:

     () [] . ->           ("primary expression operators")
     * & - ! ~ ++ --      (unary operators)
     * / %                (binary operators: multiplicative)
     + -                  (additive)
     >> <<                (shift)
     < > <= >=            (comparison)
     == !=                (equality)
     &                    (bitwise and)
     ^                    (bitwise exclusive or)
     |                    (bitwise or)
     &&                   (logical and)
     ||                   (logical or)
     ?:                   (ternary operator: conditional)

You see that C does put unary operators right after parentheses; but 
since there is no exponent operator, this doesn't conflict with my 

You may be interested in this page I ran across, on the order of 
operations in Microsoft Excel. It says that they evaluate unary minus 
before exponentiation, and will not change it though they acknowledge 
that this is different from the normal order:   

- Doctor Peterson, The Math Forum   
Associated Topics:
High School About Math
High School Basic Algebra
Middle School About Math
Middle School Algebra

Search the Dr. Math Library:

Find items containing (put spaces between keywords):
Click only once for faster results:

[ Choose "whole words" when searching for a word like age.]

all keywords, in any order at least one, that exact phrase
parts of words whole words

Submit your own question to Dr. Math

[Privacy Policy] [Terms of Use]

Math Forum Home || Math Library || Quick Reference || Math Forum Search

Ask Dr. MathTM
© 1994- The Math Forum at NCTM. All rights reserved.