BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

Using a 64-bit build of perl 5.18:

Given:

C:\test>\perl5.18\perl\bin\perl.exe -E"printf qq[%u\n], 2**63" 9223372036854775808

And this fails:

C:\test>\perl5.18\perl\bin\perl.exe -E"1 for 1 .. 9223372036854775808" Range iterator outside integer range at -e line 1.

And this succeeds:

C:\test>\perl5.18\perl\bin\perl.exe -E"1 for 1 .. 9223372036854775808 +- 1"

Why does this fail?:

C:\test>\perl5.18\perl\bin\perl.exe -E"1 for 1 .. 2**63 - 1" Range iterator outside integer range at -e line 1.

And this fail?:

C:\test>\perl5.18\perl\bin\perl.exe -E"1 for 1 .. 2**63 - 512" Range iterator outside integer range at -e line 1.

But this succeed?:

C:\test>\perl5.18\perl\bin\perl.exe -E"1 for 1 .. 2**63-513"

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re: Range iterator outside integer range ... Why?
by Anonymous Monk on Jan 16, 2015 at 16:02 UTC

    $ perl -e "print int ((1<<63)-512)" 9223372036854775296 $ perl -e "print int ((2**63)-512)" 9223372036854775808

      So, if I'm interpreting your (rather terse) message correctly, because 2**63 is greater than a signed 64-bit int, it gets coersed into a double; before the subtraction is done.

      And because 63-bits of precision is greater than the 53 bits the double can retain, by the time the subtraction is done, the loss of precision causes the problem.

      In anycase, 1<<63-1 is a much nicer way of writing that constant. Thanks!


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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. Agile (and TDD) debunked

        Or, as stated by oiskuu here, it's because 2**63 is implemented with pow, when 1<<63 actually works on integers.