Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another precedence trap: -1**5 #971

Closed
AlexDaniel opened this issue Oct 16, 2016 · 17 comments
Closed

Another precedence trap: -1**5 #971

AlexDaniel opened this issue Oct 16, 2016 · 17 comments
Labels
docs Documentation issue (primary issue type) trap

Comments

@AlexDaniel
Copy link
Member

Sometimes I do this:

Code:

say -1**2

Output:

-1

When I actually mean this:

say (-1)**2

Output:

1
@AlexDaniel AlexDaniel added the docs Documentation issue (primary issue type) label Oct 16, 2016
@LLFourn
Copy link
Contributor

LLFourn commented Oct 16, 2016

is it intended that infix ** be tighter than prefix - ?

On Mon, Oct 17, 2016 at 5:01 AM Aleks-Daniel Jakimenko-Aleksejev <
notifications@github.com> wrote:

Sometimes I do this:

Code:

say -1**2

Output:

-1

When I actually mean this:

say (-1)**2

Output:

1


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#971, or mute the thread
https://github.com/notifications/unsubscribe-auth/ADj6Qycqa2NqUDk9GEU1Nxf9GG87UEP8ks5q0mZlgaJpZM4KYCNK
.

@pierre-vigier
Copy link

It seems normal for me that ** is tigher that -, after a quick check, python, ruby and Octave have the same behavior as well

@labster
Copy link
Contributor

labster commented Oct 17, 2016

Assuming only real numbers, in normal mathematical notation:

y = -x²
z = -3²

Would you expect y to be positive or negative? The same for z? I would definitely expect a negative number for y.

@AlexDaniel
Copy link
Member Author

perhaps it means that unicode ops should have looser precedence? 🤷

@labster
Copy link
Contributor

labster commented Oct 17, 2016

Aren't all ops Unicode?

@LLFourn
Copy link
Contributor

LLFourn commented Oct 17, 2016

@labster I think @AlexDaniel means that the more mathematical notation of ² could be tighter while **2 be looser. I expected ** to be looser simply because it looks looser to me. What I expected is apparently not the norm so nvm me :)

@VynceMontgomery
Copy link
Contributor

VynceMontgomery commented Oct 26, 2016

(subject of issue is misleading, because answer is the same either way)

- x**2 should definitely square before negating.
-5 ** 2 sure feels to me like it should negate before squaring.
whitespace affecting precedence is a sketchy place to be, but Perl is supposed to DWIM and in this case, it doesn't feel like - is a unary operator, it feels like it's part of the number literal. YMMV, of course.

@VynceMontgomery
Copy link
Contributor

in fact, -2 is given as an example of an integer literal, and yet it appears not to be parsed that way. this seems ... wrong.

@jnthn
Copy link
Contributor

jnthn commented Oct 26, 2016

Indeed, negatives are not parsed as literals (with the exception of -Inf), so if something gives -2 as an example of an integer literal it probably warrants a clarification.

VynceMontgomery added a commit to VynceMontgomery/doc that referenced this issue Oct 26, 2016
Belief in negative numeric literals creates false expectations regarding precedence conflicts (see Raku#971)
@VynceMontgomery
Copy link
Contributor

What reasoning (and evidence) is there for -Inf parsing as a literal and not as unary - applied to Inf?

@jnthn
Copy link
Contributor

jnthn commented Oct 26, 2016

Can't say much about reasoning. As for evidence, I checked it in the grammar; see https://github.com/rakudo/rakudo/blob/nom/src/Perl6/Grammar.nqp#L3386

@b2gills
Copy link
Contributor

b2gills commented Oct 26, 2016

Perhaps -x ** y should produce an error telling you to use parens to disambiguate.

@pierre-vigier
Copy link

If you write -1 to the power of 2 by hand, without parenthesis, you expect the power to take precedence on the minus, so why would perl6 act differently. Written by hand, space are not affecting precedence of the multiplication operator or the power operator.
Written by hand, if you want -1 to the power of 2 to be equal to one, you will add parenthesis, so the current behaviour seems expected, at least for me

@AlexDaniel
Copy link
Member Author

@pierre-vigier but if you write -1 ** 2 you definitely mean something different (although my original report says -1**2).

@pierre-vigier
Copy link

Written by hand, spaces for that expression do not matter and do not change precedence, I really don't see why they should change something in perl, I would not treat it differently because of the spaces, and I would actually use parentheses, to elevate minus one to power 2

@AlexDaniel
Copy link
Member Author

How often do you write ** operator by hand? /o\

@pierre-vigier
Copy link

For me having the **2 notation, or the superscript ² does not make any difference, it is the same operator, no? Why would they be treated differently? anyway, as i am concerned, i almost never use a statement with just that, but if you take the inverse statement, it seems odd :

my $x = - ( 1 ** 2 );

i feel more confortable with:

my $x = -1**2;

But maybe the correct solution is the one proposed previously that force the usage of parentethese, to not have an error or a warning.

As a reference, if usefull, with ruby repl:

irb(main):001:0> -1**2
=> -1
irb(main):002:0> -1 ** 2
=> -1
irb(main):003:0> -1  **  2
=> -1

in python:

Python 2.7.12 (default, Jun 29 2016, 14:05:02)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> -1**2
-1
>>> -1 ** 2
-1
>>> -1  **   2
-1
>>>

perl5

» perl -E "say -1**2;say -1 ** 2;say -1  **  2"
-1
-1
-1

a search in google with -1 ** 2 also give -1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation issue (primary issue type) trap
Projects
None yet
Development

No branches or pull requests

7 participants