in reply to Out of memory.

Hi, I don't think that there is a real out-of-memory problem, but surely a problem with accuracy. Sorry, I am too tired to decode your program right now, but it looks a little bit C-ish to me. Something more terse and Perlish to start from might be:

#!/usr/bin/perl use strict; chomp (my $equation = <DATA>); my @stack; foreach ( split(/\s*,\s*/, $equation) ) { push(@stack,$_), next if /\d+/; die "stack underrun" if @stack < 2; my ($val1, $val2) = (pop(@stack), pop(@stack)); push(@stack, $val1 + $val2), next if $_ eq '+'; push(@stack, $val1 * $val2), next if $_ eq 'x'; die "undef. operator ($_)"; } print "Result (stack): ", join(", ", @stack), "\n"; __DATA__ 1,2,3,4,+,5,+,x,6, ...... x,x,x,x,x,9164,x,+,9165,9166,91 <--- you +r input as a single line Result (stack): 1, 144, 1.9792773969506e+27, 6.22437334578068e+26, 5.7 +7209920038837e+273, 8966, 4.97957520221034e+261, 9165, 9166, 91
Seems that the equation is not complete? Furthermore, as you can see, the usual built-in precision might not be sufficient for your purpose. So you should have a look at e.g. Math::BigInt too.

Replies are listed 'Best First'.
Re^2: Out of memory.
by dneedles (Sexton) on Oct 22, 2008 at 01:22 UTC
    I believe the equation got truncated. Precision isn't an issue since it is a symbolic manipulation. The numbers represent variables (lines in an excel file) and are not actual numbers. Is there a package that handled symbolic algebra. I basically need to multiply these variables through in order to get a list of independent vars. For example: (a+b)*(c+d) or ab+cd+* would turn into ac+ad+bc+bd. If not, is there a package to page parts of an array to a file or someother manipulation to get around the memory issue. I printed out the array before the join and the array was around 1/2 a gig. What is the byte size limit on a string?

      Hm. I misunderstood your question, thinking that the digits compose concrete numbers instead of symbols. Well, I think that BrowserUk has already provided a solution that probably suits your problem better than a generic Perl module for symbolic algebra. If you are still interested in such a module you could search CPAN for symbolic and/or algebra or perform a Super Search. This also sounds interesting: recommendations on scientific computing with Perl.

      Maximum string size in Perl: AFIK, its only limited by your OS (e.g. 32bit/64bit) and your hardware (memory, disk). Experimentally, my Perl 5.8.4 on a 64bit Solaris 10 box (8GB) was able to create a string of at least 3945 MB (1MB = 1024 x 1024) before out-of-memory occurred.

      Right, the equation got truncated. If you like, please surround your sample equation with <readmore>...</readmore> tags - although it won't fix the truncation effect, it will make this thread more readable.

        As of yet I have not found a solution. My next guess is to page part of the equation to a database as a way to externalize the problem. Thanks for your input. I am unclear why it is getting "Out of Memory!" I agree with your assessment that it should be.