I'm encountering a case when minmax() sometimes returns an undef max. So far I haven't been able to create a simpler test case which reproduces this problem, but this happens when I'm testing my Bencher module with Bencher::Scenario::AcmePERLANCARTestPerformance:
% bencher -m AcmePERLANCARTestPerformance Use of uninitialized value $max in numeric le (<=) at lib/Bencher/Form +atter/ScaleTime.pm line 28.
The offending code is (starting from line 25 in ScaleTime.pm):
my ($min, $max) = minmax(map {$_->{time}} @{$envres->[2]}); my ($unit, $factor); if ($max <= 1.5e-6) {
Dumping the variables shows that minmax() is getting (0.002) (a single-element list with the element value of 0.002). The result from minmax is (0.002, undef).
This does not happen when the input list is of different value, e.g.: (0.0023), (0.0019), (0.0022), and so on. Only when the value is (0.002) on my computer.
This does not happen when I change the above code into:
my @list = map {$_->{time}} @{$envres->[2]}; my ($min, $max) = minmax(@list); ...
This does not happen when I try to reproduce this in a simple test program, using the same value of $envres.
This does not happen if I use the List::MoreUtils::PP backend (by setting LIST_MOREUTILS_PP environment variable to 1).
Any clue?
UPDATE 1: I'm now able to create a simple test to reproduce the bug. This is still List::MoreUtils 0.413. It seems related to floating point rounding in sprintf() (e.g. using %f or %g format).
% perl -MList::MoreUtils=minmax -MData::Dump -e'for(1..20) { my ($min, + $max) = minmax(sprintf("%.4g", rand())); dd ($min, $max) }' (0.9403, 0.9403) (0.2669, 0.2669) (0.4618, 0.4618) (0.6728, 0.6728) (0.829, undef) (0.6572, 0.6572) (0.7323, 0.7323) (0.521, undef) (0.03817, 0.03817) (0.9032, 0.9032) (0.8139, 0.8139) (0.8573, 0.8573) (0.9723, 0.9723) (0.7832, 0.7832) (0.7387, 0.7387) (0.06714, 0.06714) (0.127, undef) (0.6433, 0.6433) (0.02692, 0.02692) (0.157, undef)
I'm able to reproduce this on perl 5.22.1, 5.22.0, 5.20.3. perls 5.18.4 and earlier don't seem to exhibit this bug.
UPDATE 2 (2016-03-17): Bug submitted to List-MoreUtils: https://rt.cpan.org/Ticket/Display.html?id=113117 . Thanks to everyone for the responses.
In reply to List::MoreUtils' minmax bug? by perlancar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |