trizen has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Converting Math::MPFR to Math::GMPq
by syphilis (Archbishop) on Dec 28, 2015 at 13:08 UTC | |
by trizen (Hermit) on Dec 28, 2015 at 14:05 UTC | |
by syphilis (Archbishop) on Dec 29, 2015 at 01:46 UTC | |
by trizen (Hermit) on Dec 29, 2015 at 05:24 UTC |