Forum: Ruby-core [ruby-trunk - Feature #7328][Open] Move ** operator precedence under unary + and -

Posted by boris_stitnicky (Boris Stitnicky) (Guest)
on 2012-11-11 07:58
(Received via mailing list)
Issue #7328 has been reported by boris_stitnicky (Boris Stitnicky).

----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-11-11 09:01
(Received via mailing list)
Issue #7328 has been updated by alexeymuranov (Alexey Muranov).


(-1) ** 0.5 should not be defined! I just tried it and it gave me 
(6.123233995736766e-17+1.0i) -- approximately the complex i. Why not -i 
??
----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32768

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by Matthew Kerwin (mattyk)
on 2012-11-11 09:45
(Received via mailing list)
It woudl never be -i.  (-1) ** 0.5 => i (or, in complex coordinates, 
0+1i,
which is basically what (6.123233995736766e-17+1.0i) means.


On 11 November 2012 18:01, alexeymuranov (Alexey Muranov) <
redmine@ruby-lang.org> wrote:

> Author: boris_stitnicky (Boris Stitnicky)
> result -1 and having to type parenthesis (-1) ** 0.5... Even if it's not
> worth changing, I'd like to hear this rationalized. I've asked about
> rationalization of this on SO, and nobody seems to know why this precedence
> is the way it is.
>
>
> --
> http://bugs.ruby-lang.org/
>
>


--
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-11-11 10:34
(Received via mailing list)
Issue #7328 has been updated by alexeymuranov (Alexey Muranov).


phluid61 (Matthew Kerwin) wrote:
> It woudl never be -i.  (-1) ** 0.5 => i (or, in complex coordinates, 0+1i,
>  which is basically what (6.123233995736766e-17+1.0i) means.
>

What do you mean? Why (-1)^(0.5) i and not -i?  Mathematically, it is 
not defined.
----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32772

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-11-11 10:39
(Received via mailing list)
Issue #7328 has been updated by alexeymuranov (Alexey Muranov).


I have not still figured out how exactly this mailing issue tracker 
works, i have not managed to configure it to receive email 
notifications, and it seems that if i edit my comment the edited version 
is not what the others reply to, so i will post again the second part of 
my comment:

I agree that from pragmatic point of view it may be better for (-1)**0.5 
to return i than nothing, but then i would prefer it written clearly as 
(-1) ** 0.5.

In mathematics you would put parentheses in (-a)^n.

I think that unary +/- have the same precedence as binary, at least in 
mathematics.

I am -1 for the change.
----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32773

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by Thomas Sawyer (7rans)
on 2012-11-11 16:01
(Received via mailing list)
Issue #7328 has been updated by trans (Thomas Sawyer).


"In mathematics you would put parentheses in (-a)^n.  For example: -e^x 
is ... well ... -(e^x)."

I think that's the opposite of what's generally expected. Currently Ruby 
does:

  -2**2 #=> -4

but commonly it would be:

  -2**2 #=> 4

It's curious how Ruby got the operator precedence it has. Clearly it is 
based on C, which most languages seem to mimic without much 
consideration. Yet Ruby stuck `**` way up top. I wonder why?

----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32779

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-11-11 16:40
(Received via mailing list)
Issue #7328 has been updated by alexeymuranov (Alexey Muranov).


trans (Thomas Sawyer) wrote:
> It's curious how Ruby got the operator precedence it has. Clearly it is based on 
C, which most languages seem to mimic without much consideration. Yet Ruby stuck 
`**` way up top. I wonder why?
I am sorry, i hear for the first time that commonly -2^2 would be 4 :).
----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32780

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by Thomas Sawyer (7rans)
on 2012-11-11 17:26
(Received via mailing list)
Issue #7328 has been updated by trans (Thomas Sawyer).


@alexeymuranov

    -2 * -2 = 4


----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32782

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-11-11 17:35
(Received via mailing list)
Issue #7328 has been updated by alexeymuranov (Alexey Muranov).


trans (Thomas Sawyer) wrote:
> @alexeymuranov
>
>     -2 * -2 = 4

This is not the same, and I do not agree with this either.  In my 
opinion, "-2 * -2" is not a legal syntax (the "*-" part), it should be 
(-2)*(-2) or -2*(-2) = - (2*(-2)).

Tomas, can you find a printed or otherwise confirmed example where an 
expression with parentheses like "-(2^2)" or "-(e^x)" or "-(x^2)" is 
used?
----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32783

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by Thomas Sawyer (7rans)
on 2012-11-11 17:46
(Received via mailing list)
Issue #7328 has been updated by trans (Thomas Sawyer).


"In my opinion, "-2 * -2" is not a legal syntax (the "*-" part), it 
should be (-2)*(-2) or -2*(-2) = - (2*(-2))."

I don't understand this. Type `-2*-2` in Ruby and it produces `4`.

"Can you find a printed or otherwise confirmed example where an 
expression with parentheses like "-(2^2)" or "-(e^x)" or "-(x^2)" is 
used?"

I stand corrected on this. I was thinking in terms of just then 
numerical value. Sorry.

----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32784

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-11-11 18:00
(Received via mailing list)
Issue #7328 has been updated by alexeymuranov (Alexey Muranov).


trans (Thomas Sawyer) wrote:
> "In my opinion, "-2 * -2" is not a legal syntax (the "*-" part), it should be 
(-2)*(-2) or -2*(-2) = - (2*(-2))."
>
> I don't understand this. Type `-2*-2` in Ruby and it produces `4`.
>

I was talking about writing conventions, how i learned them in 
elementary school.  I think it is unfortunate that Ruby allows this 
"shortcut": if Ruby allows `-a * -b`, this means that it first computes 
`-a` and `-b`, and afterwards the product. This means that if i want 
Ruby to compute `- a*b` in the "standard" way, i have to put the 
parentheses `-(a*b)`.  I consider this a bug.
----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32785

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by Thomas Sawyer (7rans)
on 2012-11-11 18:02
(Received via mailing list)
Issue #7328 has been updated by trans (Thomas Sawyer).


Actually now that I think about it some more, this is similar to my 
problem with using power notation in my Stick project (an SI unit system 
for Ruby). When applying powers to units the result wasn't always the 
one desired --sometimes you want it to apply to the number, other times 
to the unit itself (e.g cubic meters). It really would be nice if `^` 
could be used for power too, along side `**`, and allow one to be just 
above unary operations and the other just below. That would provide the 
most flexibility.
----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32786

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by Thomas Sawyer (7rans)
on 2012-11-11 18:30
(Received via mailing list)
Issue #7328 has been updated by trans (Thomas Sawyer).


@alexeymuranov Okay, I see what you are saying. But I think the problem 
really is that mathematical notation tends to be a little too 
conventional and variant for a programming language, which needs to be 
more precise. In equations, the negative sign tends to either negate the 
whole, or is used to mean subtraction. It rarely occurs in the middle 
somewhere as a negation because it gets factored out to the whole. So it 
just rarely an issue.

For programming however, it would kind of suck is `* -2` was an error 
b/c `*` is applied before `-`. And it could get rather complicated if 
Ruby tried to apply one order in one case and another order in another.



----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32787

Author: boris_stitnicky (Boris Stitnicky)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by marcandre (Marc-Andre Lafortune) (Guest)
on 2012-11-11 19:33
(Received via mailing list)
Issue #7328 has been updated by marcandre (Marc-Andre Lafortune).

Status changed from Open to Rejected

Quoting Matz from http://www.ruby-forum.com/topic/87126#163398:

> People with mathematical background demand precedence for ** being
> higher than that of unary minus.  That's the reason.

Thomas: Please check your facts, e.g. 
http://en.wikipedia.org/wiki/Order_of_operations#E...
----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32788

Author: boris_stitnicky (Boris Stitnicky)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-11-11 19:34
(Received via mailing list)
Issue #7328 has been updated by alexeymuranov (Alexey Muranov).


In mathematical notation, when possible, the "minus operation" is 
usually introduced as the unary negation, and "a - b" is usually defined 
as a shorthand notation for "a + (-b)".  To avoid any ambiguity, as far 
as i know (but i do not have a reference), the minus and the plus, both 
binary and unary, have all the same precedence.  To the best of my 
knowledge, the expression "a * - b" is not a valid syntax in 
mathematics, at least in languages i am familiar with.

trans (Thomas Sawyer) wrote:
> @alexeymuranov Okay, I see what you are saying. But I think the problem really 
is that mathematical notation tends to be a little too conventional and variant 
for a programming language, which needs to be more precise. In equations, the 
negative sign tends to either negate the whole, or is used to mean subtraction. It 
rarely occurs in the middle somewhere as a negation because it gets factored out 
to the whole. So it is rarely an issue.
>
> For programming however, it would kind of suck if `* -2` was an error b/c `*` is 
applied before `-`. And it could get rather complicated if Ruby tried to apply one 
order in one case and another order in another.


----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32789

Author: boris_stitnicky (Boris Stitnicky)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by Thomas Sawyer (7rans)
on 2012-11-11 20:02
(Received via mailing list)
Issue #7328 has been updated by trans (Thomas Sawyer).


"Thomas: Please check your facts, e.g. 
http://en.wikipedia.org/wiki/Order_of_operations#E...

1) I did not make this issue.
2) If you read #9 you will see that I did check my facts.
3) Some consideration of #11 would at least be nice.

