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

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

Replies are listed 'Best First'.
Re^4: Weird behavior of int()
by cLive ;-) (Prior) on May 27, 2024 at 15:53 UTC

    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