Java Float Number Clamp clamp(float v)

Here you can find the source of clamp(float v)

Description

Clamps the value so the result is between 0.0 and 1.0.

License

Open Source License

Parameter

Parameter Description
v the value to clamp

Return

a value between 0.0 and 1.0.

Declaration

public static float clamp(float v) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  w w  w  . ja v  a  2 s  . co m
     * Clamps the value so the result is between 0.0 and 1.0.
     * <p/>
     * This means that if the value is smaller than 0.0, this method will return 0.0.
     * If the value is larger than 1.0, this method will return 1.0.
     * Values within the range are returned unchanged.
     *
     * @param v the value to clamp
     * @return a value between 0.0 and 1.0.
     */
    public static float clamp(float v) {
        return 0 > v ? 0 : 1 < v ? 1 : v;
    }

    /**
     * Clamps the value so the result is between 0.0 and 1.0.
     * <p/>
     * This means that if the value is smaller than 0.0, this method will return 0.0.
     * If the value is larger than 1.0, this method will return 1.0.
     * Values within the range are returned unchanged.
     *
     * @param v the value to clamp
     * @return a value between 0.0 and 1.0.
     */
    public static double clamp(double v) {
        return 0 > v ? 0 : 1 < v ? 1 : v;
    }

    /**
     * Clamps the value so the result is between the given minimum and maximum value.
     * <p/>
     * This means that if the value is smaller than min, this method will return min.
     * If the value is larger than max, this method will return max.
     * Values within the range are returned unchanged.
     *
     * @param v   the value to clamp
     * @param min the minimum value
     * @param max the maximum value
     * @return a value between min and max.
     */
    public static float clamp(float v, float min, float max) {
        return min > v ? min : max < v ? max : v;
    }

    /**
     * Clamps the value so the result is between the given minimum and maximum value.
     * <p/>
     * This means that if the value is smaller than min, this method will return min.
     * If the value is larger than max, this method will return max.
     * Values within the range are returned unchanged.
     *
     * @param v   the value to clamp
     * @param min the minimum value
     * @param max the maximum value
     * @return a value between min and max.
     */
    public static double clamp(double v, double min, double max) {
        return min > v ? min : max < v ? max : v;
    }
}

Related

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