import math


def correct_integer_ub(ub, integer_tolerance):
    """Converts an upper bound for an integer optimization
    variable to an integer equal to `floor(ub)`, taking care
    not to move a non-integer bound away from an integer
    point already within a given tolerance."""
    assert 0 <= integer_tolerance < 0.5
    if math.ceil(ub) - ub > integer_tolerance:
        return int(math.floor(ub))
    else:
        return int(math.ceil(ub))