(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 ;-)

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.