Boost logo

Boost :

Subject: Re: [boost] [Review:Algorithms] Order of args to clamp
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-09-23 13:09:42

Marshall Clow wrote:
> On Sep 23, 2011, at 9:21 AM, Phil Endecott wrote:
> > template<typename V>
> > V clamp ( V val, V lo, V hi );
> >
> > I have a clamp function that orders the args low - middle -
> > high, which seems like a more natural ordering to me. Is
> > there some rationale or precedent for this middle - low -
> > high ordering? I think the confusion matters in this case
> > because type checking won't help to detect mistakes, and in
> > the most obvious applications (e.g. sanitising input) it
> > might be hard to spot an error.

This is a matter of semantics, I think. You call the "extra" argument _middle_ so naturally, the order you specified is best. However, it isn't the middle value, but rather the current value.

So, if you think of clamp() as ensuring the following relation holds, then there's some value in the low/middle/high argument order:

   lo <= result <= hi

OTOH, if you think of clamp() as "returning val after limiting it to the range [lo, hi]" then the current order makes sense.

> I'm not saying you're wrong - but you might want to look at:
> where this discussion played out in the past.

I definitely agree that val should not be last.

Either first or second makes sense and whichever is selected will become the accepted order and folks will get used to it like so many other things that aren't necessarily the way we'd prefer them.

For comparision, the _Clamping (graphics)_ article in Wikipedia uses the current argument order. That article links to clamp functions, using the same order, in the jsPerf library. The Code Project has a 2008 article showing a generic C# Clamp() that uses the same order. I found a number of other examples of the same order. There's even a "preview only" page in MSDN for a float and an int clamp() that follows this order. By contrast, in my quick examination of search results, I didn't find any with the low/middle/high order.

Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP


IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Boost list run by bdawes at, gregod at, cpdaniel at, john at