IIRC the range operator works internally only on unsigned integers, i.e. 32 bits or 64 bits depending on your compilation. I suppose bigint doesn't influence it.

I'll search and update a similar discussion we had about this.

UPDATE: here it is Infinity and Inf?

IMHO a bug or at least a reasonable feature request.

Cheers Rolf

UPDATE:

A two part answer to your question:

Part1:

As I thought, the range operator only returns "normal" integers, and 145**145 causes an overflow and becomes INF.

But copying to another var helps.

my $answer = 1; for my $j ( 1 .. 999 ) { print "$i: $answer\n"; $i=$j+0; # $i is now bigint $answer += $i ** $i; }

For the records, I didn't say brute force is the best way to solve an Euler question... ;)

Part2:

The second part of the answer is that foreach doesn't do a scalar assignment to $i, where bigint could intercept by auto-blessing $i into Math::BigInt. Foreach makes $i an alias of the integer values from the range, such that the **-operator isn't overloaded and produces an overflow.

So the only way to solve this is to make the range operator operating on big-ints on demand.


In reply to Re: c-style for loop versus list for loop, and bigint by LanX
in thread c-style for loop versus list for loop, and bigint by AR

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.