----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32790

Author: boris_stitnicky (Boris Stitnicky)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by marcandre (Marc-Andre Lafortune) (Guest)
on 2012-11-11 21:38
(Received via mailing list)
Issue #7328 has been updated by marcandre (Marc-Andre Lafortune).


Hi,

trans (Thomas Sawyer) wrote:
> "Thomas: Please check your facts, e.g. 
http://en.wikipedia.org/wiki/Order_of_operations#E...
>
> 1) I did not make this issue.

My understanding is that the original poster was wondering why the 
precedence was like that and that he would have preferred it the other 
way. Alexey answered correctly that the reason was because of the order 
or operations in mathematics.

I only wanted to suggest that before contradicting someone it was a good 
idea to check the facts, in particular if one isn't extremely familiar 
with the field. I didn't mean to offend and I'm sorry if I did.

----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32791

Author: boris_stitnicky (Boris Stitnicky)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by Matthew Kerwin (mattyk)
on 2012-11-11 22:38
(Received via mailing list)
Can I just point out that everyone is using asterisks and carets and
arguing about standard mathematical notation?

I reckon the biggest factor driving this discussion should be existing
behaviour. Changing how operators behave is probably the single biggest
thing that defines and thus changes a language.

If you were writing a new language, however, I'd find this disussion and
its outcome fascinating.
On Nov 12, 2012 6:37 AM, "marcandre (Marc-Andre Lafortune)" <
Posted by boris_stitnicky (Boris Stitnicky) (Guest)
on 2012-11-12 03:51
(Received via mailing list)
Issue #7328 has been updated by boris_stitnicky (Boris Stitnicky).


