bug-gmp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GMP ceil/floor/trunc & round-offs


From: Hans Aberg
Subject: Re: GMP ceil/floor/trunc & round-offs
Date: Thu, 3 May 2001 11:22:26 +0200

At 11:09 +1000 2001/05/03, Kevin Ryde wrote:
>>   (floating, precision) -> floating
>> where the precision argument specoifies the precision of the function
>> return.
>
>mpf_set_prec does that, more or less.  Or mpf_set, since it uses the
>precision of its target when copying.  I'd doubt the value of having a
>specific rounding direction (ceil/floor/trunc), since all the other
>mpf functions understand only trunc (as it happens).  Exact rounding
>is left to mpfr.

I am aware of that. However, a representation that comes to my mind is to
add a filed which gives the requested precision in bits. Then GMP always
makes the number of limbs sufficiently big to avoid too big computational
errors.

The requested precision is like requesting the number of digits displayed
on a calculator, and mathematically corresponds to this dlog error analysis.

>>      int_ceil, int_floor, int_trunc: rational -> integer
>
>mpz_set_q does the trunc version.  A direct call to mpz_fdiv_q or
>mpz_cdiv_q can be used for the others.
>
>>   frac_ceil, frac_floor, frac_trunc: rational -> rational
>
>I guess that'd be mpq_ceil etc.  That sort of thing is among the many
>operations not directly provided for mpq.  Use of mpq_numref and
>mpq_denref is recommended.

I did implement them in C++ using those operations:

class rational {
  mpq_t value_;
  ...
};

inline integer int_ceil(const rational& x) {
  integer r;
  mpz_cdiv_q(**r.value_, mpq_numref(x.value_), mpq_denref(x.value_));
  return r;
}

inline rational frac_ceil(const rational& x) {
  rational r;
  mpz_cdiv_r(mpq_numref(r.value_), mpq_numref(x.value_), mpq_denref(x.value_));
  mpq_set_den(r.value_, mpq_denref(x.value_));
  return r;
}

inline std::pair<integer, rational> div_ceil(const rational& x) {
  std::pair<integer, rational> r;
  mpz_cdiv_qr(**r.first.value_, mpq_numref(r.second.value_),
    mpq_numref(x.value_), mpq_denref(x.value_));
  mpq_set_den(r.second.value_, mpq_denref(x.value_));
  return r;
}

-- But if one should expand on the GMP mpq interface, I think that these
are some of the functions to be added. (Those functions are rather unlikely
to be inlined by the C++ compiler, as they are too complex. At least, mine
does it not.)

  Hans Aberg





reply via email to

[Prev in Thread] Current Thread [Next in Thread]