Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Is it a bug in perl6 ?

by davido (Cardinal)
on Aug 01, 2011 at 23:52 UTC ( [id://917940]=note: print w/replies, xml ) Need Help??


in reply to Is it a bug in perl6 ?

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:

use v5.12; use strict; use warnings; use List::Util qw/sum/; BEGIN{ say "# Running tests: "; } use Test::More tests => 14; my %ball = map { $_ => 1 } 1 .. 8; $ball{3} = .9; my $a1 = sum @ball{ 1 .. 4 }; my $a2 = sum @ball{ 5 .. 8 }; my $result1 = sum( @ball{ 1 .. 4 } ) <=> sum( @ball{ 5 .. 8 } ); my $result2 = $a1 <=> sum @ball{ 5 .. 8 }; note "\nEnvironment:"; note "\t%ball holds:"; note "\t\t$_ => $ball{$_}" for sort keys %ball; note "\t\$a1 = $a1\t \$a2 = $a2"; note "\t\$result1 = $result1\t \$result2 = $result2\n\nTests:"; is_deeply( \%ball, { 1 => 1, 2 => 1, 3 => .9, 4 => 1, 5 => 1, 6 => 1, 7 => 1, 8 => 1 }, "%ball has expected keys and values." ); cmp_ok( $ball{3}, '==', 0.9, "\$ball{3} ($ball{3}) == 0.9" ); cmp_ok( sum( @ball{ 1 .. 8 } ), '==', 7.9, "sum( \@ball{ 1 .. 8 } ) == 7.9" ); cmp_ok( $a1, '==', 3.9, "\$a1 == 3.9" ); cmp_ok( $a2, '==', 4.0, "\$a2 == 4.0" ); cmp_ok( $a1, '<', $a2, '$a1 (' . $a1 . ') < $a2 (' . $a2 . ')' ); cmp_ok( $result1, '==', $result2, '$result1 (' . $result1 . ') == $result2 (' . $result2 . ')' ); cmp_ok( $a1, '<', sum( @ball{ 5 .. 8 } ), '$a1 (' . $a1 . ') < sum @ball{ 5 .. 8 } (' . sum( @ball{ 5 .. 8 } ) . ')' ); cmp_ok( $a2, '>', sum( @ball{ 1 .. 4 } ), '$a2 (' . $a2 . ') > sum @ball{ 1 .. 4 } (' . sum( @ball { 1 .. 4 } ) . ')' ); cmp_ok( sum( @ball{ 1 .. 4 } ), '<', sum( @ball{ 5 .. 8 } ), 'sum( @ball{ 1 .. 4 } ) (' . sum( @ball{ 1 .. 4 } ) . ')' . ' < ' . 'sum( @ball{ 5 .. 8 } ) (' . sum( @ball{ 5 .. 8 } ) . ')' ); cmp_ok( 3.9 <=> 4, '==', -1, "3.9 <=> 4 == -1" ); cmp_ok( 4 <=> 3.9, '==', 1, "4 <=> 3.9 == 1" ); cmp_ok( 3.9 <=> 3.9, '==', 0, "3.9 <=> 3.9 == 0" ); cmp_ok( 4 <=> 4, '==', 0, "4 <=> 4 == 0" );

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: note [id://917940]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found