I am pleased by this discussion and I thank Tom Sawyer for showing me 
that I am not alone who has feelings of surprise at -2 ** 2 behaviour. 
However, SO people finally found this link 
(http://www.ruby-forum.com/topic/87126#163398) in which Matz has already 
rationalized ** behaviour. For this reason, I rescind this feature 
request (which has already been changed to 'Rejected' status by the time 
I am writing this), and also for the reasons of stability as per 
phluid61. Just like Alex Muranov, I do dislike the current result of 
(-1) ** 0.5, which is (6.123233995736766e-17+1.0i), but that would be 
another issue, which I don't feel like opening at the moment.
----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32798

Author: boris_stitnicky (Boris Stitnicky)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-11-12 09:59
(Received via mailing list)
Issue #7328 has been updated by alexeymuranov (Alexey Muranov).


Since #7331 is rejected, i do not think anymore it is nonsense to treat 
unary `-` specially and give it the highest precedence.  Otherwise

1. it is stuck between `*` and `**`
2. even with respect to `**` its behavior is not consistent: the 
expression `-2 ** -2` does not raise an error, but evaluates to (-1/4).
----------------------------------------
Feature #7328: Move ** operator precedence under unary + and -
https://bugs.ruby-lang.org/issues/7328#change-32812

Author: boris_stitnicky (Boris Stitnicky)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


I would like to ask to consider decreasing ** operator precedence just 
below that of -/+ unary operators. I know that other languages (eg. 
Python) have ** operator bind tighter than negation, but seeing -1 ** 
0.5 give the result -1 and having to type parenthesis (-1) ** 0.5... 
Even if it's not worth changing, I'd like to hear this rationalized. 
I've asked about rationalization of this on SO, and nobody seems to know 
why this precedence is the way it is.
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
No account? Register here.