6 ms·
But it's 9! Check it: https://www.mathplanet.com/education/pre-algebra/explore-and-understand-integers/multiplying-and-dividing-with-integers#:~:text=You%20als
by mobilio 2y ago
But it's 9!
Check it:
https://www.mathplanet.com/education/pre-algebra/explore-and-understand-integers/multiplying-and-dividing-with-integers#:~:text=You%20also%20have%20to%20pay,the%20product%20is%20always%20positive https://www.mathplanet.com/education/pre-algebra/explore-and....
"You also have to pay attention to the signs when you multiply and divide. There are two simple rules to remember:
When you multiply a negative number by a positive number then the product is always negative.
When you multiply two negative numbers or two positive numbers then the product is always positive."
So basically you have -3x-3 and result is 9.
- n_plus_1_acc 2y agoCorrect, but irrelevant to the question. The usual rules of math require it to be parsed like -(3²), so there are only positive numbers being multiplied.
- shagie 2y agohttps://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.
- bikenaga 2y agoNo - in a subtle way, you're assuming that the unary minus has precedence, when the point is that it doesn't. You're right that "thing^2" means "thing times thing", but in "-3^2", what is it that is being squared? To write it, as you did, as "(-3) x (-3)", assumes that in "-3^2" the thing being squared is "-3". But that in turn assumes that the unary minus is done before the square. By the standard mathematical convention, in "-3^2" the thing being squared is "3". So you do "3 * 3", then you negate the result and get "-9".
- deleted 2y ago[deleted]
- HKH2 2y agoThanks. I had no idea. Seems counterintuitive at first, but it makes sense, since ’-3’ is '0 - 3'.
- setopt 2y agoThis is a good way to think of it. It would be very confusing if “0 - 3^2” and “0 + (-3^2)” should mean different things.
- zmgsabst 2y agoBut what about (0-3)^2? You said that “-3” = “0-3”. So we have “-3^2” is “(0-3)^2” is 9. Agreeing with -3^2 = 9. You’re performing a sleight of hand when you define “-3” to be “0-3”, but move the parenthesis to get your second equation. You have to insert your definition as a single term inside parenthesis — you can’t simply remove them to change association (as you have done). That’s against the rules. So if you think “-3” is “0-3”, then you should agree the answer is 9.
- RandomThoughts3 2y ago> (0-3)^2? It’s entirely unambiguous due to the parenthesis. I don’t think the rest of your argument actually makes sense. There is no sleight of hand required. The original argument is entirely related to having unary minus and binary minus which are different operators conceptually have similar precedence as being less surprising.
- alpinisme 2y agoHow we right negative literals in expressions is the issue. Nobody would seriously think -x^2 = -4 had no non-imaginary solutions. But if we swap out x for a literal number people start interpreting it ambiguously as maybe a negative number exponentiated, but fundamentally it’s the same syntax as before.