Class JSLMath


  • public final class JSLMath
    extends java.lang.Object
    This class implements additional mathematical functions and determines the parameters of the floating point representation.

    This is based on the DhbMath class of Didier Besset in "Object-Oriented Implementation of Numerical Methods", Morgan-Kaufmann

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double binomialCoefficient​(int n, int k)
      Computes the binomial coefficient.
      static boolean equal​(double a, double b)
      Compares two numbers a and b and checks if they are within the default numerical precision of each other.
      static boolean equal​(double a, double b, double precision)
      Compares two numbers a and b and checks if they are within the supplied precision of each other.
      static double factorial​(int n)
      Returns the factorial (n!) of the number
      static double getDefaultNumericalPrecision()
      Gets the default numerical precision.
      static double getLargestExponentialArgument()
      Computes the largest exponent argument
      static double getLargestNumber()
      Gets the largest positive number that can be represented
      static double getMachinePrecision()
      Gets the largest positive number that when added to 1 yields 1
      static int getMaxNumIterations()
      Gets the default maximum number of iterations A constant that can be used in algorithms to specify the maximum number of iterations.
      static double getNegativeMachinePrecision()
      Gets the largest positive number that when subtracted from 1 yield 1
      static int getRadix()
      Gets the radix of the floating point representation
      static double getSmallestExponentialArgument()
      Computes the smallest exponent argument
      static double getSmallestNumber()
      Gets the smallest positive number different from 0.0
      static double getSmallNumber()
      Returns the number that can be added to some value without noticeably changing the result of the computation
      static double logFactorial​(int n)
      Computes the natural logarithm of the factorial operator.
      static void printParameters​(java.io.PrintStream printStream)
      Computes and prints the mathematical precision parameters to the supplied PrintStream
      static void reset()
      Resets the constants.
      static double roundTo​(double value, double scale)
      This method returns the specified value rounded to the nearest integer multiple of the specified scale.
      static double roundToScale​(double value, boolean integerValued)
      Round the specified value upward to the next scale value.
      static void setMaxNumIterations​(int iterations)
      Sets the default maximum number of iterations A constant that can be used in algorithms to specify the maximum number of iterations.
      static double sign​(double x)
      Get the sign of the number based on the equal() method Equal is 0.0, positive is 1.0, negative is -1.0
      static boolean within​(double a, double b, double precision)
      Returns true if Math.abs(a-b) < precision
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getDefaultNumericalPrecision

        public static double getDefaultNumericalPrecision()
        Gets the default numerical precision. This represents an estimate of the precision expected for a general numerical computation. For example, two numbers x and y can be considered equal if the relative difference between them is less than the default numerical precision. This value has been defined as the square root of the machine precision
        Returns:
        the default numerical precision
      • getMaxNumIterations

        public static int getMaxNumIterations()
        Gets the default maximum number of iterations A constant that can be used in algorithms to specify the maximum number of iterations. This is static and thus a change will change it for any algorithm that depends on this constant
        Returns:
        the maximum number of iterations
      • setMaxNumIterations

        public static void setMaxNumIterations​(int iterations)
        Sets the default maximum number of iterations A constant that can be used in algorithms to specify the maximum number of iterations. This is static and thus a change will change it for any algorithm that depends on this constant
        Parameters:
        iterations - the number of iterations
      • equal

        public static boolean equal​(double a,
                                    double b)
        Compares two numbers a and b and checks if they are within the default numerical precision of each other.
        Parameters:
        a - double
        b - double
        Returns:
        boolean true if the difference between a and b is less than the default numerical precision
      • equal

        public static boolean equal​(double a,
                                    double b,
                                    double precision)
        Compares two numbers a and b and checks if they are within the supplied precision of each other.
        Parameters:
        a - double
        b - double
        precision - double
        Returns:
        boolean true if the relative difference between a and b is less than precision
      • within

        public static boolean within​(double a,
                                     double b,
                                     double precision)
        Returns true if Math.abs(a-b) < precision
        Parameters:
        a - the first number
        b - the second number
        precision - the precision to check
        Returns:
        true if within the precision
      • getLargestExponentialArgument

        public static double getLargestExponentialArgument()
        Computes the largest exponent argument
        Returns:
        the largest exponent argument
      • getSmallestExponentialArgument

        public static double getSmallestExponentialArgument()
        Computes the smallest exponent argument
        Returns:
        the smallest exponent argument
      • getLargestNumber

        public static double getLargestNumber()
        Gets the largest positive number that can be represented
        Returns:
        the largest positive number that can be represented
      • getMachinePrecision

        public static double getMachinePrecision()
        Gets the largest positive number that when added to 1 yields 1
        Returns:
        the largest positive number that when added to 1 yields 1
      • getNegativeMachinePrecision

        public static double getNegativeMachinePrecision()
        Gets the largest positive number that when subtracted from 1 yield 1
        Returns:
        the largest positive number that when subtracted from 1 yield 1
      • getRadix

        public static int getRadix()
        Gets the radix of the floating point representation
        Returns:
        the radix of the floating point representation
      • getSmallestNumber

        public static double getSmallestNumber()
        Gets the smallest positive number different from 0.0
        Returns:
        the smallest positive number different from 0.0
      • printParameters

        public static void printParameters​(java.io.PrintStream printStream)
        Computes and prints the mathematical precision parameters to the supplied PrintStream
        Parameters:
        printStream - the stream to write to
      • reset

        public static void reset()
        Resets the constants. They will be recomputed at next usage
      • roundTo

        public static double roundTo​(double value,
                                     double scale)
        This method returns the specified value rounded to the nearest integer multiple of the specified scale.
        Parameters:
        value - number to be rounded
        scale - defining the rounding scale
        Returns:
        rounded value
      • roundToScale

        public static double roundToScale​(double value,
                                          boolean integerValued)
        Round the specified value upward to the next scale value.
        Parameters:
        value - the value to be rounded.
        integerValued - fag specified whether integer scale are used, otherwise double scale is used.
        Returns:
        a number rounded upward to the next scale value.
      • getSmallNumber

        public static double getSmallNumber()
        Returns the number that can be added to some value without noticeably changing the result of the computation
        Returns:
        the number that can be added to some value without noticeably changing the result of the computation
      • sign

        public static double sign​(double x)
        Get the sign of the number based on the equal() method Equal is 0.0, positive is 1.0, negative is -1.0
        Parameters:
        x - the number
        Returns:
        the sign of the number
      • factorial

        public static double factorial​(int n)
        Returns the factorial (n!) of the number
        Parameters:
        n - The number to take the factorial of
        Returns:
        The factorial of the number.
      • binomialCoefficient

        public static double binomialCoefficient​(int n,
                                                 int k)
        Computes the binomial coefficient. Computes the number of combinations of size k that can be formed from n distinct objects.
        Parameters:
        n - The total number of distinct items
        k - The number of subsets
        Returns:
        the binomial coefficient
      • logFactorial

        public static double logFactorial​(int n)
        Computes the natural logarithm of the factorial operator. ln(n!)
        Parameters:
        n - The value to be operated on.
        Returns:
        the log of the factorial