Hi,

Just wondering: what is the most efficient way to convert a Math::MPFR object into a Math::GMPq. Ideally, without loosing too much precision.
So far, I tried converting Math::MPFR to Math::GMPf, then Math::GMPf to Math::GMPq, which works correctly, but it's not the best solution in terms of efficiency.

Here is the code for that:

use strict; use warnings; use Math::MPFR qw(:mpfr); use Math::GMPf qw(:mpf); use Math::GMPq qw(:mpq); my $PREC = 128; # Compute log(3) my $fr = Rmpfr_init2($PREC); Rmpfr_set_ui($fr, 3, MPFR_RNDN); Rmpfr_log($fr, $fr, MPFR_RNDN); print "$fr\n"; # print as float # MPFR -> floating-point my $f = Rmpf_init2($PREC); Rmpfr_get_f($f, $fr, MPFR_RNDN); # floating-point -> rational my $q = Rmpq_init(); Rmpq_set_f($q, $f); print "$q\n"; # print as rational

I'm pretty sure this can be done better, but just can't figure it how. I would be very happy if someone will point out an easier way for doing this.
Thank you very much and happy holidays!


In reply to Converting Math::MPFR to Math::GMPq by trizen

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.