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
Comments
|
is it intended that infix ** be tighter than prefix - ? On Mon, Oct 17, 2016 at 5:01 AM Aleks-Daniel Jakimenko-Aleksejev <
|
|
It seems normal for me that ** is tigher that -, after a quick check, python, ruby and Octave have the same behavior as well |
|
Assuming only real numbers, in normal mathematical notation: y = -x² Would you expect y to be positive or negative? The same for z? I would definitely expect a negative number for y. |
|
perhaps it means that unicode ops should have looser precedence? 🤷 |
|
Aren't all ops Unicode? |
|
@labster I think @AlexDaniel means that the more mathematical notation of ² could be tighter while |
|
(subject of issue is misleading, because answer is the same either way)
|
|
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. |
|
Indeed, negatives are not parsed as literals (with the exception of |
Belief in negative numeric literals creates false expectations regarding precedence conflicts (see Raku#971)
|
What reasoning (and evidence) is there for -Inf parsing as a literal and not as unary - applied to Inf? |
|
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 |
|
Perhaps |
|
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. |
|
@pierre-vigier but if you write |
|
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 |
|
How often do you write |
|
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: in python: perl5 a search in google with -1 ** 2 also give -1 |
Sometimes I do this:
Code:
Output:
When I actually mean this:
Output:
The text was updated successfully, but these errors were encountered: