Java Float Number Clamp clamp(float val, float min, float max)

Here you can find the source of clamp(float val, float min, float max)

Description

Clamp a value between min and max bounds.

License

Open Source License

Parameter

Parameter Description
val a parameter
min a parameter
max a parameter

Return

val if val>=min and val<=max. min if valmax

Declaration

public final static float clamp(float val, float min, float max) 

Method Source Code

//package com.java2s;
/*    /*  w w w . j  a  v  a2s.co m*/
 This file is part of jME Planet Demo.
    
 jME Planet Demo is free software: you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation.
    
 jME Planet Demo is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Lesser General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with jME Planet Demo.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Clamp a value between min and max bounds.
     *
     * @param val
     * @param min
     * @param max
     * @return val if val>=min and val<=max. min if val<min and max if val>max
     */
    public final static float clamp(float val, float min, float max) {
        return Math.max(min, Math.min(max, val));
    }

    /**
     * Clamp a value between min and max bounds.
     *
     * @param val
     * @param min
     * @param max
     * @return val if val>=min and val<=max. min if val<min and max if val>max
     */
    public final static int clamp(int val, int min, int max) {
        return Math.max(min, Math.min(max, val));
    }

    public final static float max(float... fs) {
        float ret = Float.MIN_VALUE;
        for (float f : fs) {
            ret = Math.max(ret, f);
        }
        return ret;
    }

    public final static float min(float... fs) {
        float ret = Float.MAX_VALUE;
        for (float f : fs) {
            ret = Math.min(ret, f);
        }
        return ret;
    }
}

Related

  1. clamp(float v)
  2. clamp(float v, float min, float max)
  3. clamp(float val, float low, float high)
  4. clamp(float val, float max, float min)
  5. clamp(float val, float min, float max)
  6. Clamp(float val, float minVal, float maxVal)
  7. clamp(float value)
  8. Clamp(float value, float lowerLimit, float upperLimit)
  9. clamp(float value, float min, float max)