[interval] suggestions and minor doc error

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; } 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]. 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"? Thanks for the good work on this class, Mark Gilbert

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

"Guillaume Melquiond" <guillaume.melquiond@ens-lyon.fr> wrote in message news:1118349671.6045.19.camel@saline...
Le jeudi 09 juin 2005 à 14:23 -0500, Mark Gilbert a écrit :
T evaluate(T t) const { return low + t * extent(); } T normalize(T t) const { return (t - low) / extent(); } evaluate and normalize map the interval to a normalized interval with values in the range [0,1].
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.
Hi Guillaume, Let me argue for evaluate and normalize a little. Here is code that generates 21 points on a line from (x0,y0) to (x1,y1): for (int i = 0; i <= 20; i++) { double p = i / 20.0; dopoint(interval(x0,x1).evaluate(p), interval(y0,y1).evaluate(p)); } Here is a linear interpolator for a function that is known only at integer values: double interpolate(double values[], double x) { int x0 = ifloor(x); int x1 = iceil(x); double xfrac = x - x0; return interval(values[x0],values[x1]).evaluate(xfrac)); } Mapping to and from the range [0,1] lets you get at the ideas "where does this value fall in my interval?" and "what value is at this position in my interval?" Your median function is similar to evaluate, but 0.5 is just one position in the interval that might be interesting. Perhaps evaluate(0.25) or evaluate(x) are more interesting for some applications. Regarding the names. I work a lot with parameterized curves in space, which are "normally" parameterized on [0,1]. I "evaluate" a value in that range to get a point on the curve (or a value in an interval). Perhaps without that context, other names would be better, though normalize might still be analogous to normalizing a vector to a unit vector. Instead of evaluate and normalize: scale and normalize? at_position and position (or get_position)? map_from_01 and map_to_01? I'm not sure I like any of those, but we can probably think of something better if you decide the functions themselves might be handy. Cheers, Mark Gilbert
participants (3)
-
Guillaume Melquiond
-
Mark Gilbert
-
Mark Gilbert