11/18/2023 0 Comments Integers negative plus negativeVarsity Tutors does not have affiliation with universities mentioned on its website. Media outlet trademarks are owned by the respective media outlets and are not affiliated with Varsity Tutors.Īward-Winning claim based on CBS Local and Houston Press awards. These parsing rules may subtly vary between different programming languages, but at least for a single language they must always be well-specified.Īlas, in maths it's often not so clear-cut – for some expressions it is indeed up to interpretation what they mean! For instance, does $\sin x^2$ mean $(\sin x)^2$ or rather $\sin(x^2)$? IMO it should be the former (because function application binds tightly), but I think the majority of mathematicians and scientist don't agree, and hence the completely ridiculous notation $\sin^2 x$ is used for that.Names of standardized tests are owned by the trademark holders and are not affiliated with Varsity Tutors LLC.Ĥ.9/5.0 Satisfaction Rating based upon cumulative historical session ratings through 12/31/20. So when the expression pow(0+(-1)*3, 2) is encountered, the parser first splits it up at the comma, then at the +, then at the *, before considering the inner parenthesis.īut in languages with an exponentiation operator, this should, as in maths notation, have a higher fixity than the other operators. For instance, in C and C++ the operator-precendence hierarchy includes the following: Hence the -3 in pow(-3,2) is independent of the 2 and the pow function.Īll other infix operators, like + and **, have some predefined fixity. Commata always separate independent subtrees.Function application is typically connected to parenthesis, so this also binds tighly. Parentheses group a subtree together, no matter what happens around them.In programming languages, the parsing rules are generally these: Which is a different calculation and gives the result $9$. OTOH, if you write in C# Math.pow(-3, 2), then this clearly is parsed as Why not? Well, these are just the conventions for how expressions are parsed: exponentiation binds more tightly than negation (which is, kinda reasonably, on the same level as addition). The term $-3^2$, or the Python expression -3**2, means For instance, $f(g(x), h(y,z))$ is really a character-string encoding for something like IMO it helps a lot to understand how syntax of programming languages, and in a less straighforward way also maths notation, always correspends to a tree data structure. What is the correct answer, and why? Thanks! Another question here on SE regarding calculators with this same issue, the accepted answer was that adding parentheses fixed the "issue", but doesn't address whether the calculator is getting it "wrong" because it's not actually wrong. My scientific calculator tells me it is 9. The high school math teacher that authored The Complete Idiot's Guide to Algebra presented this question as "be careful, this one is tricky" specifically to teach this situation, but since there seems to some confusion as to which is the right way to calculate this. How can it be a matter of opinion? If an equation is being used for calculating a space craft landing, or the engineering of a bridge design, a difference of opinion on how to calulate this could prove catastrophic. The third item on Google's search results offered up a math forum where the moderator, one Doctor Rick, states that whether it is interpreted as -3^2 or -(3)^2 is a difference of opinion. Note that this is positive because when you multiply two negative numbers you get a positive result." - This, of course, is the exact opposite of what was asked, but it's the given response. "This is because to square a number just means to multiply it by itself. Google presents an excerpt from a site that says the converse. A search on Google for why is a negative number squared negative I get conflicting results. I asked my engineer brother this problem and he got it wrong. I found this problem while doing an algebra refresher in the book The Complete Idiot's Guide to Algebra.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |