Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Infinity and Inf?

by ikegami (Pope)
on Aug 31, 2010 at 17:45 UTC ( #858192=note: print w/replies, xml ) Need Help??


in reply to Infinity and Inf?

Range iterator outside integer range

Infinity is a float, and the range operator requires an integer. Both of the following would get you the max int:

use constant MAX_INT => unpack('J', pack('j', -1)); use constant MAX_INT => 0+sprintf('%u', -1);

You could also use a C-style loop:

for (my $_ = 1; ; ++$_)

I tried to define a lazy iteration and I know that I can redefine the constant for my needs

It's not a symbol (i.e., not a constant, a sub or a builtin).

$ perl -e'Inf()' Undefined subroutine &main::Inf called at -e line 1. $ perl -Mstrict -e'Inf' Bareword "Inf" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors.

It's just a string that some underlying C libraries numify to a value representing infinity.

It seems to be a feature of Math::BigInt

It's an feature of the hardware floating point number format. Math::Big* may also support it, but that's unrelated.

Replies are listed 'Best First'.
Re^2: Infinity and Inf?
by DrHyde (Prior) on Sep 01, 2010 at 07:34 UTC
    Both of the following would get you the max int:
    use constant MAX_INT => unpack('J', pack('j', -1)); use constant MAX_INT => 0+sprintf('%u', -1);
    Or with less obfuscation ...
    use constant MAXINT => ~0;
      I wouldn't call it obfuscation, it's simply the Perl spelling of (unsigned int)-1. I didn't realise that the bitwise negation operator (~) returned a UV.
      > Or with less obfuscation ...

      > use constant MAXINT => ~0; Not true on my box..

      DB<1> print ~0 4294967295 DB<2> print ~0+1 4294967296 DB<3> print ~0+2 4294967297 ... DB<12> print ~0*2**18 1.12589990658048e+15 ... DB<15> print ~0*2**17 562949953290240 ... DB<33> p unpack('J', pack('j', -1)); 4294967295 DB<34> p 0+sprintf('%u', -1); 4294967295
      The MAX_INT is somewhere between 2**49 and 2**50!

      UPDATE:

      DB<81> p 999999999999999+1 1e+15 DB<82> p 999999999999998+1 999999999999999 DB<83> p 999999999999998+2 1e+15 DB<84> p 999999999999998+2-1 999999999999999 DB<85> p 1e+15-1 999999999999999
      Seems to depend on the decimal representation of the mantissa of the floating point arithmetics.

      UPDATE:

      to be more precise, integers still work internally for higher numbers even if they can't be represented.

      DB<88> p 999999999999998+4-5 999999999999997 ... DB<96> p 999999999999999+2**52 -2**52 999999999999999 DB<97> p 999999999999999+2**53 -2**53 1e+15

      UPDATE

      Last but not least
      DB<32> $x=0;for(1..2**31) {print;last if $x++>5} Range iterator outside integer range at (eval 36)[/usr/share/perl/5.10 +/perl5db.pl:638] line 2. DB<33> $x=0;for(1..2**30) {print;last if $x++>5} 1234567 DB<34> $x=0;for(1..~0) {print;last if $x++>5} Range iterator outside integer range at (eval 45)[/usr/share/perl/5.10 +/perl5db.pl:638] line 2.
      The range operator seems only to be implemented for signed 32bits integers...so MAX_INT wouldn't even help here!
      DB<46> $x=0;for(1..2**31-2) {print;last if $x++>5} 1234567 DB<47> $x=0;for(1..2**31-1) {print;last if $x++>5} Range iterator outside integer range at (eval 51)[/usr/share/perl/5.10 +/perl5db.pl:638] line 2.

      Cheers Rolf

        You are mistaken. We were talking about the maximum value that can be used in a range, and it's not even close to 2**50.

        (By the way, the largest positive integer that can be stored without loss of precision in a double-precision float is 2**53.)

        I was wrong too, though. I thought it was the largest native unsigned integer, but it's the largest native signed integer.

        $ perl -e'1 for 2147483647..2147483647' $ perl -e'1 for 2147483647..2147483648' Range iterator outside integer range at -e line 1.

        That limit be obtained using the following:

        use constant MAX_RANGE => (~0)>>1;
        The range operator seems only to be implemented for signed 32bits integers

        On a 64-bit perl, it handles integers greater than 32-bit.

        Though there appears to be a bug. Somewhere in between, the 64-bit integers are being transitioned through an NV (double) before being converted back to integers for the iterator it seems:

        for my $p ( 32 .. 64 ) { my $n = 2**$p; printf "\n$p"; printf " $_" for $n .. $n + 2; };; 32 4294967296 4294967297 4294967298 33 8589934592 8589934593 8589934594 34 17179869184 17179869185 17179869186 35 34359738368 34359738369 34359738370 36 68719476736 68719476737 68719476738 37 137438953472 137438953473 137438953474 38 274877906944 274877906945 274877906946 39 549755813888 549755813889 549755813890 40 1099511627776 1099511627777 1099511627778 41 2199023255552 2199023255553 2199023255554 42 4398046511104 4398046511105 4398046511106 43 8796093022208 8796093022209 8796093022210 44 17592186044416 17592186044417 17592186044418 45 35184372088832 35184372088833 35184372088834 46 70368744177664 70368744177665 70368744177666 47 140737488355328 140737488355329 140737488355330 48 281474976710656 281474976710657 281474976710658 49 562949953421312 562949953421313 562949953421314 50 1125899906842624 1125899906842625 1125899906842626 51 2251799813685248 2251799813685249 2251799813685250 52 4503599627370496 4503599627370497 4503599627370498 53 9007199254740992 9007199254740993 9007199254740994 54 18014398509481984 55 36028797018963968 56 72057594037927936 57 144115188075855872 58 288230376151711744 59 576460752303423488 60 1152921504606846976 61 2305843009213693952 62 4611686018427387904 63[Range iterator outside integer range at (eval 21) line 1, <STDIN> l +ine 17.

        Good luck in trying to track that bug down :)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        The MAX_INT is somewhere between 2**49 and 2**50!

        Your test is flawed. The maximum integer that can be accurately stored in a NV is 2**53 = 9007199254740992.

        printf "%.15f\n", 2**53;; 9007199254740992.000000000000000

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Infinity and Inf?
by LanX (Bishop) on Aug 31, 2010 at 17:55 UTC
    > It's not a symbol (not a constant or function).

    hmm strange:

    DB<8> print 5+Inf inf DB<9> use strict;print 5+Inf Bareword "Inf" not allowed while "strict subs" in use at (eval 13)[/us +r/share/perl/5.10/perl5db.pl:638] line 2.

    Cheers Rolf

      How is that weird? Barewords are autoquoted when strict doesn't prevent it.
      $ perl -E'$x=abc; say $x' abc $ perl -E'$x=Inf; say $x' Inf

      Nothing special there. Like I said, only its numification is special.

      $ perl -E'$x=Inf; say 0+$x' inf
        > How is that weird?
        DB<10> print 5+abc 5 DB<11> print 5+Inf inf

        So Inf is a kind of dualvar but not a constant?

        Strange...

        Cheers Rolf

Re^2: Infinity and Inf?
by LanX (Bishop) on Aug 31, 2010 at 18:17 UTC
    > It's not a symbol (i.e., not a constant, a sub or a builtin).

    FYI:

    DB<8> print $::{Inf} *main::Inf

    Cheers Rolf

      $ perl -wle'print $::{Inf}' Use of uninitialized value in print at -e line 1.
      Are you sure you didn't create it (explicitly or via autovivification) earlier in your session?
        maybe!

        Thanks I'm giving up, this feature is useless.

        Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://858192]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-02-19 12:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (264 votes). Check out past polls.

    Notices?