my $idx = binsearch { $a <=> $b } $val, @array; if ($idx < 0) { $idx = ~$idx; --$idx if $idx > 0 && $val - $array[$idx-1] <= $array[$idx] - $val; }