in reply to Re: Weird behavior of int()
in thread Weird behavior of int()

There's no practical issue in normal code, but it behaves in a way that feels unintuitive. I have never used NaN or Inf in code, and never would. I would be using Math::BigFloat for anything that odd.

I guess the part that I don't like is that NaN and Inf are valid return values, more than anything. Neither are integers - though I also realize we can't change how int works, it feels very unintuitive.

The +0 was a quick hack I would use to coerce a string into a number - in lieu of $num =~ /^\d+$/ or $num=0 for untainting - I didn't know Inf and Nan were valid integers in Perl until last week (one of those things I thought I knew but had just never encountered - this is up there with when I first head about using _ in code :D

I can live with NaN+0=NaN<code> and <code>Inf+0=Inf, but I really do think int('information') should be zero rather than Inf!

Replies are listed 'Best First'.
Re^3: Weird behavior of int()
by syphilis (Archbishop) on May 27, 2024 at 02:18 UTC
    I can live with NaN+0=NaN and Inf+0=Inf..

    It's just a matter of understanding the process.
    If you can accept that '42.31' + 0 is 42.31 and '42.31ormation' + 0 is 42.31 and 'inf' + 0 is Inf, then it makes perfect sense that 'information' + 0 is Inf.
    Then, once you understand that the int() function merely chops off the fractional part of its argument, there's not much left to puzzle over, AFAICS.

    This process that perl is using to numify strings is the same as that used by the strtod() function in the C programming language.
    I think it's a fairly standard process across many programming languages.
    It's not about to change any time soon.

    Cheers,
    Rob

      Look at the official documentation:

      "Returns the integer portion of EXPR"

      That's incorrect and vague

      • NaN and Inf are not integers
      • What does 'integer portion' actually mean? It's not defined, so one could read that to mean that the integer portion of eee5 is 5

      At the very least, this documentation needs fixing - even if the underlying code is not getting touched.

        NaN and Inf are not integers

        That's true - for completeness, the documentation should probably specify that int($x) returns $x, as evaluated in numeric context, if $x (as evaluated in numeric context) is either NaN or +Inf or -Inf or has no fractional part.

        What does 'integer portion' actually mean? It's not defined, so one could read that to mean that the integer portion of eee5 is 5.

        Maybe it should be specified that EXPR is first evaluated in numeric context, and the integer portion of that derived value is returned.
        But then, neither the sin() nor cos() nor log() nor exp() nor sqrt() documentations specify that EXPR is to be evaluated in numeric context.
        Do those docs need to make the same clarification ?

        One might argue that it's somehow already clear that those math functions will evaluate their args in numeric context, but that it's not immediately apparent that int() will do the same.
        I don't think I'd agree with that argument ... nor would I necessarily disagree with it ... dunno ;-)

        You should present you point of view at https://github.com/Perl/perl5/pull/22227.

        Cheers,
        Rob