Joel VanderWerf wrote: > Yukihiro Matsumoto wrote: >> Hi, >> >> In message "Re: [ ruby-Bugs-6468 ] the sign of a number is omitted >> when squaring it. -2**2 vs (-2)**2" >> on Sun, 5 Nov 2006 04:23:30 +0900, Joel VanderWerf >> <vjoel / path.berkeley.edu> writes: >> >> |Any yet >> | >> |irb(main):002:0> -2.abs >> |=> 2 >> | >> |So there are cases where the operation of "concatenating characters >> to |form a literal" has higher priority than an operation on objects. >> >> People with mathematical background demands precedence for ** being >> higher than that of unary minus. That's the reason. > > Precedence isn't the whole story: > > irb(main):001:0> x=2 > => 2 > irb(main):002:0> -x**2 > => -4 > irb(main):003:0> -2**2 > => -4 > irb(main):004:0> -x.abs > => -2 > irb(main):005:0> -2.abs > => 2 > > Tokenization works differently in different contexts (as it should). > Mathematicians need to learn this, when they read line 005 above. > I think I want to weigh in here as a mathematician and long-time scientific programmer. My view is that is the *programmer's* *responsibility* *alone* to code mathematical formulas in a manner such that they are unambiguous, both to the compiler or interpreter, and to the readers of the code. Therefore, the correct code is either (-x)**2 or -(x**2) depending on which meaning the programmer intended. And I can't for the life of me understand why anyone would code (-x).abs