import math


def _ceil_with_max(num, max_value):
    """
    Ceil function that also takes a max value, to deal with possible rounding errors.
    Rounding errors in floats can cause out of index errors when we index into an array.
    """
    return min(math.ceil(num), max_value)