Why is there no clamp function in math.h
Asked Answered
G

4

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.

Guidon answered 9/2, 2014 at 7:39 Comment(13)
It is so simple to code it.Skolnik
@BasileStarynkevitch It is also simple to code max and min, and yet they made those functions.Guidon
@Guidon You'd think so, but so many people get it wrong. If #define us part of your mix/max, you did it wrong.Threescore
Either way, having written my own version of clamp multiple times, it would be nice to have,Dovelike
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.Carboxylate
@Threescore Which just goes to further prove why the lack of a standard clamp function is so odd.Guidon
@Guidon Not really. Since, once you have min and max, clamp is trivial. Being both trivial and seldom used, it's not really surprising it's not in. I'm betting the fact that a lot of algorithms use min might also have something to do with it the inclusion.Threescore
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 ..."Nessus
@Nessus This is my point. Even if the answer was that they didn't even consider adding it that's something conclusive. Unfortunately I expect that they've never spoken publicly about the decisions made regarding the C standard library and thus the question will go unanswered. Bjarne and Larry on the other hand are always answering questions like this, but I don't think Dennis actually has much to do with the development of C any more.Guidon
Some developers believe it to be useful. Examples: - linux/kernel.h has clamp() - GTK+ GLib has CLAMP(). And I always #define it. But it would be better if it were standardized.Prepotency
Very sadly, en.wikipedia.org/wiki/Dennis_RitchiePrepotency
@JosephQuinsey Oh yeah, I forgot he died. It was before I started programming so it slipped my mind.Guidon
A lot of the of 3rd party libraries I've seen/used define their own variation of clamp. Not to mention a fair number of people have asked on stackoverflow "Where is the clamp function in (X) language", which shows that certain people expect it to be built in.Guidon
D
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.

Davit answered 1/2, 2017 at 16:18 Comment(2)
Maybe the better thing to do would be to tag the question C++14.Bruxelles
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.Guidon
I
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.

Inkling answered 9/2, 2014 at 9:21 Comment(10)
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 ;-)Bayberry
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 :-pInkling
Ouch! You're making it worse, +1 for the effort though :-) If you like I can edit your answer and provide a templated version.Bayberry
@Bayberry feel free to add a template version (though please note with it that the template solution will only work in C++ not C).Inkling
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.Guidon
I will admit that the namespace collisions part is more on topic than most of the other things that have been said.Guidon
The evidence is that they did not include it. If they had felt that need for it, they would have included it (given, as illustrated, it's easy to write). Given that we know it would have been easy to write, but they didn't include it, by modus tollens we can conclude they didn't feel the need for it. I also said that it doesn't form part of any standard. Given these libraries are open source, the way to find the real answer (if you disagree with my assessment) is to submit a patch and see what happens.Inkling
@Inkling 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.Guidon
I'd prefer one of:<br/> - return min( max( lower, value ), upper );<br/> - return min( max( value, lower ), upper );Ptyalin
Downvote. This is just snarky and unhelpful. The whole point of math.h is to provide commonly-used maths functions for convenience. Being simple and easy to implement ourselves doesn't come into it. By that logic, it shouldn't include min or max either.Eckard
B
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.

Bayberry answered 11/2, 2014 at 18:15 Comment(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.Guidon
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."Ptyalin
D
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.

Dichotomize answered 9/2, 2014 at 7:43 Comment(3)
Do you have evidence that the authors thought that it was not used often enough or is that just speculation?Guidon
It is not included in the standard and I believe this is enough as a proof.Dichotomize
That proves they didn't add it, it doesn't prove that they didn't add it because it wasn't used often enough.Guidon

© 2022 - 2024 — McMap. All rights reserved.