So far, I tried converting Math::MPFR to Math::GMPf, then Math::GMPf to Math::GMPq, which works correctly

I think that's about the best you can do - ie, I can't spot anything better.
The mpfr library provides direct conversion to mpf_t and mpz_t types (using the mpfr_get_f and mpfr_get_z functions), but the mpfr_get_q function is not there - and hence no Rmpfr_get_q function in Math::MPFR.

It would be nice to avoid the overhead of having to load Math::GMPf but, other than that, I think your conversion should be working quite efficiently.

For 128 bits of precision, the value of the mpfr_t and mpf_t will be exactly equivalent, and the final mpq_t will be an exact representation of that floating point value.

Belated update: That last statement of mine is poorly thought out - the value of the mpfr_t and mpf_t will be exactly equivalent if the precision allocated to the mpfr_t equals the precision allocated to the mpf_t (irrespective of whether the precision is 128 or not).

Cheers,
Rob

In reply to Re: Converting Math::MPFR to Math::GMPq by syphilis
in thread 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.