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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |