Math::Clamp assert

Minor issues with the Ogre API that can be trivial to fix
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts: 2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
x 218

Math::Clamp assert

Post by Jabberwocky »

This is an extremely minor annoyance, but I guess that's what papercuts are for.

The function, from ogremath.h

Code: Select all

		/** Clamp a value within an inclusive range. */
		template <typename T>
		static T Clamp(T val, T minval, T maxval)
		{
			assert (minval < maxval && "Invalid clamp range");
			return std::max(std::min(val, maxval), minval);
		}
I think the assert should be:

Code: Select all

assert (minval <= maxval && "Invalid clamp range");
Since I think this should be valid:

Code: Select all

int n = 15;
int nMin = 3;
int nMax = 3;
n = Math::Clamp( n, nMin, nMax );
I know that looks a little weird and redundant.
But as currently written, I often have to wrap my Math::Clamp calls with a little extra logic to make sure I won't trigger that assert, in places where nMin and nMax are variable values.
Image