(posted this in the FB Perl group, but I know a lot of you aren't on Fb, so...
Been writing perl since '95. Only found out today that this idiom for coercing a string into an int can fail:
my $x = int($str+0);
It fails when the string begins with nan or inf (case insensitive). So, running this:
my @test = qw(in inf info information INF INFO Na Nan Nanny NAN NANNY +nan04); foreach my $x (@test) { my $y = int($x); printf "%11s -> %s\n", $x, $y; }
outputs:
in -> 0 inf -> Inf info -> Inf information -> Inf INF -> Inf INFO -> Inf Na -> 0 Nan -> NaN Nanny -> NaN NAN -> NaN NANNY -> NaN nan04 -> NaN
It feels like a bug that the regex matches on /^(?:nan|inf).*/s (or \w* - I haven't fully tested it. Rather than matching on /^(?:nan|inf)$/s
Just wondered if this actually is a bug. I've always used Math::BigFloat whenever I need accurate math, so I've never used NaN/Inf in code before.
what do you all think?
In reply to Weird behavior of int() by cLive ;-)
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |