Next / Previous / Contents / TCC Help System / NM Tech homepage

51. divCeil(): Discrete rounding function
# - - -   d i v C e i l

def divCeil(n, d):
    '''Computes the number of multiples of d necessary to hold n.

      [ (n is a nonnegative integer) and
        (d is a positive integer) ->
          return int(ceil(float(n)/float(d))) ]

This function represents one of the author's oldest bits of programming practice: how many multiples of d are necessary to hold n things? For example, if we are placing n radiobuttons in a table of d columns, how many rows are there? The method is to add d-1 to n and then use integer arithmetic to divide by d.
    return (n+d-1) // d


The Python “//” operator is integer divide; in future Python versions the “/” operator will actually return the quotient as a float.