mpz_mod inside loop

Vincent Lefevre vincent at vinc17.net
Fri May 17 12:44:02 UTC 2019


On 2019-05-17 08:37:30 -0400, Richard Damon wrote:
> On 5/17/19 8:17 AM, Torbjörn Granlund wrote:
> > I suppressed one line which made my suggested shift-up solution hard to
> > read.  Here is more complete code, with assignment of s:
> >
> >   unsigned long s = mpz_sizeinbase (a, 2);
> >   mpz_mul_2exp (a, a, (GMP_NUMB_BITS - s) % GMP_NUMB_BITS);
> >
> > Of course, this is equivalent to (-s) % GMP_NUMB_BITS as GMP_NUMB_BITS
> > divides GMP_NUMB_BITS...
> >
> Not quite. -1 % 64 = -1 not 63, as -1 / 64 == 0 so the remainder is
> negative, and division is defined as truncation (towards zero) not
> flooring (towards minus infinity)

No, s is an unsigned long, thus this is an unsigned integer division,
i.e. with flooring.

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the gmp-discuss mailing list