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

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

Description

Clamp the given value to the given range.

License

Open Source License

Parameter

Parameter Description
v is the value to clamp.
min is the min value of the range.
max is the max value of the range.

Return

the value in [min;max] range.

Declaration

public static float clamp(float v, float min, float max) 

Method Source Code

//package com.java2s;
/* //from w w  w  .j  av a 2  s.  c o  m
 * $Id$
 * 
 * Copyright (c) 2011-15 Stephane GALLAND <stephane.galland@utbm.fr>.
 * 
 * This library 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 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * This program is free software; you can redistribute it and/or modify
 */

public class Main {
    /** Clamp the given value to the given range.
     * <p>
     * If the value is outside the {@code [min;max]}
     * range, it is clamp to the nearest bounding value
     * <var>min</var> or <var>max</var>.
     * 
     * @param v is the value to clamp.
     * @param min is the min value of the range.
     * @param max is the max value of the range.
     * @return the value in {@code [min;max]} range.
     */
    public static float clamp(float v, float min, float max) {
        if (min < max) {
            if (v < min)
                return min;
            if (v > max)
                return max;
        } else {
            if (v > min)
                return min;
            if (v < max)
                return max;
        }
        return v;
    }
}

Related

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