Clamping. This could be added to fast math. I have needed it a fair bit in different circumstances. But again this is myself, who knows if anyone else needs it.
/**
* Take a float and clamp it between 0.0 and 1.0
*
* @param input
* @return clamped input
*/
public static float clamp( float input ) {
input = input < 0 ? 0 : input;
input = input > 1 ? 1 : input;
return input;
}
The Java version does a few more checks. Java.min and max both check to see if the number is valid ie not a NaN. Also it makes a difference between positive zero and negative zero. So it would seem it is slower due to the excessive checking and extra method call.
/**
* Take a float and clamp it between min and max.
*
* @param input
* @param min
* @param max
* @return clamped input
*/
public static float clamp( float input , float min , float max ) {
input = input < min ? min : input;
input = input > max ? max : input;
/**
* Take a float and clamp it between min and max.
*
* @param input
* @param min
* @param max
* @return clamped input
*/
public static float clamp( float input , float min , float max ) {
return (input < min) ? min : (input > max) ? max : input;