about DivideAndRemainder functionnality

Paul Zimmermann Paul.Zimmermann at loria.fr
Mon Jan 28 15:19:31 CET 2008


> Hello,
> I'm not a expert with mathematic, so I come to you because I need to 
> find the divider and the remainder of 2 numbers, like the Java 
> BigInteger.DivideAndRemainder() do. I do not know wich GMP's divide 
> function to use :
> 
> To get the remainder I think I've just to call mpz_mod() but I do not 
> know the divide function : cdiv or fdiv or tdiv or another ?
> 
> Thanks for your help
> Cyrille.

mpz_mod returns the classical remainder, i.e., in [0, |D|-1].

The difference between the cdiv/fdiv/tdiv is clearly explained in the manual:

        * `cdiv' rounds Q up towards +infinity, and R will have the
          opposite sign to D.  The `c' stands for "ceil".

        * `fdiv' rounds Q down towards -infinity, and R will have the
          same sign as D.  The `f' stands for "floor".

        * `tdiv' rounds Q towards zero, and R will have the same sign
          as N.  The `t' stands for "truncate".

For a positive divisor D, mpz_mod thus corresponds to fdiv. Note that if you
want to be sure that N=Q*D+R, it is better to use the mpz_?div_qr functions,
which return both quotient and remainder.

Paul Zimmermann


More information about the gmp-discuss mailing list