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);

In reply to Re: issues using bigrat by ikegami
in thread issues using bigrat by pc88mxer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.