Calculate the average of function P(x), in the interval [length1, length2], where P(x) is the fraction of tuples with length < x (or length <= x if 'equal' is true).