in reply to issues using bigrat
I read the problem description you gave (summing the series), and wrote a "1-liner" (iterative 'bashing') which seems to work exactly as you describe. I am using the ".." operator just as you are, so I don't think you need to use the "C" style "for" loop. Not yet sure why things aren't working the same for you (am using 5.8.8, here, as well). Original "1-liner format:"
Or pretty-i-fied:perl -e 'use bigrat;for my $n (1..5){my $s=0;for my $i (1..$n) {$s+=($ +n+1-$i)/$i} push @nums, $s->numerator; push @dens, $s->denominator; $ +num=$s->numerator; $den=$s->denominator; print "n = $n, s = $s, num=$ +num, den=$den\n";} print "nums=",(join ", ",@nums), "\n"; print "dens +=",(join ", ",@dens),"\n";'
Did you convert some 'bignum' type to an integer somewhere? I'll have to study your code more closely to find out where it's breaking, but thought I'd at least offer something that seemed to do what you wanted in the way you wanted...:-)#!/usr/bin/perl -w use bigrat; for my $n (1 .. 5) { my $s = 0; for my $i (1 .. $n) {$s += ($n + 1 - $i) / $i} push @nums, $s->numerator; push @dens, $s->denominator; $num = $s->numerator; $den = $s->denominator; print "n = $n, s = $s, num=$num, den=$den\n"; } print "nums=", (join ", ", @nums), "\n"; print "dens=", (join ", ", @dens), "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: issues using bigrat
by ikegami (Patriarch) on May 11, 2008 at 21:08 UTC | |
by perl-diddler (Chaplain) on May 11, 2008 at 21:47 UTC | |
by ikegami (Patriarch) on May 11, 2008 at 22:15 UTC |