Here you can find the source of clamp(double val)
Parameter | Description |
---|---|
val | The value to clamp |
public static double clamp(double val)
//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)); } }