Operator precedence for: 1/-2/3

  • Context: Undergrad 
  • Thread starter Thread starter Twinbee
  • Start date Start date
  • Tags Tags
    Operator
Click For Summary
SUMMARY

The discussion centers on the operator precedence in the expression 1/-2/3, leading to two interpretations: A) -0.1666666667 and B) -1.5. Most programming languages and calculators yield A as the correct answer, but participants argue that B adheres more closely to operator precedence rules, where division is performed before unary minus. The consensus suggests that clarity in mathematical expressions is crucial, advocating for the use of parentheses to avoid ambiguity.

PREREQUISITES
  • Understanding of operator precedence in programming languages
  • Familiarity with unary operators and their implications
  • Basic knowledge of mathematical expressions and their evaluations
  • Awareness of differences in operator precedence across programming languages
NEXT STEPS
  • Research operator precedence rules in Java and C programming languages
  • Explore the use of parentheses to clarify mathematical expressions
  • Learn about unary operators and their evaluation in different contexts
  • Investigate how different programming languages handle ambiguous expressions
USEFUL FOR

Mathematicians, software developers, educators, and anyone involved in programming or mathematical computation who seeks to understand operator precedence and its implications in code and calculations.

Twinbee
Messages
116
Reaction score
0
The sum below has two potential answers:

A: 1/-2/3 = -0.1666666667
B: 1/-2/3 = -1.5

Programming languages and most (but not all) calculators claim A is correct.

However, B seems to follow operator precedence more accurately as the division is performed before the unary minus. Using brackets:

1/(-(2/3))
...makes more sense than:
(1/-2)/3

Is there a more official stance on the issue?
 
Mathematics news on Phys.org
Twinbee said:
division is performed before the unary minus.
That sounds backwards to me.
 
If "division is performed before unary minus", what does 1/-2 mean?
 
That sounds backwards to me.
Okay, most people would agree that:

-3^2 = -9

So there the exponential operator has precedence over the unary minus. If ^ has precedence, it makes sense to reason that / and * should have precedence too.

If "division is performed before unary minus", what does 1/-2 mean?

The ordering of the symbols fundamentally disallows the / to be performed first here.
 
Last edited:
The order of the unitary minus has nothing to do with it. The unitary minus is equivalent to multiplying by -1. The question is which division is performed first and the convention is to use some D*** parentheses!
When there is no good reason to assume someone can figure out what you mean, don't assume someone can figure out what you mean, just say it. However, if you have a gun to you head and are asked which one is right, I'd say "A" since it reads left to right and there is no convention in math (maybe there is in compsci where the computer doesn't have an option to think about it).
 
Twinbee said:
Okay, most people would agree that:

-3^2 = -9

So there the exponential operator has precedence over the unary minus. If ^ has precedence, it makes sense to reason that / and * should have precedence too.
But the ^ operator is different from the arithmetic operators +, -, *, and /, and has higher precedence. For that reason, 3^2 + 2 is evaluated as 9 + 2 = 11, rather than 3^(2+2) = 81. Similarly 2 * 3^2 = 2 * 9 = 18, not 6^2 = 36.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 7 ·
Replies
7
Views
26K