GMP floor and ceil

Torbjorn Granlund tg at
Sat Nov 29 23:17:52 CET 2008

"Sarad AV" <esarad at> writes:

  I have the following requirement. a, b are positive integers and a>b.
  To find Floor( a/b ) and Ceil ( a/b ) efficiently, when b does not divide
  I am aware that gmp has floor and ceil functions. Where can I find the
  algorithm used by GMP to find floor and ceil for the above case?

Are you asking how GMP performs division with rounding towards +oo and

It first computes floor(|a|/|b|) and the corresponding remainder.  Then,
from the sign of a and b, and whether the remainder is zero, it adjusts
the quotient.

I suppose the exact rules are actually easiest to see in the source
code, see mpz/*div_q.c.  These files are really small.


More information about the gmp-discuss mailing list