Forum: Ruby-core the sign of a number is omitted when squaring it. -2**2 vs (

95604dc5a060875e8efe9d80151b0403?d=identicon&s=25 unknown (Guest)
on 2006-11-07 10:58
(Received via mailing list)
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...
5d9e2d54d4f7fa5fe9a24cdce5b9010a?d=identicon&s=25 Nate Wiger (Guest)
on 2006-11-07 10:59
(Received via mailing list)
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
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-11-07 10:59
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-11-07 11:01
(Received via mailing list)
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?
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-11-07 11:02
(Received via mailing list)
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
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-11-07 11:04
(Received via mailing list)
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.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-11-07 11:04
(Received via mailing list)
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.
3bb23e7770680ea44a2d79e6d10daaed?d=identicon&s=25 M. Edward (Ed) Borasky (Guest)
on 2006-11-07 11:04
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-11-07 11:04
(Received via mailing list)
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.
This topic is locked and can not be replied to.