Java Double Number Clamp clamp(double val)

Here you can find the source of clamp(double val)

Description

Clamps a double between -1 and 1, inclusive.

License

Open Source License

Parameter

Parameter Description
val The value to clamp

Return

The clamped value

Declaration

public static double clamp(double val) 

Method Source Code

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

public class Main {
    /**//from w w w. ja va  2 s  . c om
     * Clamps a double between -1 and 1, inclusive.
     * 
     * @param val The value to clamp
     * @return The clamped value
     */
    public static double clamp(double val) {
        return clamp(1, val);
    }

    /**
     * Clamps a double between -Math.abs(range) and Math.abs(range), inclusive.
     * 
     * @param range The clamping range
     * @param val The value to clamp
     * @return The clamped value
     */
    public static double clamp(double range, double val) {
        return clamp(-Math.abs(range), Math.abs(range), val);
    }

    /**
     * Clamps a double between min and max, inclusive.<br>
     * If min > max, it swaps them.
     * 
     * @param min The minimum value
     * @param max The maximum value
     * @param val The value to clamp
     * @return The clamped value
     */
    public static double clamp(double min, double max, double val) {
        double min_ = min, max_ = max;
        if (min > max) {
            min_ = max;
            max_ = min;
        }

        return Math.max(min_, Math.min(max_, val));
    }
}

Related

  1. clamp(double number)
  2. clamp(double v, double l, double h)
  3. clamp(double v, double lower, double upper)
  4. clamp(double v, double lower, double upper)
  5. clamp(double val)
  6. clamp(double val, double min, double max)
  7. clamp(double val, double min, double max)
  8. clamp(double value)
  9. clamp(double value)