Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no clamp function in math.h

Tags:

c++

std

clamp

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.

like image 386
Pharap Avatar asked Feb 09 '14 07:02

Pharap


2 Answers

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.

like image 130
pauluss86 Avatar answered Sep 28 '22 06:09

pauluss86


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.

like image 30
abligh Avatar answered Sep 28 '22 07:09

abligh