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

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

Description

With this function you can maintain an input value between a specified range.

License

Open Source License

Parameter

Parameter Description
val The value to clamp.
min The minimum value to clamp between.
max The maximum value to clamp between.

Return

a value clamped between the specified minimum and maximum.

Declaration

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

Method Source Code

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

public class Main {
    /**//www. j  a  v  a 2s .c o m
     * With this function you can maintain an input value between a specified range.
     *
     * @param val The value to clamp.
     * @param min The minimum value to clamp between.
     * @param max The maximum value to clamp between.
     * @return a value clamped between the specified minimum and maximum.
     * @see #clamp(int, int, int)
     */
    public static float clamp(final float val, final float min, final float max) {
        return Math.max(min, Math.min(max, val));
    }

    /**
     * With this function you can maintain an input value between a specified range.
     *
     * @param val The value to clamp.
     * @param min The minimum value to clamp between.
     * @param max The maximum value to clamp between.
     * @return a value clamped between the specified minimum and maximum.
     * @see #clamp(float, float, float)
     */
    public static int clamp(final int val, final int min, final int max) {
        return Math.max(min, Math.min(max, val));
    }
}

Related

  1. clamp(final float min, final float x, final float max)
  2. clamp(final float num, final float bound1, final float bound2)
  3. clamp(final float v, final float min, final float max)
  4. clamp(final float value, final float min, final float max)
  5. clamp(final float x, final float a, final float b)
  6. clamp(float a, float min, float max)
  7. Clamp(float a, float min, float max)