
Le jeudi 09 juin 2005 à 14:23 -0500, Mark Gilbert a écrit :
In my own code, I have long used a type similar to boost::interval. In my class, I find these functions useful:
T center() const { return (low + up) / 2; } T extent() const { return up - low; }
They are available as 'median' and 'width' in the library.
T clamp(T t) const { return t < low ? low : t > up ? up : t; } T wrap(T t) const { return fmodpos(t - low, extent()) + low; } T evaluate(T t) const { return low + t * extent(); } T normalize(T t) const { return (t - low) / extent(); }
clamp provides a convenient way to limit a value, changing this ugly thing: double x = max(lower, min(upper, raw_x)); to this: double x = interval(lower, upper).clamp(raw_x);
evaluate and normalize map the interval to a normalized interval with values in the range [0,1].
I never had a use myself for functions such as 'clamp' and 'wrap', but they could indeed be useful. Concerning 'evaluate' and 'normalize' though, they don't strike me as necessary. First, the names are not really indicative of their semantic. Second, the interval [0,1] seems a bit arbitrary: [-1,1] would make as much sense to me.
The minor documentation error is at (http://www.boost.org/libs/numeric/interval/doc/interval.htm), where it says, ". . . rounding a+b down and c+d up will suffice." Shouldn't that say "a+c down and b+d up"?
Indeed. Thanks for spotting it.
Thanks for the good work on this class,
Thanks for your suggestions. Best regards, Guillaume