Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Is it a bug in perl6 ?

by ej8000 (Initiate)
on Aug 01, 2011 at 22:55 UTC ( [id://917935]=perlquestion: print w/replies, xml ) Need Help??

ej8000 has asked for the wisdom of the Perl Monks concerning the following question:

hi dears, I think it is a bug in my version of perl6 !! $result1 and $result2 must be the same but you got different answers as in this code.

my %ball = map {; $_ =>1 },1..8; %ball{'3'}=.9; my $a1= [+] %ball{1..4}; my $a2= [+] %ball{5..8}; my $result1= [+] %ball{1..4} <=> [+] %ball{5..8} ; print "$a1 $a2"~"\n"; say $result1; my $result2= $a1 <=> [+] %ball{5..8} ; say $result2;

Replies are listed 'Best First'.
Re: Is it a bug in perl6 ?
by TimToady (Parson) on Aug 02, 2011 at 00:53 UTC
    You have a precedence error. [+] parses as a list operator, which is much looser than <=>. If you put parens around the first summation it will work as you expect in any of the implementations.

      Is any one of the various perl6 implementations yet complete, quick and stable enough for real work?

        I'll answer that if you tell me what percentage of monks are complete, quick, and stable enough for real work? :-)
Re: Is it a bug in perl6 ?
by davido (Cardinal) on Aug 01, 2011 at 23:52 UTC

    Given the pre-alpha nature of Perl6, as well as its several development branches, it's a good idea to identify what build you're working with. Particularly since Perl6 is anything that passes the Perl6 test suite, which is supposed to be thorough.

    If what you're describing is really happening (and if I understand your Perl6 code, which I may not), there's a problem. Could you build some tests similar to the Perl5 code below? One would expect floating point errors, but not as significant as 0.1 when adding only four numbers.

    In translating, I tried to keep the same concepts in place, replacing the Perl6 [+] operator with List::Util's sum() operator. The rest was pretty much just small syntax changes.

    Perl5 test code follows:

    And the output:

    # Running tests: 1..14 # # Environment: # %ball holds: # 1 => 1 # 2 => 1 # 3 => 0.9 # 4 => 1 # 5 => 1 # 6 => 1 # 7 => 1 # 8 => 1 # $a1 = 3.9 $a2 = 4 # $result1 = -1 $result2 = -1 # # Tests: ok 1 - %ball has expected keys and values. ok 2 - $ball{3} (0.9) == 0.9 ok 3 - sum( @ball{ 1 .. 8 } ) == 7.9 ok 4 - $a1 == 3.9 ok 5 - $a2 == 4.0 ok 6 - $a1 (3.9) < $a2 (4) ok 7 - $result1 (-1) == $result2 (-1) ok 8 - $a1 (3.9) < sum @ball{ 5 .. 8 } (4) ok 9 - $a2 (4) > sum @ball{ 1 .. 4 } (3.9) ok 10 - sum( @ball{ 1 .. 4 } ) (3.9) < sum( @ball{ 5 .. 8 } ) (4) ok 11 - 3.9 <=> 4 == -1 ok 12 - 4 <=> 3.9 == 1 ok 13 - 3.9 <=> 3.9 == 0 ok 14 - 4 <=> 4 == 0

    It seemed foolish building so many redundant tests, but if what you are trying to highlight is an actual bug it would merit even more tests than this to be sure. As you can see, I couldn't reproduce it in Perl5. I intentionally stayed away from tests that might have accumulated floating point errors, which should be a different topic. Having 3.9 suddenly test greater than 4 would be more than a small floating point rounding error. I wish I had Perl6 installed to try to reproduce it there, but don't wish so much as to actually install it yet.

    You've got to love Test::More. I assume some similar framework exists for Perl6 testing.

    Update: Added a few more tests. Why, I don't know. ;)


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://917935]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-29 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found