Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Is it a bug in perl6 ? by davido
in thread Is it a bug in perl6 ? by ej8000

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2022-09-29 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (125 votes). Check out past polls.

    Notices?