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