You didn't mention adding, either. You only requested more operations,
in particular 'averaging', but not 'adding'. Were such an operation be
provided, it would be the implementation's duty to work out the details
such that it always worked, not yours or Dik's.
Dik was right to guess that I was imagining that addition and division
were the operations provided, with the user implementing averaging as
(p1 + p2) / 2.[/QUOTE]
What if (p1 + p2) isn't subject to a division? If you are going to define
addition over a domain, that addition should be meaningful by itself.
What are the constraints for p1 + p2? Do these have to be compatible types?
What is the type of the result? Is it a pointer?
What about overflows? p1 and p2 could be anywhere in the address space.
The concept of interpolating values does not require addition in the domain of
those values.
If you want a position one third between two addresses p1 and p2 within some
object, simply take one third of the delta p2 - p1, and add that to p1.
Adding the two quantities and then dividing is a ``hack'' which only works when
the interpolation is 50%.
If you want the point which is 1/10 between p1 and p2, it's not (p1 + p2)/3 but
rather (9 * p1 + p2) / 10. This is because you're taking 1:9 blend of the
values: 9/10 p1 + 1/10 p2. So now you need multiplication of a pointer by
a number number, unless you are happy writing this as
(p1 + p1 + p1 + p1 + p1 + p1 + p1 + p1 + p1 + p2) / 10.
Somehow, p1 + (p2 - p1) / 10 looks a lot better.
In fact, it's better to interpolate integers this way, not only pointers,
because of the avoidance of overflow.
E.g.
/* update smoothed estimator,
blending 75% of the old value with 25% of the sample */
smoothed_estimator += (new_sample - smoothed_estimator) / 4;
You'd not want to do this using:
smoothed_estimator = (3 * smoothed_estimator + new_sample) / 4