in reply to issues using bigrat
Why didn't bigrat figure out that the expression $k/($n-$k) should produce a bigrat and not a floating point number?
The only things that get converted to big num objects are constants. Everything else is done through operator overload, which only affects operators whose operands are big num objects.
However, the range operator cannot be overloaded, so using a bigXXX as an operand to a range operator collapses it into a plain integer.
use bigrat; $i = 2; print("i: ", ref($i), "\n"); ($j) = ($i .. $i); print("j: ", ref($j), "\n");
i: Math::BigInt j:
Since you converted the constants from big num objects into plain numbers, you needed to convert them back to big num objects to get the desired output.
You can continue to explicitly convert the loop counters to big nums (using "1 *", "1/1 *" is overkill), or you can use a C-style loop instead of using the range operator.
use bigrat; for (my $n=1; $n<=30; ++$n) { my $s = 0; for (my $k=1; $k<$n; ++$k) { $s += $k / ($n-$k); } print "n = $n, s = $s\n"; }
Why can't I call numerator and denominator on a rational that's actually an integer?
You can't call a Math::BigRat method on something that's not a Math::BigRat. Nowhere does bigrat say it produces Math::BigRat objects. It actually says "Integer and floating-point constants are created as proper BigInts or BigFloats". If you want a Math::BigRat object, you'll need to create it.
$x = Math::BigRat->new($x);
|
|---|