Package ca.team4308.absolutelib.math
Class DoubleUtils
java.lang.Object
ca.team4308.absolutelib.math.DoubleUtils
Utility methods for working with doubles in typical robotics control
scenarios.
This class provides common operations such as clamping values and mapping between ranges. All methods are static and side-effect free.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleclamp(double d, double min, double max) Clampsdto the closed interval [min,max].static doublemapRange(double d, double old_min, double old_max, double new_min, double new_max) Linearly maps a value from one range into another.static doublemapRangeNew(double x, double in_min, double in_max, double out_min, double out_max) Alternate form ofmapRange(double, double, double, double, double)using different parameter names.static doublenormalize(double d) Normalizes a value into the range [-1.0, 1.0].
-
Constructor Details
-
DoubleUtils
public DoubleUtils()
-
-
Method Details
-
normalize
public static double normalize(double d) Normalizes a value into the range [-1.0, 1.0].- If
d > 1.0, returns1.0. - If
d < -1.0, returns-1.0. - Otherwise, returns
dunchanged.
- Parameters:
d- the input value- Returns:
- the value clamped to [-1.0, 1.0]
- If
-
clamp
public static double clamp(double d, double min, double max) Clampsdto the closed interval [min,max].- If
d > max, returnsmax. - If
d < min, returnsmin. - Otherwise, returns
dunchanged.
- Parameters:
d- the input valuemin- the minimum allowed value (inclusive)max- the maximum allowed value (inclusive)- Returns:
dclamped to [min,max]
- If
-
mapRange
public static double mapRange(double d, double old_min, double old_max, double new_min, double new_max) Linearly maps a value from one range into another.Given an input
din the range [old_min,old_max], returns a value in the range [new_min,new_max] such that:d = old_min -> new_min d = old_max -> new_max
and the mapping is linear in between.This method does not clamp
dto the input range.- Parameters:
d- the value to map (typically in [old_min,old_max])old_min- lower bound of the input rangeold_max- upper bound of the input rangenew_min- lower bound of the output rangenew_max- upper bound of the output range- Returns:
- mapped value in the output range (possibly outside if
dis outside the input range) - Throws:
ArithmeticException- ifold_max == old_min
-
mapRangeNew
public static double mapRangeNew(double x, double in_min, double in_max, double out_min, double out_max) Alternate form ofmapRange(double, double, double, double, double)using different parameter names.Given an input
xin the range [in_min,in_max], returns a value in the range [out_min,out_max] using the standard linear mapping:out = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
- Parameters:
x- the value to mapin_min- lower bound of the input rangein_max- upper bound of the input rangeout_min- lower bound of the output rangeout_max- upper bound of the output range- Returns:
- mapped value in the output range
- Throws:
ArithmeticException- ifin_max == in_min
-