4 ms·
Order of operations. -4^2 is equivalent to -(4^2).
by jhurliman 15y ago
Order of operations. -4^2 is equivalent to -(4^2).
- aaronblohowiak 15y agoheehee, programmer's error. -4 is assumed to be the value, and not seen as -1 * 4, which i believe is the interpretation. order of operations is a useless source of confusion -- we should just use parens always and avoid ambiguity. #toomuchcprogramminglately
- patio11 15y agoI tend to agree, and always did explicit parens for that sort of thing. Then again I use parens in ruby code, too, so maybe I just like them a little too much. You could argue from consistency that the standard convention is more aesthetically appealing, though. I tried writing this in a HN post but it boiled my brain so I sketched it on the back of an envelope instead. http://images2.bingocardcreator.com/blog-images/hn/arithmetic-consistency.jpg http://images2.bingocardcreator.com/blog-images/hn/arithmeti... Basically, assume high school algebra works the way you think it works. If we treat unary negation like it magically grabs the nearest integer prior to the exponent happening, things we assume are equal in high school algebra start to break catastrophically. You'd have to invent a new and perhaps uglier way to do algebra which would lose properties like "moving terms around the same side of an equation on paper for clarity is allowed because it doesn't change what they sum to." Anyhow, if this ever comes up in my household, I'm going with "Yeah, that one's weird. Your dad didn't like it either, but convention is that -2^2 is -4. That's totally separate from math, it's just how we choose to represent math in squiggles, and this collection of squiggles is supposed to be read as -1 * (2^2) instead of (-1 * 2)^2 . Why did we pick that math-to-squiggles mapping? Long story and kind of boring, but it's sort of important that everyone read the squiggles the same way, for the same reason that we don't get into an argument over which side of a road should be called 'left' and then smash our cars into each other. Either way would have been a perfectly good 'left' because left is ultimately just squiggles, but now we're stuck with one of the squiggles."
- aaronblohowiak 15y agoAt the risk of getting off in the weeds, I'd rather re-frame the problem here as to wether unary negation makes sense as an operation, at all! In my ideal world, -EXPR would be a parse error, and it would only be valid to write -LITERAL * expr. (-1 * a.) So, you could have -4^2 is 16 because the - would not associate to the nearest constant, but rather be part of the 4-value. The reason for this is that -a^2 is super confusing, and I believe it should be treated as invalid. I do not think that promoting the order of precedence of unary - is the best solution because it leads to the common confusion of thinking "well, a is already negative, so there is nothing to do". if instead this was explicitly -1 * a ^ 2, then you can always "plug and chug" with simpler rules. Essentially, the confusion is because unary negation is the only operation that is confuse-able with the value definition. Final qualm against unary negation: what does --4 evaluate to in ruby? 3? No. 4. If I had my way, then --4 would clearly be invalid because the inner -4 could not receive the unary negation. In more programming stuff, I think that mixed-type operations should be illegal (no implicit casts!) adding an int64 to an int32 and storing the result in an int32 should be a compiler error!
- yequalsx 15y agoI've found that most people will correctly do 1 - 4^2 The answer is -15. So what is 0 - 4^2 Most will say -16. To be consistent we must agree that -4^2 is -16 since it is the same thing as 0 - 4^2. It takes a while for students to understand this. I still get calculus students who mess it up.