Subject: | is_Int sometimes gives false positives |
Date: | Thu, 29 Aug 2019 14:10:46 +0200 |
To: | bug-Type-Tiny [...] rt.cpan.org |
From: | Curtis Poe <ovid [...] allaroundtheworld.fr> |
Calling 'int" in a number sets the pIOK flag and that causes is_Int to
think floats are ints.
#!/usr/bin/env perl
use Veure::Types 'is_Int';
use Test::Most;
use Devel::Peek;
my $num = 3.14;
ok !is_Int($num), 'Floating point numbers should not report as ints';
Dump $num;
if ( int($num) ) {1}
Dump $num;
ok !is_Int($num), 'Floating point numbers should not report as ints';
explain $num;
done_testing;
The output from the above:
ok 1 - Floating point numbers should not report as ints
SV = NV(0x7f9b5400e198) at 0x7f9b5400e1b0
REFCNT = 1
FLAGS = (NOK,pNOK)
NV = 3.14
SV = PVNV(0x7f9b581dd270) at 0x7f9b5400e1b0
REFCNT = 1
FLAGS = (NOK,pIOK,pNOK)
IV = 3
NV = 3.14
PV = 0
not ok 2 - Floating point numbers should not report as ints
# Failed test 'Floating point numbers should not report as ints'
# at int.pl line 12.
# 3.14
1..2
# Looks like you failed 1 test of 2.
If int($num) is called, is_Int($num) returns true even if the number is not
an integer. I suspect this is because int() sets the pIOK flag, but I
haven't dug in enough to prove it.
Or as a one-liner you can cut-n-paste into your terminal:
$ perl -MTypes::Standard=is_Int -E 'my $num = 3.14; say
is_Int($num)?"Yes":"No"; int($num); say is_Int($num)?"Yes":"No";say $num'
No
Yes
3.14
This is Types::Standard version 1.004004
This is perl 5, version 26, subversion 2 (v5.26.2) built for darwin-2level
Best,
Curtis "Ovid" Poe
--
CTO, All Around the World
World-class software development and consulting
https://allaroundtheworld.fr/