Re: Function f(lb,x,ub); equivalent to min(ub,max(lb,x)); ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Harvey wrote:
On Feb 25, 11:57 pm, "Harvey" <harve...@xxxxxxxx> wrote:

Is there a (mfc) library function like f(lb,x,ub); that will return
the value of x if it is in range, else the bound x exceeded? It would
be equivalent to min(ub,max(lb,x)); but faster and not a macro? I
think I would use it a lot. :)
TIA,
Harvey


Final version:

template <typename T>
T Clamp( T lower, T x, T upper )
{
if ( lower > x )
return lower;
if ( x > upper )
return upper;
return x;
}

I used > for both rather than < so that the flow would follow the
input args.
Thanks to all who commented.
Harvey


Harvey:

Not to beat a dead horse, but I would still follow what the standard library does for min() and max(): use < only and const T&.

The reason to use < is because that is what many standard library algorithms (like std::sort()) require to be defined. If you use > you are placing a requirement on your class that is not required by the standard library.

The reason to use const T& is that it will be much more efficient for a complex type, particularly one that requires memory allocation in the copy constructor. I would need detailed evidence that using T is more efficient for simple types, and even if it was it would be extremely unusual for it to really matter.

The standard library folks must have thought about these things before they defined and implemented min() and max() the way they did.

David Wilkinson
.



Relevant Pages

  • Re: Function f(lb,x,ub); equivalent to min(ub,max(lb,x)); ?
    ... if (x> upper) ... mov eax, DWORD PTR ... If there is a higher-level downside to returning a const reference argument ...
    (microsoft.public.vc.mfc)
  • Re: Case-insensitivity considered harmful
    ... |> about this, primarily for programming languages, but the same rationale ... are lots where it is very hard to distinguish many lower-case letters. ... fonts use a non-traditional '0' for that reason, ... common convention was commentary in mixed case and code in upper. ...
    (comp.arch)
  • Re: Function f(lb,x,ub); equivalent to min(ub,max(lb,x)); ?
    ... if (x> upper) ... int f ... If there is a higher-level downside to returning a const reference argument ...
    (microsoft.public.vc.mfc)
  • Re: More of that "Government" Bellomy is so in love with...
    ... BTW they are also the reason dozens of states ... (yours was one of the first IIRC) ... You probably thought from this that all upper case street signs have ... And btw if you are a civil engineering or wannabe civil engineering ...
    (rec.sport.football.college)
  • Re: More of that "Government" Bellomy is so in love with...
    ... BTW they are also the reason dozens of states ... You probably thought from this that all upper case street signs have ... case have to be replaced with mixed case letters. ... And btw if you are a civil engineering or wannabe civil engineering ...
    (rec.sport.football.college)