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

I am writing some perl which is giving me an error that I do not understand.

# x is 2 to the 128th my $x = Math::BigFloat->new(q(2.0)); $x->bpow(q(128)); # ...more stuff... # $product starts as x! $product = $x->copy(); $product->bfac();

The error I get is

Can't use an undefined value as an ARRAY reference at /System/Library/Perl/5.8.6/Math/BigInt/Calc.pm line 899 (#1) (F) A value used as either a hard reference or a symbolic referenc +e must be a defined value. This helps to delurk some insidious errors. Uncaught exception from user code: Can't use an undefined value as an ARRAY reference at /System/Libr +ary/Perl/5.8.6/Math/BigInt/Calc.pm line 899. at /System/Library/Perl/5.8.6/Math/BigInt/Calc.pm line 899 Math::BigInt::Calc::_acmp('ARRAY(0x18690a0)', 'ARRAY(0x18690d0)') +called at /System/Library/Perl/5.8.6/Math/BigInt/Calc.pm line 1304 Math::BigInt::Calc::_fac('Math::BigInt::Calc', 'ARRAY(0x1868fc8)') + called at /System/Library/Perl/5.8.6/Math/BigFloat.pm line 1789 Math::BigFloat::bfac('Math::BigFloat=HASH(0x1868f5c)') called at r +epeat.pl line 19

I have googled and not found this question asked before. Can anyone explain what is going wrong? Thanks.

Replies are listed 'Best First'.
Re: undefined value as an ARRAY
by BrowserUk (Patriarch) on Aug 12, 2010 at 03:28 UTC

    You're probably running out of memory!

    According to Wolfram/Alpha, 2**128! is 101040.11273643527576 and has 85070591730234615865843651857942052838 trailing zeros.

    As such, it would require more memory than will ever exist to calculate.


    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: undefined value as an ARRAY (Math::BigFloat)
by toolic (Bishop) on Aug 12, 2010 at 02:07 UTC
    I can't answer your question directly, but it seems as though you have either uncovered a bug or you have exceeded some not-so-obviously specified limit of the bfac function of Math::BigFloat. It seems the maximum value for this function is between 2**23 and 2**24. This does not generate the error:
    use strict; use warnings; use diagnostics; use Math::BigFloat; my $x = Math::BigFloat->new(2**23); print "x=$x\n"; my $product = $x->copy(); $product->bfac(); print "p=$product\n";

    You could submit a bug report on CPAN. But, before doing so, you could use Super Search to see if anyone has encountered similar issues with this module.

    I tried Math::BigInt as well, but I get the same results.

Re: undefined value as an ARRAY
by graff (Chancellor) on Aug 12, 2010 at 02:38 UTC
    Curiously, when I run the following command-line script on my mac (to replicate the essentials of your code), the "bfac" call apparently does not return, and the process runs in a seemingly infinite loop, chewing up cpu time:
    perl -MMath::BigFloat -e '$|++;$x=Math::BigFloat->new("2.0"); print "\ncalling bpow at ",scalar(localtime)."\n";$x->bpow("128"); print "calling copy at ",scalar(localtime)."\n";$p=$x->copy(); print "calling bfac at ",scalar(localtime)."\n";$p->bfac(); print "finished at ",scalar(localtime)."\n"' calling bpow at Wed Aug 11 21:44:28 2010 calling copy at Wed Aug 11 21:44:28 2010 calling bfac at Wed Aug 11 21:44:28 2010
    (still running an hour later... oh well, I'm just going to kill it now.) FWIW, I'm using the version that comes with "snow leopard" (osx 10.6.4):
    This is perl, v5.10.0 built for darwin-thread-multi-2level
    When you ran your script, did it crash right away, or did it run for a long time before crashing?

    (Updated my command-line script to include the (rather important) first line of the command.)

      I suspect you'd have to wait a bit more than an hour. Given this:

      #!/usr/bin/perl -w use strict; use Benchmark qw/:all/; use Math::BigFloat; cmpthese( 1, { '2**10' => sub { my $x = Math::BigFloat->new(2**10); my $prod +uct = $x->copy(); $product->bfac(); }, '2**11' => sub { my $x = Math::BigFloat->new(2**11); my $prod +uct = $x->copy(); $product->bfac(); }, '2**12' => sub { my $x = Math::BigFloat->new(2**12); my $prod +uct = $x->copy(); $product->bfac(); }, '2**13' => sub { my $x = Math::BigFloat->new(2**13); my $prod +uct = $x->copy(); $product->bfac(); }, '2**14' => sub { my $x = Math::BigFloat->new(2**14); my $prod +uct = $x->copy(); $product->bfac(); } });

      I get run times like these:

      s/iter 2**14 2**13 2**12 2**11 2**10 2**14 139 -- -78% -97% -99% -100% 2**13 31.3 345% -- -88% -97% -99% 2**12 3.88 3488% 706% -- -78% -95% 2**11 0.870 15902% 3493% 346% -- -76% 2**10 0.210 66195% 14786% 1748% 314% --

      With that kind of progression, 2**128 is, um, likely to take a while. :)


      --
      "Language shapes the way we think, and determines what we can think about."
      -- B. L. Whorf
      When you ran your script, did it crash right away, or did it run for a long time before crashing?
      Another data point: I get the error immediately. When I try smaller numbers, I do not get the error, but it does seem to take a while.
      This is perl, v5.8.8 built for MSWin32-x86-multi-thread
        If your script works for smaller numbers then it's probably a bug in either Math::BigFloat itself or in a library it uses .

        If it is an option for you try it on another system or with a newer version of Perl.