11

math.h goes to the trouble of providing min and max, but not a clamp function. I would have thought that as they are all usually similar implementation-wise they would all appear in the same library.

Is there any particular reason that math.h does not feature a clamp function|macro? Is it that the creators of math.h did not deem it necessary or did they just not think about it?

EDIT: It seems people are missing the point here. I'm not asking why didn't they add clamp because I'm lazy and don't like writing a new clamp - quite the opposite, I hardly ever use it (though admittedly I use it more than I use some of the standard libraries).

What I'm asking is "does anyone know of any reason why the c++ standardisation authorities or creators or whoever chose not to include a clamp function in math.h?".

I am by no means complaining that it is not in math.h, I am merely asking "is there a good reason it isn't there?".

EDIT: I am explicitly not asking how to write a clamp() function.

13
  • 6
    @BasileStarynkevitch It is also simple to code max and min, and yet they made those functions.
    – Pharap
    Commented Feb 9, 2014 at 7:43
  • 3
    Either way, having written my own version of clamp multiple times, it would be nice to have, Commented Feb 9, 2014 at 9:38
  • 1
    The burden of a library writer is not to implement a function correctly when the programmer uses it correctly, that's easy. It is to some meaningful when he doesn't. Commented Feb 9, 2014 at 10:36
  • 2
    Interesting question indeed and, alas, I've asked similar questions already. But how can one know? Maybe even the coders of math.h don't know the answer themselves. Like "Clamp? Yes, that would've been nice, too ..." Commented Feb 10, 2014 at 13:34
  • 2
    @JosephQuinsey Oh yeah, I forgot he died. It was before I started programming so it slipped my mind.
    – Pharap
    Commented Feb 15, 2014 at 17:23

4 Answers 4

14

The other answers are no longer valid, as std::clamp is now in C++17.

At the time of writing it isn't supported by GCC, but will be in GCC 7.

2
  • Maybe the better thing to do would be to tag the question C++14.
    – Quentin
    Commented Feb 1, 2017 at 16:21
  • Perhaps someone on the comittee saw this question and answered my prayers. In all seriousness though, I am now conflicted as to whether to change the accepted answer given that the correct answer has indeed changed, or whether to mark this question as pre-C++17 (as per @Quentin's suggestion) and ask a separate post-C++17 specific question.
    – Pharap
    Commented Feb 1, 2017 at 17:57
13

Perhaps because:

double clamp(double x, double upper, double lower)
{
    return min(upper, max(x, lower));
}

uses fewer characters than your question.

An alternative type-free method is

#define CLAMP(x, upper, lower) (MIN(upper, MAX(x, lower)))

assuming you have MIN and MAX macros in the normal form.

A templated C++ version could be implemented as follows:

template<class T>
const T& clamp(const T& x, const T& upper, const T& lower) {
    return min(upper, max(x, lower));
}

Naturally, the latter will not work in good ol' C.

To be more constructive, the function isn't in the standard library because the authors did not feel the need for it to be there sufficiently to add it. It's pretty obvious how to achieve the function you want (see above), so there's no particular hardship. Once you have a standard for what's in the library, adding further functions risks namespace collisions with existing code, requires documentation, testing etc., so there is a threshold of general usefulness any new function needs to cross.

10
  • 2
    Didn't downvote this, but this does not answer the question. The same can be said for the min and max functions. If you template this, I'll compensate for the downvote though ;-)
    – pauluss86
    Commented Feb 9, 2014 at 15:00
  • Alright, I've attempted to be more constructive. Being a C guy rather than a C++ guy (question has both tags) I've added a #define rather than a template :-p
    – abligh
    Commented Feb 9, 2014 at 16:17
  • Ouch! You're making it worse, +1 for the effort though :-) If you like I can edit your answer and provide a templated version.
    – pauluss86
    Commented Feb 9, 2014 at 16:59
  • 1
    Is there any evidence that the authors didn't feel the need for it or is that just speculation? pauluss is right by the way, you could give me 1000 variations of clamp and wouldn't make a difference because I'm not actually asking how to write a clamp function.
    – Pharap
    Commented Feb 10, 2014 at 12:55
  • 2
    @abligh The part where that logic falls down is that they did include the min and max functions, which are equally as easy to write, if not easier. By all means if you can point me to where to submit a patch to the C standards committee, I'll gladly see what they say. If you mean submitting a patch to an individual implementation of the standard libraries, that's not really assisting in answering the question.
    – Pharap
    Commented Feb 10, 2014 at 17:28
7

Although I can't really answer this, I'll add my two cents anyway. After searching for mathematical functions on this standards committee page, I couldn't find any mention of a clamp() function.

I did find, among other similar documents, this PDF that documents proposed math-functions; most seem pretty specialized. This leads me to believe that a function like clamp() is absent from the standard library, simply because no-one has proposed it.

Personally, I'd rather see simple (and often used) math functions added to the standard library than functions that most probably will never be used by 99% of developers. For the latter there are specialized libraries, but that's just my irrelevant opinion of course.

An example of what I'd definitely like to see added to the standard: the majority of the core functions of the (excellent) GLM library. This would include clamp() as well :-)

Perhaps it's time to write a proposal and submit it for comments\approval; it's the only way to do something about it. This page presents information regarding proposals.

2
  • +1 for research and references. I believe this is probably as close as we're going to get to an actual answer, so I'm calling it accepted unless some miraculous new evidence springs forth at the last minute.
    – Pharap
    Commented Feb 11, 2014 at 19:39
  • 1
    clamp() is being proposed for <algorithm>, see N4536, An algorithm to "clamp" a value between a pair of boundary values. This proposal is to be revised by P0025 (revision 1) in October 2015." Commented Sep 19, 2015 at 7:58
0

You can't hope to have built-in function for everything you'll need. Also if you have a look here, there is no clamp function in Java either. A clamp function is simple to write and apparently authors thought that it is not used often enough to have it in the standard library.

3
  • Do you have evidence that the authors thought that it was not used often enough or is that just speculation?
    – Pharap
    Commented Feb 10, 2014 at 13:03
  • It is not included in the standard and I believe this is enough as a proof. Commented Feb 10, 2014 at 13:54
  • 3
    That proves they didn't add it, it doesn't prove that they didn't add it because it wasn't used often enough.
    – Pharap
    Commented Feb 10, 2014 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.