Bugs item #6468, was opened at 2006-11-03 17:25 You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=169... Category: Core Group: 1.8.5 Status: Open Resolution: None Priority: 3 Submitted By: Boris Schmid (bor_) Assigned to: Nobody (None) Summary: the sign of a number is omitted when squaring it. -2**2 vs (-2)**2 Initial Comment: Due to the way ruby interprets formula's, squaring a negative number isn't possible unless you use brackets. This caught me unaware, and might be counterintuitive for more people. irb(main):018:0> -2**2 => -4 irb(main):019:0> (-2)**2 => 4 Some trouble: irb(main):006:0> 3-2**2 => -1 The above works fine right now, but only because it is seen as 3 - 2**2. If "-2" is recognized as a negative number, and "- 2" as 'substract 2, then the 3-2**2 example should rise an error. ---------------------------------------------------------------------- You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=169...
on 2006-11-07 10:58
on 2006-11-07 10:59
noreply@rubyforge.org wrote: > > Some trouble: > irb(main):006:0> 3-2**2 > => -1 > The above works fine right now, but only because it is seen as 3 - 2**2. If "-2" is recognized as a negative number, and "- 2" as 'substract 2, then the 3-2**2 example should rise an error. FWIW, I don't see how this is a bug or even bad behavior. Other languages work similiarly: $ perl -le 'print -2**2' -4 $ perl -le 'print 3-2**2' -1 This seems like one of those "could see it either way" issues. I personally think the Ruby behavior is consistent. Changing it could break existing code. Using parens to disambiguate the expression works fine. -Nate
on 2006-11-07 10:59
On 11/3/06, noreply@rubyforge.org <noreply@rubyforge.org> wrote: > Due to the way ruby interprets formula's, squaring a negative number > isn't possible unless you use brackets. This caught me unaware, and > might be counterintuitive for more people. > > irb(main):018:0> -2**2 > => -4 > irb(main):019:0> (-2)**2 > => 4 The "problem" lies in the confluence of precedence with the syntax of literals. It should be obvious that exponentiation (**) binds with a higher precedence than unary negation /as an operation/, because exponentiation has precedence of multiplication (and unary negation is essentially multiplication by -1). The confusion is because there's a misconception the the "-" in "-2" is part of the literal when it is not -- it is an operation applied to the object derived from the literal "2". Jacob Fugal
on 2006-11-07 11:01
Jacob Fugal wrote: > The "problem" lies in the confluence of precedence with the syntax of > literals. It should be obvious that exponentiation (**) binds with a > higher precedence than unary negation /as an operation/, because > exponentiation has precedence of multiplication (and unary negation is > essentially multiplication by -1). The confusion is because there's a > misconception the the "-" in "-2" is part of the literal when it is > not -- it is an operation applied to the object derived from the > literal "2". 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. It's not simply a matter of `-' having priority over `.', as can be seen from this example: irb(main):006:0> x=2 => 2 irb(main):007:0> -x.abs => -2 So "dot" does have priority over "unary minus", but not over literal formation. Why shouldn't literals always take precedence? Does it beak too many habits from ancestor languages (perl, as pointed out)? Is it too hard to parse?
on 2006-11-07 11:02
On 11/4/06, Joel VanderWerf <vjoel@path.berkeley.edu> wrote: > > > > => 2 > irb(main):007:0> -x.abs > => -2 > > So "dot" does have priority over "unary minus", but not over literal > formation. > > Why shouldn't literals always take precedence? Does it beak too many > habits from ancestor languages (perl, as pointed out)? Is it too hard to > parse? Good points, I don't know. Jacob Fugal
on 2006-11-07 11:04
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.
matz.
on 2006-11-07 11:04
Yukihiro Matsumoto wrote: > |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.
on 2006-11-07 11:04
Joel VanderWerf wrote: >> |irb(main):002:0> -2.abs > irb(main):001:0> x=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
on 2006-11-07 11:04
M. Edward (Ed) Borasky wrote: > depending on which meaning the programmer intended. And I can't for the > life of me understand why anyone would code > > (-x).abs Ok, irb(main):006:0> -2.5.ceil => -2 irb(main):007:0> -(2.5.ceil) => -3 irb(main):008:0> x = 2.5 => 2.5 irb(main):009:0> -x.ceil => -3 irb(main):010:0> -(x.ceil) => -3 To a mathematician (me) the operators appear to take different precedence depending on whether you have a literal or a variable. What's really happening is not fully explained by precedence. The lexical rules are important too and they are not simple. I'll agree with you on the importance of explicit bracketing, especially if you have to communicate with scientists or mathematicians who are not programmers.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.