Java Float Number Clamp clamp(float value, float minimum, float maximum)

Here you can find the source of clamp(float value, float minimum, float maximum)

Description

Assure that a value is between two other values.

License

Open Source License

Parameter

Parameter Description
value The value to clamp.
minimum The minimum return value.
maximum The maximum return value.

Return

value if it is between minimum and maximum , minimum if value < minimum , or maximum of value > maximum .

Declaration

public static float clamp(float value, float minimum, float maximum) 

Method Source Code

//package com.java2s;
/*/*from w  w w .j  av a 2  s  .com*/
   This file is part of GBukkitCore.
    
GBukkitCore 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, either version 3 of the License, or
(at your option) any later version.
    
GBukkitCore 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 Lesser General Public License
along with GBukkitCore.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Assure that a value is between two other values.
     * @param value The value to clamp.
     * @param minimum The minimum return value.
     * @param maximum The maximum return value.
     * @return {@code value} if it is between {@code minimum} and {@code maximum}, {@code minimum} if {@code value < minimum}, or {@code maximum} of {@code value > maximum}.
     */
    public static float clamp(float value, float minimum, float maximum) {
        return Math.max(Math.min(value, maximum), minimum);
    }

    /**
     * Assure that a value is between two other values.
     * @param value The value to clamp.
     * @param minimum The minimum return value.
     * @param maximum The maximum return value.
     * @return {@code value} if it is between {@code minimum} and {@code maximum}, {@code minimum} if {@code value < minimum}, or {@code maximum} of {@code value > maximum}.
     */
    public static double clamp(double value, double minimum, double maximum) {
        return Math.max(Math.min(value, maximum), minimum);
    }

    /**
     * Assure that a value is between two other values.
     * @param value The value to clamp.
     * @param minimum The minimum return value.
     * @param maximum The maximum return value.
     * @return {@code value} if it is between {@code minimum} and {@code maximum}, {@code minimum} if {@code value < minimum}, or {@code maximum} of {@code value > maximum}.
     */
    public static long clamp(long value, long minimum, long maximum) {
        return Math.max(Math.min(value, maximum), minimum);
    }

    /**
     * Assure that a value is between two other values.
     * @param value The value to clamp.
     * @param minimum The minimum return value.
     * @param maximum The maximum return value.
     * @return {@code value} if it is between {@code minimum} and {@code maximum}, {@code minimum} if {@code value < minimum}, or {@code maximum} of {@code value > maximum}.
     */
    public static int clamp(int value, int minimum, int maximum) {
        return Math.max(Math.min(value, maximum), minimum);
    }
}

Related

  1. clamp(float value, float min, float max)
  2. clamp(float value, float min, float max)
  3. clamp(float value, float min, float max)
  4. clamp(float value, float min, float max)
  5. clamp(float value, float min, float max)
  6. clamp(float value, float minimum, float maximum)
  7. clamp(float x, float a, float b)
  8. clamp(float x, float y, float z)
  9. clamp180(float r1, float r2)