def _to_frac(x, maxdenom=10):
    """
    Convert x to a common fraction.
    
    Chooses the closest fraction to x with denominator <= maxdenom.
    If x is closest to an integer, return that integer; otherwise,
    return an (integer, numerator, denominator) tuple.
    """
    assert x >= 0, '_to_frac only works on positive numbers.'
    intpart = int(x)
    x -= intpart
    bestfrac = 0, 1
    mindiff = x
    for denom in range(1, maxdenom + 1):
        for num in (int(x * denom), int(x * denom + 1)):
            diff = abs(float(num) / denom - x)
            if diff < mindiff:
                bestfrac = num, denom
                mindiff = diff
    if bestfrac[0] == 0:
        return intpart
    elif mindiff >= 1 - x:
        return intpart + 1
    else:
        return intpart, bestfrac[0], bestfrac[1]
