Class NumericsUtilities


  • public class NumericsUtilities
    extends Object
    Class to help out with numeric issues, mostly due to floating point usage.

    Since the floating point representation keeps a constant relative precision, comparison is done using relative error.

    Be aware of the fact that the methods

    can be used in the case of "simple" numerical comparison, while in the case of particular values that are generated through iterations the user/developer should consider to supply an epsilon value derived from the knowledge of the domain of the current problem and use the methods

    Author:
    Andrea Antonello (www.hydrologis.com)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double D_TOLERANCE
      The double tolerance used for comparisons.
      static float F_TOLERANCE
      The float tolerance used for comparisons.
      static double RADTODEG
      Radiants to degrees conversion factor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean dEq​(double a, double b)
      Returns true if two doubles are considered equal based on a tolerance of .
      static boolean dEq​(double a, double b, double epsilon)
      Returns true if two doubles are considered equal based on an supplied epsilon.
      static boolean fEq​(float a, float b)
      Returns true if two floats are considered equal based on a tolerance of .
      static boolean fEq​(float a, float b, float epsilon)
      Returns true if two floats are considered equal based on an supplied epsilon.
      static double gamma​(double x)  
      static double getMachineDEpsilon()
      Getter for the calculated machine double epsilon.
      static List<int[]> getNegativeRanges​(double[] x)
      Get the range index for which x is negative.
      static boolean isBetween​(double value, double... ranges)
      Check if value is inside a ND interval (bounds included).
      static <T extends Number>
      T
      isNumber​(String value, Class<T> adaptee)
      Checks if a string is a number (currently Double, Float, Integer).
      static double logGamma​(double x)
      Gamma function ported from the apache math package.
      static float machineFEpsilon()
      Getter for the calculated machine float epsilon.
      static double normalize​(double max, double min, double value, double normMax)
      Normalizes a value for a given normalization max (assuming the lower is 0).
      static double pythagoras​(double d1, double d2)
      Calculates the hypothenuse as of the Pythagorean theorem.
      static double[] range2Bins​(double min, double max, double step, boolean doLastEqual)
      Creates an array of bins from a range and a step to use.
      static double[] range2Bins​(double min, double max, int binsNum)
      Creates an array of equal bin from a range and the number of bins.
    • Field Detail

      • D_TOLERANCE

        public static final double D_TOLERANCE
        The double tolerance used for comparisons.
      • F_TOLERANCE

        public static final float F_TOLERANCE
        The float tolerance used for comparisons.
      • RADTODEG

        public static final double RADTODEG
        Radiants to degrees conversion factor.
        See Also:
        Constant Field Values
    • Constructor Detail

      • NumericsUtilities

        public NumericsUtilities()
    • Method Detail

      • getMachineDEpsilon

        public static double getMachineDEpsilon()
        Getter for the calculated machine double epsilon.
        Returns:
        the machine epsilon for double values.
      • machineFEpsilon

        public static float machineFEpsilon()
        Getter for the calculated machine float epsilon.
        Returns:
        the machine epsilon for float values.
      • dEq

        public static boolean dEq​(double a,
                                  double b)
        Returns true if two doubles are considered equal based on a tolerance of .

        Note that two Double.NaN are seen as equal and return true.

        Parameters:
        a - double to compare.
        b - double to compare.
        Returns:
        true if two doubles are considered equal.
      • dEq

        public static boolean dEq​(double a,
                                  double b,
                                  double epsilon)
        Returns true if two doubles are considered equal based on an supplied epsilon.

        Note that two Double.NaN are seen as equal and return true.

        Parameters:
        a - double to compare.
        b - double to compare.
        Returns:
        true if two doubles are considered equal.
      • fEq

        public static boolean fEq​(float a,
                                  float b)
        Returns true if two floats are considered equal based on a tolerance of .

        Note that two Float.NaN are seen as equal and return true.

        Parameters:
        a - float to compare.
        b - float to compare.
        Returns:
        true if two floats are considered equal.
      • fEq

        public static boolean fEq​(float a,
                                  float b,
                                  float epsilon)
        Returns true if two floats are considered equal based on an supplied epsilon.

        Note that two Float.NaN are seen as equal and return true.

        Parameters:
        a - float to compare.
        b - float to compare.
        Returns:
        true if two float are considered equal.
      • isNumber

        public static <T extends Number> T isNumber​(String value,
                                                    Class<T> adaptee)
        Checks if a string is a number (currently Double, Float, Integer).
        Type Parameters:
        T -
        Parameters:
        value - the string to check.
        adaptee - the class to check against. If null, the more permissive Double will be used.
        Returns:
        the number or null, if the parsing fails.
      • pythagoras

        public static double pythagoras​(double d1,
                                        double d2)
        Calculates the hypothenuse as of the Pythagorean theorem.
        Parameters:
        d1 - the length of the first leg.
        d2 - the length of the second leg.
        Returns:
        the length of the hypothenuse.
      • isBetween

        public static boolean isBetween​(double value,
                                        double... ranges)
        Check if value is inside a ND interval (bounds included).
        Parameters:
        value - the value to check.
        ranges - the bounds (low1, high1, low2, high2, ...)
        Returns:
        true if value lies inside the interval.
      • gamma

        public static double gamma​(double x)
      • normalize

        public static double normalize​(double max,
                                       double min,
                                       double value,
                                       double normMax)
        Normalizes a value for a given normalization max (assuming the lower is 0).
        Parameters:
        max - the max of the sampling values.
        min - the min of the sampling values.
        value - the current value from the sampling values to be normalized.
        normMax - the normalization maximum to use.
        Returns:
        the normalized value.
      • getNegativeRanges

        public static List<int[]> getNegativeRanges​(double[] x)
        Get the range index for which x is negative.
        Parameters:
        x -
        Returns:
      • range2Bins

        public static double[] range2Bins​(double min,
                                          double max,
                                          int binsNum)
        Creates an array of equal bin from a range and the number of bins.
        Parameters:
        min - the min value.
        max - the max value.
        binsNum - the number of wanted bins.
        Returns:
        the array of bin bounds of size [binNum+1], since it contains the min and max.
      • range2Bins

        public static double[] range2Bins​(double min,
                                          double max,
                                          double step,
                                          boolean doLastEqual)
        Creates an array of bins from a range and a step to use.

        Note that if the step doesn't split the range exactly, the last bin will be smaller if doLastEqual is set to false.

        Parameters:
        min - the min value.
        max - the max value.
        step - the wanted size of the bins.
        doLastEqual - make also the last bin of equal step size.
        Returns:
        the array of bin bounds.