Class DoubleUtils

java.lang.Object
ca.team4308.absolutelib.math.DoubleUtils

public class DoubleUtils extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    clamp(double d, double min, double max)
    Clamps d to the closed interval [min, max].
    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.
    static double
    mapRangeNew(double x, double in_min, double in_max, double out_min, double out_max)
    Alternate form of mapRange(double, double, double, double, double) using different parameter names.
    static double
    normalize(double d)
    Normalizes a value into the range [-1.0, 1.0].

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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, returns 1.0.
      • If d < -1.0, returns -1.0.
      • Otherwise, returns d unchanged.
      Parameters:
      d - the input value
      Returns:
      the value clamped to [-1.0, 1.0]
    • clamp

      public static double clamp(double d, double min, double max)
      Clamps d to the closed interval [min, max].
      • If d > max, returns max.
      • If d < min, returns min.
      • Otherwise, returns d unchanged.
      Parameters:
      d - the input value
      min - the minimum allowed value (inclusive)
      max - the maximum allowed value (inclusive)
      Returns:
      d clamped to [min, max]
    • 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 d in 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 d to the input range.

      Parameters:
      d - the value to map (typically in [old_min, old_max])
      old_min - lower bound of the input range
      old_max - upper bound of the input range
      new_min - lower bound of the output range
      new_max - upper bound of the output range
      Returns:
      mapped value in the output range (possibly outside if d is outside the input range)
      Throws:
      ArithmeticException - if old_max == old_min
    • mapRangeNew

      public static double mapRangeNew(double x, double in_min, double in_max, double out_min, double out_max)
      Alternate form of mapRange(double, double, double, double, double) using different parameter names.

      Given an input x in 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 map
      in_min - lower bound of the input range
      in_max - upper bound of the input range
      out_min - lower bound of the output range
      out_max - upper bound of the output range
      Returns:
      mapped value in the output range
      Throws:
      ArithmeticException - if in_max == in_min