3 ms·
https://www.wolframalpha.com/input?i=-3%5E2 https://www.wolframalpha.com/input?i=-3%5E2 https://en.wikipedia.org/wiki/Order_of_operations https://en.wikipedia.
by shagie 2y ago
https://www.wolframalpha.com/input?i=-3%5E2 https://www.wolframalpha.com/input?i=-3%5E2
https://en.wikipedia.org/wiki/Order_of_operations https://en.wikipedia.org/wiki/Order_of_operations
Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction
-3^2 would then be correctly parsed as -(3^2) which is -9.
Parsing it as (-3)^2 would require the addition of parentheses.
This gets to the special case of the unary minus sign... which the Wikipedia article specifically calls out.
Special cases
Unary minus sign
There are differing conventions concerning the unary operation '−' (usually pronounced "minus"). In written or printed mathematics, the expression −3² is interpreted to mean −(3²) = −9.
In some applications and programming languages, notably Microsoft Excel, PlanMaker (and other spreadsheet applications) and the programming language bc, unary operations have a higher priority than binary operations, that is, the unary minus has higher precedence than exponentiation, so in those languages −3² will be interpreted as (−3)² = 9. This does not apply to the binary minus operation '−'; for example in Microsoft Excel while the formulas =-2^2, =-(2)^2 and =0+-2^2 return 4, the formulas =0-2^2 and =-(2^2) return −4.
(edit)
Digging into this a little bit more...
https://www.gnu.org/software/bc/manual/html_mono/bc.html#TOC13 https://www.gnu.org/software/bc/manual/html_mono/bc.html#TOC...
The expression precedence is as follows: (lowest to highest)
|| operator, left associative
&& operator, left associative
! operator, nonassociative
Relational operators, left associative
Assignment operator, right associative
+ and - operators, left associative
*, / and % operators, left associative
^ operator, right associative
unary - operator, nonassociative
++ and -- operators, nonassociative
This precedence was chosen so that POSIX compliant bc programs will run correctly. This will cause the use of the relational and logical operators to have some unusual behavior when used with assignment expressions. Consider the expression:
...
This brings us to the POSIX specification for bc https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/bc.html https://pubs.opengroup.org/onlinepubs/9699919799.2018edition...
This also shows the unary - having higher precedence than ^.
https://github.com/gavinhoward/bc/blob/master/manuals/development.md https://github.com/gavinhoward/bc/blob/master/manuals/develo...
This document is meant for the day when I (Gavin D. Howard) get hit by a bus. In other words, it's meant to make the bus factor a non-issue.
This document is supposed to contain all of the knowledge necessary to develop bc and dc.
In addition, this document is meant to add to the oral tradition of software engineering, as described by Bryan Cantrill.
... now, it would be interesting if gavinhoward could clarify some of the design thoughts there (and I absolutely love the oral tradition talk).
- JonChesterfield 2y agoFortunately these rules are natural and easy to remember, especially the details of how different languages choose different orders, relative to the confusing and abhorrent prefix notation (- (^ 3 2)) or (^ (- 3) 2).
- shagie 2y agoAs a lover of prefix and postfix notation, there is an unambiguous parsing of each of those that does not deepened on any order of operations. Neither lisp nor forth have the question at all - you can only write it exactly as you mean it. (- (expt 3 2)) is always -9 without needing to ask which has higher precedence. (expt -3 2) is likewise always 9. There is no question if - is a binary or unary operator in prefix notation and what its order of operation should be. Likewise, in dc 3 _ 2 ^ p _3 2 ^ p and 3 2 ^ _ p where '_' is the negation operator (it can be used for writing -3 directly as _3, but _ 3 is a parse error) return their results without any question of order of operations. When you start touching infix, you get into https://en.wikipedia.org/wiki/Shunting_yard_algorithm https://en.wikipedia.org/wiki/Shunting_yard_algorithm which was not a fun part of my compiler class. (And yes, I do recognize your credentials ... I still think that lisp and forth (above examples for dc) are better notational systems for working with computers even if it takes a bit of head wrapping for humans).
- bikenaga 2y agoMaybe something's wrong with my terminal. In dc: 3 _ 2 ^ p gives 0 _3 2 ^ p gives 9 3 2 ^ _ p gives 0 5 _ p gives 0 _5 p gives -5 You didn't intend that I should get those zeros, right?
- shagie 2y ago~ % dc -v dc 6.5.0 Copyright (c) 2018-2023 Gavin D. Howard and contributors Report bugs at: https://git.gavinhoward.com/gavin/bc This is free software with ABSOLUTELY NO WARRANTY. ~ % dc 3 _ 2 ^ p 9 _3 2 ^ p 9 3 2 ^ _ p -9 5 _ p -5 _5 p -5 (control-D) The version that I have appears to have _ parsed as an operator in addition to the negation of a numeric constant.