The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Number::Closest::XS - find numbers closest to a given

VERSION

This document describes Number::Closest::XS version 0.09

SYNOPSIS

    use Number::Closest::XS qw(:all);
    my $res = find_closest_numbers($num, \@list, $amount);
    my $res2 = find_closest_numbers_around($num, \@list, $amount);

DESCRIPTION

Module provides functions to extract from the list numbers closest to the given.

SUBROUTINES

find_closest_numbers($num, \@list, [$amount])

selects from the @list up to $amount numbers closest to the $num. If $amount is not specified, is assumed to be 1. Returns reference to the array containing found numbers sorted by the distance from the $num. The sort is stable, so the numbers that have the same distance to the $num will be present in the result in the same order as in the @list. Distance between $num and $x computed as abs($num - $x).

find_closest_numbers_around($num, \@list, [$amount])

selects from the @list up to $amount numbers closest to the $num. Tries to select equal amounts of numbers from both sides of the $num, but if there are not enough numbers from one side will select more numbers from the other. If $amount is odd, then the last number will be closest to $num, e.g. if $num is 5, @list is 1, 2, 6, 7, and amount is 3 it will return 2, 6, and 7, because 7 is closer to 5 than 1. If $amount is not specified, is assumed to be 2. Returns reference to the array containing closest numbers sorted by their values.

SEE ALSO

Number::Closest, Number::Closest::NonOO

CAVEATS

Internally module coerses all numbers into double or long double depending on perl compilation options. If perl was compiled with 64 bit integer support and underlying platform does not support long double data type then during conversion of integers into double some significant digits may be lost and module may produce incorrect results. This only happens for integers that have more significant bits than fraction part of the double (usually 52 bit). The problem is known to be present on NetBSD and Windows if perl was compiled with Microsoft compiler.

AUTHOR

Pavel Shaydo <zwon at cpan.org>

THANKS

Thanks to Dana Jacobsen for reporting a bug with handling 64 bit integers and long doubles.

LICENSE AND COPYRIGHT

Copyright (C) 2014 Pavel Shaydo

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.