Class MathTools


  • public final class MathTools
    extends java.lang.Object
    A collection of useful static routines of a general mathematical nature. This file includes functions that accomplish all types of wondrous mathematical stuff.

    Modified by: Joseph A. Huwaldt

    Version:
    December 4, 2023
    Author:
    Joseph A. Huwaldt, Date: September 29, 1997
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double EPS
      The machine epsilon (macheps) or unit roundoff for double in the Java environment.
      static float EPSF
      The machine epsilon (macheps) or unit roundoff for float in the Java environment.
      static double LOG10
      The natural logarithm of 10.
      static double LOG2
      The natural logarithm of 2.
      static double MAX_LOG
      The natural logarithm of the maximum double value: log(MAX_VALUE).
      static double MIN_LOG
      The natural logarithm of the minimum double value: log(MIN_VALUE).
      static double SQRT_EPS
      Square-root of the machine epsilon for double.
      static float SQRT_EPSF
      Square-root of the machine epsilon for float.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static double acosh​(double x)
      Returns the inverse hyperbolic cosine of the specified argument.
      static double asinh​(double xx)
      Returns the inverse hyperbolic sine of the specified argument.
      static double atanh​(double x)
      Returns the inverse hyperbolic tangent of the specified argument.
      static long bin2dec​(java.util.BitSet bits)
      Converts binary number to it's base 10 decimal equivalent.
      static double cosh​(double x)
      Deprecated. 
      static double cubeRoot​(double x)
      Deprecated. 
      static void dec2bin​(int decimal, java.util.BitSet bits)
      Converts a positive decimal number to it's binary equivalent.
      static double epsilon​(double x)
      Returns the smallest roundoff in quantities of size x, EPSILON, such that x + EPSILON > x.
      static float epsilon​(float x)
      Returns the smallest roundoff in quantities of size x, EPSILON, such that x + EPSILON > x.
      static boolean even​(long n)
      Test to see if a given long integer is even.
      static java.math.BigInteger factorial​(int n)
      Returns the factorial of n (usually written as n!) for any value of n.
      static double frac​(double x)
      Returns the fractional part of a floating point number (removes the integer part).
      static long greatestCommonDivisor​(long xval, long yval)
      Calculates the greatest common divisor between two input integers.
      static int intFactorial​(int n)
      Returns the factorial of n (usually written as n!) for values of n up to 12.
      static boolean isApproxEqual​(double a, double b)
      Returns true if the two supplied numbers are approximately equal to within machine precision.
      static boolean isApproxEqual​(double a, double b, double tol)
      Returns true if the two supplied numbers are approximately equal to within the specified tolerance.
      static boolean isApproxEqual​(float a, float b)
      Returns true if the two supplied numbers are approximately equal to within machine precision.
      static boolean isApproxEqual​(float a, float b, float tol)
      Returns true if the two supplied numbers are approximately equal to within the specified tolerance.
      static boolean isApproxZero​(double a)
      Returns true if the supplied number is approximately zero to within machine precision.
      static boolean isApproxZero​(double a, double tol)
      Returns true if the supplied number is approximately zero to within the specified tolerance.
      static boolean isApproxZero​(float a)
      Returns true if the supplied number is approximately zero to within machine precision.
      static boolean isApproxZero​(float a, float tol)
      Returns true if the supplied number is approximately zero to within the specified tolerance.
      static double lineInterp​(double x1, double y1, double x2, double y2, double x)
      Straight linear 1D interpolation between two points.
      static double lnGamma​(double value)
      Returns the natural log of the Gamma Function defined by the integral: Gamma(z) = integral from 0 to infinity of t^(z-1)*e^-t dt.
      static double log10​(double x)
      Deprecated. 
      static double log2​(double x)
      Find the base 2 logarithm of the given double.
      static void main​(java.lang.String[] args)
      Used to test out the methods in this class.
      static boolean odd​(long n)
      Test to see if a given long integer is odd.
      static long pow​(long a, long b)
      Returns a number "a" raised to the power "b".
      static double pow10​(int x)
      Raises 10 to the small integer power indicated (eg: 10^5 = 100000).
      static long pow2​(long x)
      Raises 2 to the small integer power indicated (eg: 2^3 = 8).
      static double roundDownToPlace​(double value, int place)
      Rounds a floating point number down to the desired decimal place.
      static double roundToPlace​(double value, int place)
      Rounds a floating point number to the desired decimal place.
      static double roundToSigFig​(double value, int sigFig)
      Rounds a floating point number to the desired number of significant digits.
      static double roundUpToPlace​(double value, int place)
      Rounds a floating point number up to the desired decimal place.
      static double sign​(double a, double b)
      Returns the absolute value of "a" times the sign of "b".
      static float sign​(float a, double b)
      Returns the absolute value of "a" times the sign of "b".
      static int sign​(int a, double b)
      Returns the absolute value of "a" times the sign of "b".
      static long sign​(long a, double b)
      Returns the absolute value of "a" times the sign of "b".
      static double sinh​(double x)
      Deprecated. 
      static double sqr​(double x)
      Calculates the square (x^2) of the argument.
      static double tanh​(double x)
      Deprecated. 
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOG10

        public static final double LOG10
        The natural logarithm of 10.
      • LOG2

        public static final double LOG2
        The natural logarithm of 2.
      • MAX_LOG

        public static final double MAX_LOG
        The natural logarithm of the maximum double value: log(MAX_VALUE).
      • MIN_LOG

        public static final double MIN_LOG
        The natural logarithm of the minimum double value: log(MIN_VALUE).
      • EPS

        public static final double EPS
        The machine epsilon (macheps) or unit roundoff for double in the Java environment. Machine epsilon gives an upper bound on the relative error due to rounding in floating point arithmetic. Machine epsilon is the smallest number such that (1.0 + EPS != 1.0).
      • SQRT_EPS

        public static final double SQRT_EPS
        Square-root of the machine epsilon for double.
      • EPSF

        public static final float EPSF
        The machine epsilon (macheps) or unit roundoff for float in the Java environment. Machine epsilon gives an upper bound on the relative error due to rounding in floating point arithmetic. Machine epsilon is the smallest number such that (1F + EPSF != 1F).
      • SQRT_EPSF

        public static final float SQRT_EPSF
        Square-root of the machine epsilon for float.
    • Method Detail

      • even

        public static boolean even​(long n)
        Test to see if a given long integer is even.
        Parameters:
        n - Integer number to be tested.
        Returns:
        True if the number is even, false if it is odd.
      • odd

        public static boolean odd​(long n)
        Test to see if a given long integer is odd.
        Parameters:
        n - Integer number to be tested.
        Returns:
        True if the number is odd, false if it is even.
      • sqr

        public static double sqr​(double x)
        Calculates the square (x^2) of the argument.
        Parameters:
        x - Argument to be squared.
        Returns:
        Returns the square (x^2) of the argument.
      • cubeRoot

        @Deprecated
        public static double cubeRoot​(double x)
        Deprecated.
        Computes the cube root of the specified real number. If the argument is negative, then the cube root is negative.
        Parameters:
        x - Argument for which the cube root is to be found.
        Returns:
        The cube root of the argument is returned.
        See Also:
        Math.cbrt(double)
      • pow

        public static long pow​(long a,
                               long b)
                        throws java.lang.ArithmeticException
        Returns a number "a" raised to the power "b". A "long" version of Math.pow(). This is much faster than using Math.pow() if the operands are integers.
        Parameters:
        a - Number to be raised to the power "b".
        b - Power to raise number "a" to.
        Returns:
        A long integer "a" raised to the integer power "b".
        Throws:
        java.lang.ArithmeticException - if "b" is negative.
      • pow2

        public static long pow2​(long x)
        Raises 2 to the small integer power indicated (eg: 2^3 = 8). This is MUCH faster than calling Math.pow(2, x).
        Parameters:
        x - Amount to raise 2 to the power of.
        Returns:
        Returns 2 raised to the power indicated.
      • pow10

        public static double pow10​(int x)
        Raises 10 to the small integer power indicated (eg: 10^5 = 100000). This is faster than calling Math.pow(10, x).
        Parameters:
        x - Amount to raise 10 to the power of.
        Returns:
        Returns 10 raised to the power indicated.
      • log10

        @Deprecated
        public static double log10​(double x)
        Deprecated.
        Find the base 10 logarithm of the given double.
        Parameters:
        x - Value to find the base 10 logarithm of.
        Returns:
        The base 10 logarithm of x.
        See Also:
        Math.log10(double)
      • log2

        public static double log2​(double x)
        Find the base 2 logarithm of the given double.
        Parameters:
        x - Value to find the base 2 logarithm of.
        Returns:
        The base 2 logarithm of x.
      • roundToPlace

        public static double roundToPlace​(double value,
                                          int place)
        Rounds a floating point number to the desired decimal place. Example: 1346.4667 rounded to the 2nd place = 1300.
        Parameters:
        value - The value to be rounded.
        place - Number of decimal places to round value to. A place of 1 rounds to 10's place, 2 to 100's place, -2 to 1/100th place, et cetera.
        Returns:
        A floating point number rounded to the desired decimal place.
        See Also:
        roundToSigFig(double, int), roundDownToPlace(double, int), roundUpToPlace(double, int)
      • roundUpToPlace

        public static double roundUpToPlace​(double value,
                                            int place)
        Rounds a floating point number up to the desired decimal place. Example: 1346.4667 rounded up to the 2nd place = 1400.
        Parameters:
        value - The value to be rounded up.
        place - Number of decimal places to round value to. A place of 1 rounds to 10's place, 2 to 100's place, -2 to 1/100th place, et cetera.
        Returns:
        A floating point number rounded up to the desired decimal place.
        See Also:
        roundToPlace(double, int), roundDownToPlace(double, int)
      • roundDownToPlace

        public static double roundDownToPlace​(double value,
                                              int place)
        Rounds a floating point number down to the desired decimal place. Example: 1346.4667 rounded down to the 1st place = 1340.
        Parameters:
        value - The value to be rounded down.
        place - Number of decimal places to round value to. A place of 1 rounds to 10's place, 2 to 100's place, -2 to 1/100th place, et cetera.
        Returns:
        A floating point number rounded down to the desired decimal place.
        See Also:
        roundToPlace(double, int), roundUpToPlace(double, int)
      • roundToSigFig

        public static double roundToSigFig​(double value,
                                           int sigFig)
        Rounds a floating point number to the desired number of significant digits. For example: 1346.4667 rounded to 3 significant digits is 1350.0.
        Parameters:
        value - The value to be rounded.
        sigFig - The number of significant digits/figures to retain.
        Returns:
        A floating point number rounded to the specified number of significant digits.
        See Also:
        roundToPlace(double, int)
      • factorial

        public static java.math.BigInteger factorial​(int n)
        Returns the factorial of n (usually written as n!) for any value of n. The factorial of n is the product of all integers up to and including n.
        Parameters:
        n - The number to calculate the factorial for.
        Returns:
        The factorial of n.
        See Also:
        intFactorial(int)
      • intFactorial

        public static int intFactorial​(int n)
        Returns the factorial of n (usually written as n!) for values of n up to 12. The factorial of n is the product of all integers up to and including n. This method is more efficient than "factorial()" for values of n ≤ 12. Values of n > 12 cause a numerical overflow with this method. Use "factorial()" for larger values of n (or any value of n where performance is not critical).
        Parameters:
        n - The number to calculate the factorial for (0 ≤ n ≤ 12).
        Returns:
        The factorial of n.
        See Also:
        factorial(int)
      • greatestCommonDivisor

        public static long greatestCommonDivisor​(long xval,
                                                 long yval)
        Calculates the greatest common divisor between two input integers. The GCD is the largest number that can be divided into both input numbers.
        Parameters:
        xval - First integer
        yval - Second integer
        Returns:
        The largest number that can be divided into both input values.
      • frac

        public static double frac​(double x)
        Returns the fractional part of a floating point number (removes the integer part).
        Parameters:
        x - Argument for which the fractional part is to be returned.
        Returns:
        The fractional part of the argument is returned.
      • lineInterp

        public static double lineInterp​(double x1,
                                        double y1,
                                        double x2,
                                        double y2,
                                        double x)
        Straight linear 1D interpolation between two points.
        Parameters:
        x1 - X-coordinate of the 1st point (the high point).
        y1 - Y-coordinate of the 1st point (the high point).
        x2 - X-coordinate of the 2nd point (the low point).
        y2 - Y-coordinate of the 2nd point (the low point).
        x - The X coordinate of the point for which we want to interpolate to determine a Y coordinate. Will extrapolate if X is outside of the bounds of the point arguments.
        Returns:
        The linearly interpolated Y value corresponding to the input X value is returned.
      • dec2bin

        public static void dec2bin​(int decimal,
                                   java.util.BitSet bits)
        Converts a positive decimal number to it's binary equivalent.
        Parameters:
        decimal - The positive decimal number to be encoded in binary.
        bits - The bitset to encode the number in.
      • bin2dec

        public static long bin2dec​(java.util.BitSet bits)
        Converts binary number to it's base 10 decimal equivalent.
        Parameters:
        bits - The bitset that encodes the number to be converted.
        Returns:
        Returns the decimal equivalent of the given binary number.
      • cosh

        @Deprecated
        public static double cosh​(double x)
        Deprecated.
        Return the hyperbolic cosine of the specified argument.
        Parameters:
        x - The number whose hyperbolic cosine is to be returned.
        Returns:
        The hyperbolic cosine of x.
        See Also:
        Math.cosh(double)
      • sinh

        @Deprecated
        public static double sinh​(double x)
        Deprecated.
        Return the hyperbolic sine of the specified argument.
        Parameters:
        x - The number whose hyperbolic sine is to be returned.
        Returns:
        The hyperbolic sine of x.
        See Also:
        Math.sinh(double)
      • tanh

        @Deprecated
        public static double tanh​(double x)
        Deprecated.
        Returns the hyperbolic tangent of the specified argument.
        Parameters:
        x - The number whose hyperbolic tangent is to be returned.
        Returns:
        The hyperbolic tangent of x.
        See Also:
        Math.tanh(double)
      • acosh

        public static double acosh​(double x)
        Returns the inverse hyperbolic cosine of the specified argument. The inverse hyperbolic cosine is defined as: acosh(x) = log(x + sqrt((x-1)*(x+1)))
        Parameters:
        x - Value to return inverse hyperbolic cosine of.
        Returns:
        The inverse hyperbolic cosine of x.
        Throws:
        java.lang.IllegalArgumentException - if x is less than 1.0.
      • asinh

        public static double asinh​(double xx)
        Returns the inverse hyperbolic sine of the specified argument. The inverse hyperbolic sine is defined as: asinh(x) = log(x + sqrt(1 + x*x))
        Parameters:
        xx - Value to return inverse hyperbolic cosine of.
        Returns:
        The inverse hyperbolic sine of x.
      • atanh

        public static double atanh​(double x)
        Returns the inverse hyperbolic tangent of the specified argument. The inverse hyperbolic tangent is defined as: atanh(x) = 0.5*log((1 + x)/(1 - x))
        Parameters:
        x - Value to return inverse hyperbolic cosine of.
        Returns:
        The inverse hyperbolic tangent of x.
        Throws:
        java.lang.IllegalArgumentException - if x is outside the range -1, to +1.
      • sign

        public static double sign​(double a,
                                  double b)
        Returns the absolute value of "a" times the sign of "b".
        Parameters:
        a - The value for which the magnitude is returned.
        b - The value for which the sign is returned.
        Returns:
        The absolute value of "a" times the sign of "b".
      • sign

        public static float sign​(float a,
                                 double b)
        Returns the absolute value of "a" times the sign of "b".
        Parameters:
        a - The value for which the magnitude is returned.
        b - The value for which the sign is returned.
        Returns:
        The absolute value of "a" times the sign of "b".
      • sign

        public static long sign​(long a,
                                double b)
        Returns the absolute value of "a" times the sign of "b".
        Parameters:
        a - The value for which the magnitude is returned.
        b - The value for which the sign is returned.
        Returns:
        The absolute value of "a" times the sign of "b".
      • sign

        public static int sign​(int a,
                               double b)
        Returns the absolute value of "a" times the sign of "b".
        Parameters:
        a - The value for which the magnitude is returned.
        b - The value for which the sign is returned.
        Returns:
        The absolute value of "a" times the sign of "b".
      • lnGamma

        public static double lnGamma​(double value)
        Returns the natural log of the Gamma Function defined by the integral: Gamma(z) = integral from 0 to infinity of t^(z-1)*e^-t dt.

        It is better to implement ln(Gamma(x)) rather than Gamma(x) since the latter will overflow most computer floating point representations at quite modest values of x.

        Parameters:
        value - The value to evaluate the log of the gamma function for.
        Returns:
        The natural log of the Gamma Function.
      • epsilon

        public static double epsilon​(double x)
        Returns the smallest roundoff in quantities of size x, EPSILON, such that x + EPSILON > x. This is the Units in the Last Place (ULP) and is different than the machine roundoff EPS (macheps).
        Parameters:
        x - The value to return the roundoff quantity for.
        Returns:
        The smallest roundoff in quantities of size x, EPSILON, such that x + EPSILON > x
        See Also:
        EPS, Math.ulp(double)
      • epsilon

        public static float epsilon​(float x)
        Returns the smallest roundoff in quantities of size x, EPSILON, such that x + EPSILON > x. This is the Units in the Last Place (ULP) and is different than the machine roundoff EPS (macheps).
        Parameters:
        x - The value to return the roundoff quantity for.
        Returns:
        The smallest roundoff in quantities of size x, EPSILON, such that x + EPSILON > x
        See Also:
        EPSF, Math.ulp(float)
      • isApproxEqual

        public static boolean isApproxEqual​(double a,
                                            double b)
        Returns true if the two supplied numbers are approximately equal to within machine precision.
        Parameters:
        a - The 1st value to compare for approximate equality.
        b - The 2nd value to compare for approximate equality.
        Returns:
        true if the two supplied numbers are approximately equal to within machine precision
      • isApproxEqual

        public static boolean isApproxEqual​(double a,
                                            double b,
                                            double tol)
        Returns true if the two supplied numbers are approximately equal to within the specified tolerance.
        Parameters:
        a - The 1st value to compare for approximate equality.
        b - The 2nd value to compare for approximate equality.
        tol - The tolerance for equality.
        Returns:
        true if the two supplied numbers are approximately equal to within the specified tolerance
      • isApproxEqual

        public static boolean isApproxEqual​(float a,
                                            float b)
        Returns true if the two supplied numbers are approximately equal to within machine precision.
        Parameters:
        a - The 1st value to compare for approximate equality.
        b - The 2nd value to compare for approximate equality.
        Returns:
        true if the two supplied numbers are approximately equal to within machine precision
      • isApproxEqual

        public static boolean isApproxEqual​(float a,
                                            float b,
                                            float tol)
        Returns true if the two supplied numbers are approximately equal to within the specified tolerance.
        Parameters:
        a - The 1st value to compare for approximate equality.
        b - The 2nd value to compare for approximate equality.
        tol - The tolerance for equality.
        Returns:
        true if the two supplied numbers are approximately equal to within the specified tolerance
      • isApproxZero

        public static boolean isApproxZero​(double a)
        Returns true if the supplied number is approximately zero to within machine precision.
        Parameters:
        a - The value to be compared with zero.
        Returns:
        true if the supplied number is approximately zero to within machine precision
      • isApproxZero

        public static boolean isApproxZero​(double a,
                                           double tol)
        Returns true if the supplied number is approximately zero to within the specified tolerance.
        Parameters:
        a - The value to be compared with zero.
        tol - The tolerance for equality.
        Returns:
        true if the supplied number is approximately zero to within the specified tolerance
      • isApproxZero

        public static boolean isApproxZero​(float a)
        Returns true if the supplied number is approximately zero to within machine precision.
        Parameters:
        a - The value to be compared with zero.
        Returns:
        true if the supplied number is approximately zero to within machine precision
      • isApproxZero

        public static boolean isApproxZero​(float a,
                                           float tol)
        Returns true if the supplied number is approximately zero to within the specified tolerance.
        Parameters:
        a - The value to be compared with zero.
        tol - The tolerance for equality.
        Returns:
        true if the supplied number is approximately zero to within the specified tolerance
      • main

        public static void main​(java.lang.String[] args)
        Used to test out the methods in this class.
        Parameters:
        args - Command line arguments (not used).