Skip Menu |

This queue is for tickets about the Number-Closest-NonOO CPAN distribution.

Report information
The Basics
Id: 106827
Status: resolved
Priority: 0/
Queue: Number-Closest-NonOO

People
Owner: Nobody in particular
Requestors: gsullivan [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: result can depend on order of numbers in list
The find_closest_number result can depend on the order of numbers in the list. For example, if the number is 6, and the list contains 5 and 7, the result is 5. But, if the list contains 7 and 5 in that order, the result is 7. Similarly for find_farthest_number. I included a patch to eliminate that inconsistency. I also included a patch for the 01-basics.t test. Refer also to: https://rt.cpan.org/Ticket/Display.html?id=106811
Subject: diff-u-t.txt
--- ../Number-Closest-NonOO-0.03.orig/t/01-basics.t 2013-04-11 22:31:35.000000000 -0400 +++ t/01-basics.t 2015-09-02 12:48:30.638596000 -0400 @@ -19,4 +19,9 @@ is_deeply(find_closest_number(number=>"-inf", numbers=>["-inf", -2, -1, 0, 1, 2, "inf"], inf=>"number", items => 10), ["-inf", -2, -1, 0, 1, 2, "inf"], "inf=number 2"); +is(find_closest_number (number=>6, numbers=>[5, 7]), 5, "close order 1"); +is(find_closest_number (number=>6, numbers=>[7, 5]), 5, "close order 2"); +is(find_farthest_number(number=>6, numbers=>[5, 7]), 7, "far order 1"); +is(find_farthest_number(number=>6, numbers=>[7, 5]), 7, "far order 2"); + done_testing;
Hi Gene, Thanks for the report. It seems to be missing the mentioned patch (there is only one for the test, none for the library code itself). On Wed Sep 02 13:12:19 2015, GSULLIVAN wrote: Show quoted text
> The find_closest_number result can depend on the order of numbers in > the list. > > For example, if the number is 6, and the list contains 5 and 7, the > result is 5. > But, if the list contains 7 and 5 in that order, the result is 7. > Similarly for find_farthest_number. > > I included a patch to eliminate that inconsistency. I also included a > patch > for the 01-basics.t test. > > Refer also to: > https://rt.cpan.org/Ticket/Display.html?id=106811
Strange. I looked like I uploaded both files. Here's the .pm patch attempt #2...
Subject: diff-u-pm.txt
--- ../Number-Closest-NonOO-0.03.orig/lib/Number/Closest/NonOO.pm 2013-04-11 22:31:35.000000000 -0400 +++ lib/Number/Closest/NonOO.pm 2015-09-02 12:48:16.855765000 -0400 @@ -58,7 +58,7 @@ #use Data::Dump; dd \@res; @res = sort {$a->[1] <=> $b->[1] || $a->[2] <=> $b->[2]} @res; } else { - @res = sort {$a->[1] <=> $b->[1]} map {[$_, abs($_-$num)]} @nums; + @res = sort {$a->[1] <=> $b->[1]} map {[$_, abs($_-$num)]} sort {$a <=> $b} @nums; } @res = map {$_->[0]} @res;
On Wed Sep 02 13:35:34 2015, GSULLIVAN wrote: Show quoted text
> Strange. I looked like I uploaded both files. Here's the .pm patch > attempt #2...
Ok thanks for the patch. I prefer to do a single sort, plus your patch doesn't yet handle the case of inf=number, so here's what I did: ------------------ Author: perlancar (@pc-office) <perlancar@gmail.com> Date: Thu Sep 3 01:16:44 2015 +0700 Consistent ordering [problem reported by Gene Sullivan] diff --git a/lib/Number/Closest/NonOO.pm b/lib/Number/Closest/NonOO.pm index 73cfb8e..bdf04a8 100644 --- a/lib/Number/Closest/NonOO.pm +++ b/lib/Number/Closest/NonOO.pm @@ -62,9 +62,11 @@ sub _find { $m; } @nums; #use Data::Dump; dd \@res; - @res = sort {$a->[1] <=> $b->[1] || $a->[2] <=> $b->[2]} @res; + @res = sort {$a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] || $a->[0] <=> $b->[0] } + @res; } else { - @res = sort {$a->[1] <=> $b->[1]} map {[$_, abs($_-$num)]} @nums; + @res = sort {$a->[1] <=> $b->[1] || $a->[0] <=> $b->[0]} + map {[$_, abs($_-$num)]} @nums; } @res = map {$_->[0]} @res; diff --git a/t/01-basics.t b/t/01-basics.t index 4c77430..c5930eb 100644 --- a/t/01-basics.t +++ b/t/01-basics.t @@ -19,4 +19,18 @@ is_deeply(find_closest_number(number=> "inf", numbers=>["-inf", -2, -1, 0, 1, 2, is_deeply(find_closest_number(number=>"-inf", numbers=>["-inf", -2, -1, 0, 1, 2, "inf"], inf=>"number", items => 10), ["-inf", -2, -1, 0, 1, 2, "inf"], "inf=number 2"); +# ordering tests +is(find_closest_number (number=>6, numbers=>[5, 7]), 5, "close order 1"); +is(find_closest_number (number=>6, numbers=>[7, 5]), 5, "close order 2"); +is(find_farthest_number(number=>6, numbers=>[5, 7]), 7, "far order 1"); +is(find_farthest_number(number=>6, numbers=>[7, 5]), 7, "far order 2"); +is_deeply(find_closest_number(number=> 6, numbers=>["-inf", 5, 7, "inf"], inf=>"number", items => 4), + [5, 7, "-inf", "inf"], "close order 1 (inf=number)"); +is_deeply(find_closest_number(number=> 6, numbers=>["-inf", 7, 5, "inf"], inf=>"number", items => 4), + [5, 7, "-inf", "inf"], "close order 2 (inf=number)"); +is_deeply(find_farthest_number(number=> 6, numbers=>["-inf", 5, 7, "inf"], inf=>"number", items => 4), + ["inf", "-inf", 7, 5], "far order 1 (inf=number)"); +is_deeply(find_farthest_number(number=> 6, numbers=>["-inf", 7, 5, "inf"], inf=>"number", items => 4), + ["inf", "-inf", 7, 5], "far order 2 (inf=number)"); + done_testing; ------------------ Released 0.04, thanks!