FAQ
The following piece of trivial code, (drastically simplified from an
example I was trying to convert), produces different results (3rd
line) under Perl 5 and Perl 6. (Saved as t_f_int2)

print sqrt (1.5 * 1) ;
print "\n";
print ((1 / 2.7) ** 1);
print "\n";

print sqrt (1.5 * 1) *
((1 / 2.7) ** 1);

print "\n";

$>perl t_f_int2
1.22474487139159
0.37037037037037
0.453609211626514

$>perl6 t_f_int2
1.22474487139159
0.37037037037037
0.74535599249993

Have I found a bug or merely revealed my ignorance of a change in
precedence somewhere?

Search Discussions

  • Stefan O'Rear at May 17, 2011 at 4:16 am

    On Mon, May 16, 2011 at 03:45:08PM -0400, Parrot Raiser wrote:
    The following piece of trivial code, (drastically simplified from an
    example I was trying to convert), produces different results (3rd
    line) under Perl 5 and Perl 6. (Saved as t_f_int2)

    print sqrt (1.5 * 1) ;
    print "\n";
    print ((1 / 2.7) ** 1);
    print "\n";

    print sqrt (1.5 * 1) *
    ((1 / 2.7) ** 1);

    print "\n";

    $>perl t_f_int2
    1.22474487139159
    0.37037037037037
    0.453609211626514

    $>perl6 t_f_int2
    1.22474487139159
    0.37037037037037
    0.74535599249993

    Have I found a bug or merely revealed my ignorance of a change in
    precedence somewhere?
    Quite a few Perl 5 prefix ops are list ops in Rakudo. We haven't made
    up our minds whether this is a bug yet; note that the compilers still
    disagree.

    -sorear
  • Patrick R. Michaud at May 17, 2011 at 4:25 am

    On Mon, May 16, 2011 at 03:45:08PM -0400, Parrot Raiser wrote:
    The following piece of trivial code, (drastically simplified from an
    example I was trying to convert), produces different results (3rd
    line) under Perl 5 and Perl 6. (Saved as t_f_int2)

    print sqrt (1.5 * 1) ;
    print "\n";
    print ((1 / 2.7) ** 1);
    print "\n";

    print sqrt (1.5 * 1) *
    ((1 / 2.7) ** 1);

    print "\n";
    [...]
    $>perl6 t_f_int2
    1.22474487139159
    0.37037037037037
    0.74535599249993

    Have I found a bug or merely revealed my ignorance of a change in
    precedence somewhere?
    You've found the difference in how Perl 5 and Perl 6 handles
    whitespace+parentheses following a function name. In Perl 5, only
    the parenthesized part is passed to C<sqrt>; in Perl 6, the entire
    expression will be passed as an argument to the sqrt listop.

    Removing the space between "sqrt" and the parenthesized part will
    cause sqrt to act like a normal function on just the value in the
    parentheses instead of the full expression:

    $ ./perl6 -e 'say sqrt (1.5 * 1) * ((1 / 2.7) ** 1);'
    0.74535599249993
    $ ./perl6 -e 'say sqrt(1.5 * 1) * ((1 / 2.7) ** 1);'
    0.453609211626514

    In general, a paren immediately following an identifier is treated
    as a function call, while whitespace indicates it's a listop or some
    other construct.

    Pm
  • Parrot Raiser at May 17, 2011 at 2:41 pm
    Thanks.

    That's obscure enough that I don't feel too bad about overlooking it.
    I've spent a lot of time teaching people that Perl is ignores
    whitespace, except in quotes.
    On 5/17/11, Patrick R. Michaud wrote:
    On Mon, May 16, 2011 at 03:45:08PM -0400, Parrot Raiser wrote:
    The following piece of trivial code, (drastically simplified from an
    example I was trying to convert), produces different results (3rd
    line) under Perl 5 and Perl 6. (Saved as t_f_int2)

    print sqrt (1.5 * 1) ;
    print "\n";
    print ((1 / 2.7) ** 1);
    print "\n";

    print sqrt (1.5 * 1) *
    ((1 / 2.7) ** 1);

    print "\n";
    [...]
    $>perl6 t_f_int2
    1.22474487139159
    0.37037037037037
    0.74535599249993

    Have I found a bug or merely revealed my ignorance of a change in
    precedence somewhere?
    You've found the difference in how Perl 5 and Perl 6 handles
    whitespace+parentheses following a function name. In Perl 5, only
    the parenthesized part is passed to C<sqrt>; in Perl 6, the entire
    expression will be passed as an argument to the sqrt listop.

    Removing the space between "sqrt" and the parenthesized part will
    cause sqrt to act like a normal function on just the value in the
    parentheses instead of the full expression:

    $ ./perl6 -e 'say sqrt (1.5 * 1) * ((1 / 2.7) ** 1);'
    0.74535599249993
    $ ./perl6 -e 'say sqrt(1.5 * 1) * ((1 / 2.7) ** 1);'
    0.453609211626514

    In general, a paren immediately following an identifier is treated
    as a function call, while whitespace indicates it's a listop or some
    other construct.

    Pm

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl6-compiler @
categoriesperl
postedMay 16, '11 at 7:45p
activeMay 17, '11 at 2:41p
posts4
users3
websiteperl6.org

People

Translate

site design / logo © 2021 Grokbase