Athanasius has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I’ve been using the exponentiation operator ** in conjunction with the bigint pragma to generate large integral powers, which I need to be exact. After some debugging, I found the following:
#! perl use strict; use warnings; use bigint; print "\nWith two foreach loops:\n\n"; for my $i (12 .. 14) { for my $j (20 .. 22) { print $i ** $j, "\n"; } } print "\nWith an inner C-style for loop:\n\n"; for my $i (12 .. 14) { for (my $j = 20; $j <= 22; ++$j) { print $i ** $j, "\n"; } }
Output:
22:38 >perl 604_SoPW.pl With two foreach loops: 3.83375999244748e+021 4.60051199093697e+022 5.52061438912436e+023 1.90049637748808e+022 2.4706452907345e+023 3.21183887795486e+024 8.36682554252848e+022 1.17135557595399e+024 1.63989780633558e+025 With an inner C-style for loop: 3833759992447475122176 46005119909369701466112 552061438912436417593344 19004963774880799438801 247064529073450392704413 3211838877954855105157369 83668255425284801560576 1171355575953987221848064 16398978063355821105872896 22:38 >
As you can see, the inner foreach loop is somehow cancelling the effect of the use bigint pragma; but converting that loop into a C-style for loop fixes the problem and gives me the output I need.
So, what’s going on? Is this behaviour documented anywhere? Is it a bug?
Thanks,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Strange interaction of bigint and foreach
by moritz (Cardinal) on Apr 11, 2013 at 12:58 UTC | |
by hdb (Monsignor) on Apr 11, 2013 at 13:09 UTC | |
|
Re: Strange interaction of bigint and foreach
by Athanasius (Archbishop) on Apr 11, 2013 at 13:14 UTC | |
|
Re: Strange interaction of bigint and foreach
by salva (Canon) on Apr 11, 2013 at 13:02 UTC |