Java Float Number Clamp clamp(float val, float low, float high)

Here you can find the source of clamp(float val, float low, float high)

Description

Clamps a value between a lower and upper bound.

License

Open Source License

Parameter

Parameter Description
val the value to clamp
low the lower bound
high the upper bound

Return

the clamped value

Declaration

public static float clamp(float val, float low, float high) 

Method Source Code

//package com.java2s;
/*// www .  j ava 2 s .co  m
 * opsu! - an open-source osu! client
 * Copyright (C) 2014, 2015 Jeffrey Han
 *
 * opsu! is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * opsu! 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with opsu!.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Clamps a value between a lower and upper bound.
     * @param val the value to clamp
     * @param low the lower bound
     * @param high the upper bound
     * @return the clamped value
     * @author fluddokt
     */
    public static int clamp(int val, int low, int high) {
        if (val < low)
            return low;
        if (val > high)
            return high;
        return val;
    }

    /**
     * Clamps a value between a lower and upper bound.
     * @param val the value to clamp
     * @param low the lower bound
     * @param high the upper bound
     * @return the clamped value
     * @author fluddokt
     */
    public static float clamp(float val, float low, float high) {
        if (val < low)
            return low;
        if (val > high)
            return high;
        return val;
    }
}

Related

  1. clamp(float min, float x, float max)
  2. clamp(float n, float minValue, float maxValue)
  3. clamp(float num, float min, float max)
  4. clamp(float v)
  5. clamp(float v, float min, float max)
  6. clamp(float val, float max, float min)
  7. clamp(float val, float min, float max)
  8. clamp(float val, float min, float max)
  9. Clamp(float val, float minVal, float maxVal)