Class MathUtil



  • public class MathUtil
    extends Object
    Various utility functions for mathematical and numerical operations.
    • Constructor Summary

      Constructors

      Constructor and Description
      MathUtil()
       
    • Method Summary

      Methods

      Modifier and Type Method and Description
      static double average(double x1, double x2)
      Computes the average of two numbers.
      static double clamp(double x, double min, double max)
      Clamps a double value to a given range.
      static int clamp(int x, int min, int max)
      Clamps an int value to a given range.
      static double log10(double x)
      Computes the base-10 logarithm of a double value.
      static double max(double v1, double v2, double v3)
       
      static double max(double v1, double v2, double v3, double v4)
       
      static double min(double v1, double v2, double v3, double v4)
       
      static int wrap(int index, int max)
      Computes an index which wraps around a given maximum value.
    • Constructor Detail

      • MathUtil

        public MathUtil()
    • Method Detail

      • clamp

        public static double clamp(double x,
                                   double min,
                                   double max)
        Clamps a double value to a given range.
        Parameters:
        x - the value to clamp
        min - the minimum value of the range
        max - the maximum value of the range
        Returns:
        the clamped value
      • clamp

        public static int clamp(int x,
                                int min,
                                int max)
        Clamps an int value to a given range.
        Parameters:
        x - the value to clamp
        min - the minimum value of the range
        max - the maximum value of the range
        Returns:
        the clamped value
      • log10

        public static double log10(double x)
        Computes the base-10 logarithm of a double value.
        • If the argument is NaN or less than zero, then the result is NaN.
        • If the argument is positive infinity, then the result is positive infinity.
        • If the argument is positive zero or negative zero, then the result is negative infinity.
        Parameters:
        x - a positive number
        Returns:
        the value log a, the base-10 logarithm of the input value
      • wrap

        public static int wrap(int index,
                               int max)
        Computes an index which wraps around a given maximum value. For values >= 0, this is equals to val % max. For values < 0, this is equal to max - (-val) % max
        Parameters:
        index - the value to wrap
        max - the maximum value (or modulus)
        Returns:
        the wrapped index
      • average

        public static double average(double x1,
                                     double x2)
        Computes the average of two numbers.
        Parameters:
        x1 - a number
        x2 - a number
        Returns:
        the average of the inputs
      • max

        public static double max(double v1,
                                 double v2,
                                 double v3)
      • max

        public static double max(double v1,
                                 double v2,
                                 double v3,
                                 double v4)
      • min

        public static double min(double v1,
                                 double v2,
                                 double v3,
                                 double v4)