in reply to big integers forcing me to be a C programmer: "range iterator outside integer range"

... the C-style loop allows it only when I use bignum (but not Math::BigInt)

You can use the C-style loop with Math::BigInt, but you just have to create $x in a slightly different way - eg:
use warnings; use strict; use feature 'say'; use Math::BigInt; my $x = Math::BigInt->new(1); $x <<= 1024; for(my $i = $x; $i <= $x + 2; $i++) { say $i; }
The C-style loop works with both bignum and Math::BigInt because it makes use of operators that are overloaded. Unfortunately, the '..' operator is not currently overloaded, and that's why the second loop fails.

Cheers,
Rob
  • Comment on Re: big integers forcing me to be a C programmer: "range iterator outside integer range"
  • Download Code

Replies are listed 'Best First'.
Re^2: big integers forcing me to be a C programmer: "range iterator outside integer range"
by ikegami (Patriarch) on May 07, 2009 at 11:20 UTC

    but you just have to create $x in a slightly different way

    No you don't. The direct equivalent works just fine.

    >perl -MMath::BigInt -le"my $x = Math::BigInt->new(2) ** Math::BigInt- +>new(1024); print $x" 1797693134862315907729305190789024733617976978942306572734300811577326 +758055009631327084773224075360211201138798713933576587897688144166224 +928474306394741243777678934248654852763022196012460941194530829520850 +057688381506823424628814739131105408272371633505106845862982399472459 +38479716304835356329624224137216

    It can even be simplified

    >perl -MMath::BigInt -le"my $x = Math::BigInt->new(2) ** 1024; print $ +x" 1797693134862315907729305190789024733617976978942306572734300811577326 +758055009631327084773224075360211201138798713933576587897688144166224 +928474306394741243777678934248654852763022196012460941194530829520850 +057688381506823424628814739131105408272371633505106845862982399472459 +38479716304835356329624224137216
      The direct equivalent works just fine

      I was thinking that the "direct equivalent" would be:
      my $x = Math::BigInt->new(2**1024);
      which doesn't work quite so well :-)

      Cheers,
      Rob
        bignum converts every numeric literal into a Math::Big* object. That would even include the "2" in the for loop, although that one can also be skipped.