Mark44
Mentor
- 38,033
- 10,506
I think I understand what you think you meant, but that's different from what you actually wrote. No calculator will display either -n or n if the input is -n^2. IOW, squaring a number won't produce that number except if the number is 1.DeBangis21 said:Thanks. I am getting something in here. I use to wonder and always asked myself, and others, why -n^2 in my calculator gives -n instead of n.
PEMDAS doesn't really cover it, as the minus sign is really the (unary) negation operator rather than the (binary) subtraction operator.DaveC426913 said:*sigh* Because of PEMDAS.
Parentheses
Exponents
Multiplication and Division
Addition and Subtraction
Programming languages generally do a much better job of specifying the order of operator precedence than does mathematics. In addition, many languages also specify the associativity; i.e., how operators at the same precedence level are evaluated, whether left-to-right or right-to-left.DaveC426913 said:The minus sign ( - ) in front is an operator, just like addition, multiplication or exponentiation. It operates on what is immediately to its right (i.e. 32 AKA +9 ) to turn it into its operative inverse. That is what that minus sign means. And so, it gets processed in PEMDAS order - i.e. last.
For example, in Python, exponentiation (denoted as **) is of higher precedence than negation (see https://www.geeksforgeeks.org/precedence-and-associativity-of-operators-in-python/). For an expression such as -3**2, 3 is squared before the result is negated. Because of the precedence of the two operators, -3**2 has exactly the same value as -(3**2).