Arbitrary precision integer arithmetic: ceiling?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Mar 8 21:31:15 EST 2008
On Sun, 09 Mar 2008 10:11:53 +1100, Alasdair wrote:
> I need to apply the ceiling function to arbitrary sized (long) integers.
> However, division automatically returns the type of its operands, so
> that, for example: math.ceil(7/4) returns 1. I can use float, as in:
> math.ceil(7/float(4)), except that for very large integers float causes
> an unacceptable loss of precision.
>
> What is the best way of finding a ceiling of a quotient of arbitrary
> sized integers?
def quot_ceil(a, b):
"""Returns the integer ceiling of the quotient of longints."""
q, r = divmod(a, b)
if r: return q+1
else: return q
--
Steven
More information about the Python-list
mailing list