gmp 4.1 documentation improvement suggestions
Serge Winitzki
serge@cosmos.phy.tufts.edu
Sun, 8 Dec 2002 19:24:43 -0500 (EST)
On Mon, 9 Dec 2002, Kevin Ryde wrote:
> >> The mantissa in stored in binary, as might be imagined from the fact
> >
> > The exponent is also stored essentially in binary and it would seem easy
> > to find the binary exponent of a given number. This function seems very
> > useful for calculations and right now I'm using mp?_get_d_2exp to get
> > the binary exponent. My question is: are the functions mp?_get_d_2exp
> > efficient (constant-time) or not, and if not, what other function can I
> > use?
>
> Both get_d_2exp's are quite good I think.
>
> mpz_sizeinbase(z,2) probably does what you want for integers. I guess
> some sort of mpf_log2floor (or is it ceil?) would be the equivalent
> for an mpf_t.
Yes, it would be ceil rather than floor.
I decided to use mpz_get_d_2exp instead of mpz_sizeinbase because there
wasn't an mpf_sizeinbase and because it seemed that mpz_sizeinbase is
not as efficient since needs to give me the size in any base, not just
in base 2. Maybe I'm wrong and mpz_sizeinbase is optimized for base 2;
in this case I'd rather use mpz_sizeinbase, of course. Base 2 seems
special because the integer part of the logarithm in base 2 is useful in
many calculations. mpz_get_d_2exp also computes the double value but
this is hopefully not a big slowdown.
Another issue I wanted to mention is related to precision control in
calculations. Suppose we are using functions such as mpf_add(x,x,y);
by default the target precision is taken from the first argument "x", so
"x+y" is evaluated to the same precision as "x". However there are
situations when the precision of "x" is lower than the precision of the
result, for example, if x=0.001 (one significant bit) and y=1.000 (four
significant bits). It is not obvious to a beginner GMP user that this
situation will sometimes arise and lead to a loss of precision. Of
course, a solution is to use mpf_add(y,y,x) or to explicitly promote x
to a higher precision.
I've been using GMP 4.0.1 and just found out that GMP 4.1 has a new mpfr
facility that incorporates precision tracking. However, I couldn't find
documentation about how the precision tracking is done, and reading the
source code is too time-consuming for me. I just finished drafting my
own simple precision tracking implementation for our Yacas project,
without looking at mpfr or at another implementation, and it would be
interesting to understand how it works in mpfr. Are there any documents
or papers that describe it?
--
Serge