Scalar::Util::Numeric#

📦 std

Numeric tests for scalars: is a value a number, and what kind.

Each function asks perl’s own number parser (grok_number, the one looks_like_number uses) about the string form of its argument, so a string and a number answer alike: isint("42") and isint(42) are both true.

Synopsis#

use Scalar::Util::Numeric qw(isnum isint isfloat isneg);

print isnum("3.5e2") ? "number\n" : "not\n";   # number
print isint(" 7 "), "\n";                      # 1
print isint("-7"), "\n";                       # -1
print isfloat("1e3"), "\n";                    # 1
print isnum("abc"), "\n";                      # 0

Functions#

isnum($v) is grok_number’s answer: 0 for a non-number, otherwise a set of flags - 0x01 the value fits a UV, 0x02 it is larger, 0x04 it has a fraction or an exponent, 0x08 it is negative, 0x10 infinity, 0x20 NaN. isint is 1 for a non-negative integer, -1 for a negative one and 0 otherwise; isuv, isbig, isfloat, isneg, isinf and isnan each test one flag and return 1 or 0. uvmax() is the largest UV. undef is not a number. Nothing is exported by default; :all exports the eight tests.

This is a native implementation of Scalar::Util::Numeric 0.40’s Numeric.xs; its Scalar/Util/Numeric.pm is used unchanged.

Differences from upstream#

None.

Functions#

Numeric tests#

uvmax#

The largest unsigned integer perl holds natively.

Synopsis

my $max = Scalar::Util::Numeric::uvmax();   # 18446744073709551615

isnum#

Whether a value is a number, and which kind.

isint#

Whether a value is an integer, and its sign.

isuv#

Whether a value is a number that fits an unsigned integer.

isbig#

Whether a value is a number larger than the unsigned range.

isfloat#

Whether a value is a number with a fraction or an exponent.

isneg#

Whether a value is a negative number.

Synopsis

use Scalar::Util::Numeric qw(isneg);
isneg("-3");   # 1

What you get back

1 when the number has a leading minus sign, else 0.

isinf#

Whether a value is infinity.

isnan#

Whether a value is NaN.

Synopsis

use Scalar::Util::Numeric qw(isnan);
isnan("NaN");   # 1

What you get back

1 for a NaN, in any spelling perl reads as NaN, else 0.