Class BigDecimalMath


  • public class BigDecimalMath
    extends java.lang.Object
    Provides advanced functions operating on BigDecimals.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private BigDecimalMath()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.math.BigDecimal acos​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the arc cosine (inverted cosine) of BigDecimal x.
      static java.math.BigDecimal acosh​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the arc hyperbolic cosine (inverse hyperbolic cosine) of BigDecimal x.
      static java.math.BigDecimal acot​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the inverse cotangens (arc cotangens) of BigDecimal x.
      static java.math.BigDecimal acoth​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the arc hyperbolic cotangens (inverse hyperbolic cotangens) of BigDecimal x.
      private static int adjustScale​(int scale, long exp)  
      static java.math.BigDecimal asin​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the arc sine (inverted sine) of BigDecimal x.
      static java.math.BigDecimal asinh​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the arc hyperbolic sine (inverse hyperbolic sine) of BigDecimal x.
      static java.math.BigDecimal atan​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the arc tangens (inverted tangens) of BigDecimal x.
      static java.math.BigDecimal atan2​(java.math.BigDecimal y, java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the arc tangens (inverted tangens) of BigDecimal y / x in the range -pi to pi.
      static java.math.BigDecimal atanh​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the arc hyperbolic tangens (inverse hyperbolic tangens) of BigDecimal x.
      static java.math.BigDecimal bernoulli​(int n, java.math.MathContext mathContext)
      Calculates the Bernoulli number for the specified index.
      private static void checkMathContext​(java.math.MathContext mathContext)  
      static java.math.BigDecimal cos​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the cosine (cosinus) of BigDecimal x.
      static java.math.BigDecimal cosh​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the hyperbolic cosine of BigDecimal x.
      static java.math.BigDecimal cot​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the cotangens of BigDecimal x.
      static java.math.BigDecimal coth​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the hyperbolic cotangens of BigDecimal x.
      static java.math.BigDecimal e​(java.math.MathContext mathContext)
      Returns the number e.
      static java.math.BigDecimal exp​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the natural exponent of BigDecimal x (ex).
      private static java.math.BigDecimal expIntegralFractional​(java.math.BigDecimal x, java.math.MathContext mathContext)  
      static int exponent​(java.math.BigDecimal value)
      Returns the exponent of the specified BigDecimal written as mantissa * 10exponent.
      private static java.math.BigDecimal expTaylor​(java.math.BigDecimal x, java.math.MathContext mathContext)  
      static java.math.BigDecimal factorial​(int n)
      Calculates the factorial of the specified integer argument.
      static java.math.BigDecimal factorial​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the factorial of the specified BigDecimal.
      private static java.math.BigDecimal factorialLoop​(int n1, int n2)  
      private static java.math.BigDecimal factorialRecursion​(int n1, int n2)  
      static java.math.BigDecimal fractionalPart​(java.math.BigDecimal value)
      Returns the fractional part of the specified BigDecimal (right of the decimal point).
      static java.math.BigDecimal gamma​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the gamma function of the specified BigDecimal.
      (package private) static java.util.List<java.math.BigDecimal> getSpougeFactorialConstants​(int a)  
      static java.math.BigDecimal integralPart​(java.math.BigDecimal value)
      Returns the integral part of the specified BigDecimal (left of the decimal point).
      static boolean isDoubleValue​(java.math.BigDecimal value)
      Returns whether the specified BigDecimal value can be represented as double.
      static boolean isIntValue​(java.math.BigDecimal value)
      Returns whether the specified BigDecimal value can be represented as int.
      static boolean isLongValue​(java.math.BigDecimal value)
      Returns whether the specified BigDecimal value can be represented as long.
      static java.math.BigDecimal log​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the natural logarithm of BigDecimal x.
      static java.math.BigDecimal log10​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the logarithm of BigDecimal x to the base 10.
      static java.math.BigDecimal log2​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the logarithm of BigDecimal x to the base 2.
      private static java.math.BigDecimal logTen​(java.math.MathContext mathContext)  
      private static java.math.BigDecimal logThree​(java.math.MathContext mathContext)  
      private static java.math.BigDecimal logTwo​(java.math.MathContext mathContext)  
      private static java.math.BigDecimal logUsingExponent​(java.math.BigDecimal x, java.math.MathContext mathContext)  
      private static java.math.BigDecimal logUsingNewton​(java.math.BigDecimal x, java.math.MathContext mathContext)  
      private static java.math.BigDecimal logUsingTwoThree​(java.math.BigDecimal x, java.math.MathContext mathContext)  
      static java.math.BigDecimal mantissa​(java.math.BigDecimal value)
      Returns the mantissa of the specified BigDecimal written as mantissa * 10exponent.
      static java.math.BigDecimal pi​(java.math.MathContext mathContext)
      Returns the number pi.
      private static java.math.BigDecimal piChudnovski​(java.math.MathContext mathContext)  
      static java.math.BigDecimal pow​(java.math.BigDecimal x, long y, java.math.MathContext mathContext)
      Calculates BigDecimal x to the power of long y (xy).
      static java.math.BigDecimal pow​(java.math.BigDecimal x, java.math.BigDecimal y, java.math.MathContext mathContext)
      Calculates BigDecimal x to the power of BigDecimal y (xy).
      private static java.math.BigDecimal powInteger​(java.math.BigDecimal x, java.math.BigDecimal integerY, java.math.MathContext mathContext)
      Calculates BigDecimal x to the power of the integer value y (xy).
      static java.math.BigDecimal reciprocal​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the reciprocal of the specified BigDecimal.
      static java.math.BigDecimal root​(java.math.BigDecimal x, java.math.BigDecimal n, java.math.MathContext mathContext)
      Calculates the n'th root of BigDecimal x.
      private static java.math.BigDecimal rootUsingNewtonRaphson​(java.math.BigDecimal x, java.math.BigDecimal n, java.math.BigDecimal initialResult, java.math.MathContext mathContext)  
      static java.math.BigDecimal round​(java.math.BigDecimal value, java.math.MathContext mathContext)
      Rounds the specified BigDecimal to the precision of the specified MathContext.
      static java.math.BigDecimal roundWithTrailingZeroes​(java.math.BigDecimal value, java.math.MathContext mathContext)
      Rounds the specified BigDecimal to the precision of the specified MathContext including trailing zeroes.
      static int significantDigits​(java.math.BigDecimal value)
      Returns the number of significant digits of the specified BigDecimal.
      static java.math.BigDecimal sin​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the sine (sinus) of BigDecimal x.
      static java.math.BigDecimal sinh​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the hyperbolic sine of BigDecimal x.
      static java.math.BigDecimal sqrt​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the square root of BigDecimal x.
      static java.math.BigDecimal tan​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the tangens of BigDecimal x.
      static java.math.BigDecimal tanh​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Calculates the hyperbolic tangens of BigDecimal x.
      static java.math.BigDecimal toBigDecimal​(java.lang.String string)
      Creates a BigDecimal from the specified String representation.
      static java.math.BigDecimal toBigDecimal​(java.lang.String string, java.math.MathContext mathContext)
      Creates a BigDecimal from the specified String representation.
      (package private) static java.math.BigDecimal toBigDecimal​(java.lang.String string, java.math.MathContext mathContext, int splitLength)  
      private static java.math.BigDecimal toBigDecimalRecursive​(char[] chars, int offset, int length, int scale, int splitLength)  
      static java.math.BigDecimal toDegrees​(java.math.BigDecimal x, java.math.MathContext mathContext)
      Converts an angle measured in radians to an approximately equivalent angle measured in degrees.
      static java.math.BigDecimal toRadians​(java.math.BigDecimal x, java.math.MathContext mathContext)
      /** Converts an angle measured in degrees to an approximately equivalent angle measured in radians.
      • Methods inherited from class java.lang.Object

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

      • TWO

        private static final java.math.BigDecimal TWO
      • THREE

        private static final java.math.BigDecimal THREE
      • MINUS_ONE

        private static final java.math.BigDecimal MINUS_ONE
      • ONE_HALF

        private static final java.math.BigDecimal ONE_HALF
      • ONE_HUNDRED_EIGHTY

        private static final java.math.BigDecimal ONE_HUNDRED_EIGHTY
      • DOUBLE_MAX_VALUE

        private static final java.math.BigDecimal DOUBLE_MAX_VALUE
      • log2Cache

        private static volatile java.math.BigDecimal log2Cache
      • log2CacheLock

        private static final java.lang.Object log2CacheLock
      • log3Cache

        private static volatile java.math.BigDecimal log3Cache
      • log3CacheLock

        private static final java.lang.Object log3CacheLock
      • log10Cache

        private static volatile java.math.BigDecimal log10Cache
      • log10CacheLock

        private static final java.lang.Object log10CacheLock
      • piCache

        private static volatile java.math.BigDecimal piCache
      • piCacheLock

        private static final java.lang.Object piCacheLock
      • eCache

        private static volatile java.math.BigDecimal eCache
      • eCacheLock

        private static final java.lang.Object eCacheLock
      • ROUGHLY_TWO_PI

        private static final java.math.BigDecimal ROUGHLY_TWO_PI
      • EXPECTED_INITIAL_PRECISION

        private static final int EXPECTED_INITIAL_PRECISION
        See Also:
        Constant Field Values
      • factorialCache

        private static java.math.BigDecimal[] factorialCache
      • spougeFactorialConstantsCache

        private static final java.util.Map<java.lang.Integer,​java.util.List<java.math.BigDecimal>> spougeFactorialConstantsCache
      • spougeFactorialConstantsCacheLock

        private static final java.lang.Object spougeFactorialConstantsCacheLock
    • Constructor Detail

      • BigDecimalMath

        private BigDecimalMath()
    • Method Detail

      • toBigDecimal

        public static java.math.BigDecimal toBigDecimal​(java.lang.String string)
        Creates a BigDecimal from the specified String representation.

        This method is equivalent to the String constructor BigDecimal(String) but has been optimized for large strings (several thousand digits).

        Parameters:
        string - the String representation
        Returns:
        the created BigDecimal
        Throws:
        java.lang.NumberFormatException - if string is not a valid representation of a BigDecimal
        See Also:
        BigDecimal(String), toBigDecimal(String, MathContext)
      • toBigDecimal

        public static java.math.BigDecimal toBigDecimal​(java.lang.String string,
                                                        java.math.MathContext mathContext)
        Creates a BigDecimal from the specified String representation.

        This method is equivalent to the String constructor BigDecimal(String, MathContext) but has been optimized for large strings (several thousand digits).

        Parameters:
        string - the string representation
        mathContext - the MathContext used for the result
        Returns:
        the created BigDecimal
        Throws:
        java.lang.NumberFormatException - if string is not a valid representation of a BigDecimal
        java.lang.ArithmeticException - if the result is inexact but the rounding mode is UNNECESSARY
        See Also:
        BigDecimal(String, MathContext), toBigDecimal(String)
      • toBigDecimal

        static java.math.BigDecimal toBigDecimal​(java.lang.String string,
                                                 java.math.MathContext mathContext,
                                                 int splitLength)
      • adjustScale

        private static int adjustScale​(int scale,
                                       long exp)
      • toBigDecimalRecursive

        private static java.math.BigDecimal toBigDecimalRecursive​(char[] chars,
                                                                  int offset,
                                                                  int length,
                                                                  int scale,
                                                                  int splitLength)
      • isIntValue

        public static boolean isIntValue​(java.math.BigDecimal value)
        Returns whether the specified BigDecimal value can be represented as int.

        If this returns true you can call BigDecimal.intValueExact() without fear of an ArithmeticException.

        Parameters:
        value - the BigDecimal to check
        Returns:
        true if the value can be represented as int value
      • isLongValue

        public static boolean isLongValue​(java.math.BigDecimal value)
        Returns whether the specified BigDecimal value can be represented as long.

        If this returns true you can call BigDecimal.longValueExact() without fear of an ArithmeticException.

        Parameters:
        value - the BigDecimal to check
        Returns:
        true if the value can be represented as long value
      • isDoubleValue

        public static boolean isDoubleValue​(java.math.BigDecimal value)
        Returns whether the specified BigDecimal value can be represented as double.

        If this returns true you can call BigDecimal.doubleValue() without fear of getting Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY as result.

        Example: BigDecimalMath.isDoubleValue(new BigDecimal("1E309")) returns false, because new BigDecimal("1E309").doubleValue() returns Infinity.

        Note: This method does not check for possible loss of precision.

        For example BigDecimalMath.isDoubleValue(new BigDecimal("1.23400000000000000000000000000000001")) will return true, because new BigDecimal("1.23400000000000000000000000000000001").doubleValue() returns a valid double value, although it loses precision and returns 1.234.

        BigDecimalMath.isDoubleValue(new BigDecimal("1E-325")) will return true although this value is smaller than Double.MIN_VALUE (and therefore outside the range of values that can be represented as double) because new BigDecimal("1E-325").doubleValue() returns 0 which is a legal value with loss of precision.

        Parameters:
        value - the BigDecimal to check
        Returns:
        true if the value can be represented as double value
      • mantissa

        public static java.math.BigDecimal mantissa​(java.math.BigDecimal value)
        Returns the mantissa of the specified BigDecimal written as mantissa * 10exponent.

        The mantissa is defined as having exactly 1 digit before the decimal point.

        Parameters:
        value - the BigDecimal
        Returns:
        the mantissa
        See Also:
        exponent(BigDecimal)
      • exponent

        public static int exponent​(java.math.BigDecimal value)
        Returns the exponent of the specified BigDecimal written as mantissa * 10exponent.

        The mantissa is defined as having exactly 1 digit before the decimal point.

        Parameters:
        value - the BigDecimal
        Returns:
        the exponent
        See Also:
        mantissa(BigDecimal)
      • significantDigits

        public static int significantDigits​(java.math.BigDecimal value)
        Returns the number of significant digits of the specified BigDecimal.

        The result contains the number of all digits before the decimal point and all digits after the decimal point excluding trailing zeroes.

        Examples:

        • significantDigits(new BigDecimal("12300.00")) returns 5
        • significantDigits(new BigDecimal("1.23000")) returns 3
        • significantDigits(new BigDecimal("0.00012300")) returns 3
        • significantDigits(new BigDecimal("12300.4500")) returns 7

        See: Wikipedia: Significant figures

        Parameters:
        value - the BigDecimal
        Returns:
        the number of significant digits
        See Also:
        BigDecimal.stripTrailingZeros(), BigDecimal.precision()
      • integralPart

        public static java.math.BigDecimal integralPart​(java.math.BigDecimal value)
        Returns the integral part of the specified BigDecimal (left of the decimal point).
        Parameters:
        value - the BigDecimal
        Returns:
        the integral part
        See Also:
        fractionalPart(BigDecimal)
      • fractionalPart

        public static java.math.BigDecimal fractionalPart​(java.math.BigDecimal value)
        Returns the fractional part of the specified BigDecimal (right of the decimal point).
        Parameters:
        value - the BigDecimal
        Returns:
        the fractional part
        See Also:
        integralPart(BigDecimal)
      • round

        public static java.math.BigDecimal round​(java.math.BigDecimal value,
                                                 java.math.MathContext mathContext)
        Rounds the specified BigDecimal to the precision of the specified MathContext.

        This method calls BigDecimal.round(MathContext).

        Parameters:
        value - the BigDecimal to round
        mathContext - the MathContext used for the result
        Returns:
        the rounded BigDecimal value
        See Also:
        BigDecimal.round(MathContext), roundWithTrailingZeroes(BigDecimal, MathContext)
      • roundWithTrailingZeroes

        public static java.math.BigDecimal roundWithTrailingZeroes​(java.math.BigDecimal value,
                                                                   java.math.MathContext mathContext)
        Rounds the specified BigDecimal to the precision of the specified MathContext including trailing zeroes.

        This method is similar to BigDecimal.round(MathContext) but does not remove the trailing zeroes.

        Example:

        MathContext mc = new MathContext(5);
        System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.234567"), mc));    // 1.2346
        System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("123.4567"), mc));    // 123.46
        System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0.001234567"), mc)); // 0.0012346
        System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.23"), mc));        // 1.2300
        System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.230000"), mc));    // 1.2300
        System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0.00123"), mc));     // 0.0012300
        System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0"), mc));           // 0.0000
        System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0.00000000"), mc));  // 0.0000
        
        Parameters:
        value - the BigDecimal to round
        mathContext - the MathContext used for the result
        Returns:
        the rounded BigDecimal value including trailing zeroes
        See Also:
        BigDecimal.round(MathContext), round(BigDecimal, MathContext)
      • reciprocal

        public static java.math.BigDecimal reciprocal​(java.math.BigDecimal x,
                                                      java.math.MathContext mathContext)
        Calculates the reciprocal of the specified BigDecimal.
        Parameters:
        x - the BigDecimal
        mathContext - the MathContext used for the result
        Returns:
        the reciprocal BigDecimal
        Throws:
        java.lang.ArithmeticException - if x = 0
        java.lang.ArithmeticException - if the result is inexact but the rounding mode is UNNECESSARY or mc.precision == 0 and the quotient has a non-terminating decimal expansion.
      • factorial

        public static java.math.BigDecimal factorial​(int n)
        Calculates the factorial of the specified integer argument.

        factorial = 1 * 2 * 3 * ... n

        Parameters:
        n - the BigDecimal
        Returns:
        the factorial BigDecimal
        Throws:
        java.lang.ArithmeticException - if x < 0
      • factorialLoop

        private static java.math.BigDecimal factorialLoop​(int n1,
                                                          int n2)
      • factorialRecursion

        private static java.math.BigDecimal factorialRecursion​(int n1,
                                                               int n2)
      • factorial

        public static java.math.BigDecimal factorial​(java.math.BigDecimal x,
                                                     java.math.MathContext mathContext)
        Calculates the factorial of the specified BigDecimal.

        This implementation uses Spouge's approximation to calculate the factorial for non-integer values.

        This involves calculating a series of constants that depend on the desired precision. Since this constant calculation is quite expensive (especially for higher precisions), the constants for a specific precision will be cached and subsequent calls to this method with the same precision will be much faster.

        It is therefore recommended to do one call to this method with the standard precision of your application during the startup phase and to avoid calling it with many different precisions.

        See: Wikipedia: Factorial - Extension of factorial to non-integer values of argument

        Parameters:
        x - the BigDecimal
        mathContext - the MathContext used for the result
        Returns:
        the factorial BigDecimal
        Throws:
        java.lang.ArithmeticException - if x is a negative integer value (-1, -2, -3, ...)
        java.lang.UnsupportedOperationException - if x is a non-integer value and the MathContext has unlimited precision
        See Also:
        factorial(int), gamma(BigDecimal, MathContext)
      • getSpougeFactorialConstants

        static java.util.List<java.math.BigDecimal> getSpougeFactorialConstants​(int a)
      • gamma

        public static java.math.BigDecimal gamma​(java.math.BigDecimal x,
                                                 java.math.MathContext mathContext)
        Calculates the gamma function of the specified BigDecimal.

        This implementation uses factorial(BigDecimal, MathContext) internally, therefore the performance implications described there apply also for this method.

        See: Wikipedia: Gamma function

        Parameters:
        x - the BigDecimal
        mathContext - the MathContext used for the result
        Returns:
        the gamma BigDecimal
        Throws:
        java.lang.ArithmeticException - if x-1 is a negative integer value (-1, -2, -3, ...)
        java.lang.UnsupportedOperationException - if x is a non-integer value and the MathContext has unlimited precision
        See Also:
        factorial(BigDecimal, MathContext)
      • bernoulli

        public static java.math.BigDecimal bernoulli​(int n,
                                                     java.math.MathContext mathContext)
        Calculates the Bernoulli number for the specified index.

        This function calculates the first Bernoulli numbers and therefore bernoulli(1) returns -0.5

        Note that bernoulli(x) for all odd x > 1 returns 0

        See: Wikipedia: Bernoulli number

        Parameters:
        n - the index of the Bernoulli number to be calculated (starting at 0)
        mathContext - the MathContext used for the result
        Returns:
        the Bernoulli number for the specified index
        Throws:
        java.lang.ArithmeticException - if x < 0
        java.lang.ArithmeticException - if the result is inexact but the rounding mode is UNNECESSARY or mc.precision == 0 and the quotient has a non-terminating decimal expansion.
      • pow

        public static java.math.BigDecimal pow​(java.math.BigDecimal x,
                                               java.math.BigDecimal y,
                                               java.math.MathContext mathContext)
        Calculates BigDecimal x to the power of BigDecimal y (xy).
        Parameters:
        x - the BigDecimal value to take to the power
        y - the BigDecimal value to serve as exponent
        mathContext - the MathContext used for the result
        Returns:
        the calculated x to the power of y with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
        See Also:
        pow(BigDecimal, long, MathContext)
      • pow

        public static java.math.BigDecimal pow​(java.math.BigDecimal x,
                                               long y,
                                               java.math.MathContext mathContext)
        Calculates BigDecimal x to the power of long y (xy).

        The implementation tries to minimize the number of multiplications of x (using squares whenever possible).

        See: Wikipedia: Exponentiation - efficient computation

        Parameters:
        x - the BigDecimal value to take to the power
        y - the long value to serve as exponent
        mathContext - the MathContext used for the result
        Returns:
        the calculated x to the power of y with the precision specified in the mathContext
        Throws:
        java.lang.ArithmeticException - if y is negative and the result is inexact but the rounding mode is UNNECESSARY or mc.precision == 0 and the quotient has a non-terminating decimal expansion.
        java.lang.ArithmeticException - if the rounding mode is UNNECESSARY and the BigDecimal operation would require rounding.
      • powInteger

        private static java.math.BigDecimal powInteger​(java.math.BigDecimal x,
                                                       java.math.BigDecimal integerY,
                                                       java.math.MathContext mathContext)
        Calculates BigDecimal x to the power of the integer value y (xy).

        The value y MUST be an integer value.

        Parameters:
        x - the BigDecimal value to take to the power
        integerY - the BigDecimal integer value to serve as exponent
        mathContext - the MathContext used for the result
        Returns:
        the calculated x to the power of y with the precision specified in the mathContext
        See Also:
        pow(BigDecimal, long, MathContext)
      • sqrt

        public static java.math.BigDecimal sqrt​(java.math.BigDecimal x,
                                                java.math.MathContext mathContext)
        Calculates the square root of BigDecimal x.

        See Wikipedia: Square root

        Parameters:
        x - the BigDecimal value to calculate the square root
        mathContext - the MathContext used for the result
        Returns:
        the calculated square root of x with the precision specified in the mathContext
        Throws:
        java.lang.ArithmeticException - if x < 0
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • root

        public static java.math.BigDecimal root​(java.math.BigDecimal x,
                                                java.math.BigDecimal n,
                                                java.math.MathContext mathContext)
        Calculates the n'th root of BigDecimal x.

        See Wikipedia: Nth root

        Parameters:
        x - the BigDecimal value to calculate the n'th root
        n - the BigDecimal defining the root
        mathContext - the MathContext used for the result
        Returns:
        the calculated n'th root of x with the precision specified in the mathContext
        Throws:
        java.lang.ArithmeticException - if n <= 0
        java.lang.ArithmeticException - if x < 0
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • rootUsingNewtonRaphson

        private static java.math.BigDecimal rootUsingNewtonRaphson​(java.math.BigDecimal x,
                                                                   java.math.BigDecimal n,
                                                                   java.math.BigDecimal initialResult,
                                                                   java.math.MathContext mathContext)
      • log

        public static java.math.BigDecimal log​(java.math.BigDecimal x,
                                               java.math.MathContext mathContext)
        Calculates the natural logarithm of BigDecimal x.

        See: Wikipedia: Natural logarithm

        Parameters:
        x - the BigDecimal to calculate the natural logarithm for
        mathContext - the MathContext used for the result
        Returns:
        the calculated natural logarithm BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.ArithmeticException - if x <= 0
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • log2

        public static java.math.BigDecimal log2​(java.math.BigDecimal x,
                                                java.math.MathContext mathContext)
        Calculates the logarithm of BigDecimal x to the base 2.
        Parameters:
        x - the BigDecimal to calculate the logarithm base 2 for
        mathContext - the MathContext used for the result
        Returns:
        the calculated natural logarithm BigDecimal to the base 2 with the precision specified in the mathContext
        Throws:
        java.lang.ArithmeticException - if x <= 0
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • log10

        public static java.math.BigDecimal log10​(java.math.BigDecimal x,
                                                 java.math.MathContext mathContext)
        Calculates the logarithm of BigDecimal x to the base 10.
        Parameters:
        x - the BigDecimal to calculate the logarithm base 10 for
        mathContext - the MathContext used for the result
        Returns:
        the calculated natural logarithm BigDecimal to the base 10 with the precision specified in the mathContext
        Throws:
        java.lang.ArithmeticException - if x <= 0
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • logUsingNewton

        private static java.math.BigDecimal logUsingNewton​(java.math.BigDecimal x,
                                                           java.math.MathContext mathContext)
      • logUsingExponent

        private static java.math.BigDecimal logUsingExponent​(java.math.BigDecimal x,
                                                             java.math.MathContext mathContext)
      • logUsingTwoThree

        private static java.math.BigDecimal logUsingTwoThree​(java.math.BigDecimal x,
                                                             java.math.MathContext mathContext)
      • pi

        public static java.math.BigDecimal pi​(java.math.MathContext mathContext)
        Returns the number pi.

        See Wikipedia: Pi

        Parameters:
        mathContext - the MathContext used for the result
        Returns:
        the number pi with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • piChudnovski

        private static java.math.BigDecimal piChudnovski​(java.math.MathContext mathContext)
      • e

        public static java.math.BigDecimal e​(java.math.MathContext mathContext)
        Returns the number e.

        See Wikipedia: E (mathematical_constant)

        Parameters:
        mathContext - the MathContext used for the result
        Returns:
        the number e with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • logTen

        private static java.math.BigDecimal logTen​(java.math.MathContext mathContext)
      • logTwo

        private static java.math.BigDecimal logTwo​(java.math.MathContext mathContext)
      • logThree

        private static java.math.BigDecimal logThree​(java.math.MathContext mathContext)
      • exp

        public static java.math.BigDecimal exp​(java.math.BigDecimal x,
                                               java.math.MathContext mathContext)
        Calculates the natural exponent of BigDecimal x (ex).

        See: Wikipedia: Exponent

        Parameters:
        x - the BigDecimal to calculate the exponent for
        mathContext - the MathContext used for the result
        Returns:
        the calculated exponent BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • expIntegralFractional

        private static java.math.BigDecimal expIntegralFractional​(java.math.BigDecimal x,
                                                                  java.math.MathContext mathContext)
      • expTaylor

        private static java.math.BigDecimal expTaylor​(java.math.BigDecimal x,
                                                      java.math.MathContext mathContext)
      • sin

        public static java.math.BigDecimal sin​(java.math.BigDecimal x,
                                               java.math.MathContext mathContext)
        Calculates the sine (sinus) of BigDecimal x.

        See: Wikipedia: Sine

        Parameters:
        x - the BigDecimal to calculate the sine for
        mathContext - the MathContext used for the result
        Returns:
        the calculated sine BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • asin

        public static java.math.BigDecimal asin​(java.math.BigDecimal x,
                                                java.math.MathContext mathContext)
        Calculates the arc sine (inverted sine) of BigDecimal x.

        See: Wikipedia: Arcsine

        Parameters:
        x - the BigDecimal to calculate the arc sine for
        mathContext - the MathContext used for the result
        Returns:
        the calculated arc sine BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.ArithmeticException - if x > 1 or x < -1
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • cos

        public static java.math.BigDecimal cos​(java.math.BigDecimal x,
                                               java.math.MathContext mathContext)
        Calculates the cosine (cosinus) of BigDecimal x.

        See: Wikipedia: Cosine

        Parameters:
        x - the BigDecimal to calculate the cosine for
        mathContext - the MathContext used for the result
        Returns:
        the calculated cosine BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • acos

        public static java.math.BigDecimal acos​(java.math.BigDecimal x,
                                                java.math.MathContext mathContext)
        Calculates the arc cosine (inverted cosine) of BigDecimal x.

        See: Wikipedia: Arccosine

        Parameters:
        x - the BigDecimal to calculate the arc cosine for
        mathContext - the MathContext used for the result
        Returns:
        the calculated arc sine BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.ArithmeticException - if x > 1 or x < -1
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • tan

        public static java.math.BigDecimal tan​(java.math.BigDecimal x,
                                               java.math.MathContext mathContext)
        Calculates the tangens of BigDecimal x.

        See: Wikipedia: Tangens

        Parameters:
        x - the BigDecimal to calculate the tangens for
        mathContext - the MathContext used for the result
        Returns:
        the calculated tangens BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • atan

        public static java.math.BigDecimal atan​(java.math.BigDecimal x,
                                                java.math.MathContext mathContext)
        Calculates the arc tangens (inverted tangens) of BigDecimal x.

        See: Wikipedia: Arctangens

        Parameters:
        x - the BigDecimal to calculate the arc tangens for
        mathContext - the MathContext used for the result
        Returns:
        the calculated arc tangens BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • atan2

        public static java.math.BigDecimal atan2​(java.math.BigDecimal y,
                                                 java.math.BigDecimal x,
                                                 java.math.MathContext mathContext)
        Calculates the arc tangens (inverted tangens) of BigDecimal y / x in the range -pi to pi.

        This is useful to calculate the angle theta from the conversion of rectangular coordinates (xy) to polar coordinates (r, theta).

        See: Wikipedia: Atan2

        Parameters:
        y - the BigDecimal
        x - the BigDecimal
        mathContext - the MathContext used for the result
        Returns:
        the calculated arc tangens BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.ArithmeticException - if x = 0 and y = 0
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • cot

        public static java.math.BigDecimal cot​(java.math.BigDecimal x,
                                               java.math.MathContext mathContext)
        Calculates the cotangens of BigDecimal x.

        See: Wikipedia: Cotangens

        Parameters:
        x - the BigDecimal to calculate the cotangens for
        mathContext - the MathContext used for the result
        Returns:
        the calculated cotanges BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.ArithmeticException - if x = 0
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • acot

        public static java.math.BigDecimal acot​(java.math.BigDecimal x,
                                                java.math.MathContext mathContext)
        Calculates the inverse cotangens (arc cotangens) of BigDecimal x.

        See: Wikipedia: Arccotangens

        Parameters:
        x - the BigDecimal to calculate the arc cotangens for
        mathContext - the MathContext used for the result
        Returns:
        the calculated arc cotangens BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • sinh

        public static java.math.BigDecimal sinh​(java.math.BigDecimal x,
                                                java.math.MathContext mathContext)
        Calculates the hyperbolic sine of BigDecimal x.

        See: Wikipedia: Hyperbolic function

        Parameters:
        x - the BigDecimal to calculate the hyperbolic sine for
        mathContext - the MathContext used for the result
        Returns:
        the calculated hyperbolic sine BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • cosh

        public static java.math.BigDecimal cosh​(java.math.BigDecimal x,
                                                java.math.MathContext mathContext)
        Calculates the hyperbolic cosine of BigDecimal x.

        See: Wikipedia: Hyperbolic function

        Parameters:
        x - the BigDecimal to calculate the hyperbolic cosine for
        mathContext - the MathContext used for the result
        Returns:
        the calculated hyperbolic cosine BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • tanh

        public static java.math.BigDecimal tanh​(java.math.BigDecimal x,
                                                java.math.MathContext mathContext)
        Calculates the hyperbolic tangens of BigDecimal x.

        See: Wikipedia: Hyperbolic function

        Parameters:
        x - the BigDecimal to calculate the hyperbolic tangens for
        mathContext - the MathContext used for the result
        Returns:
        the calculated hyperbolic tangens BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • coth

        public static java.math.BigDecimal coth​(java.math.BigDecimal x,
                                                java.math.MathContext mathContext)
        Calculates the hyperbolic cotangens of BigDecimal x.

        See: Wikipedia: Hyperbolic function

        Parameters:
        x - the BigDecimal to calculate the hyperbolic cotangens for
        mathContext - the MathContext used for the result
        Returns:
        the calculated hyperbolic cotangens BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • asinh

        public static java.math.BigDecimal asinh​(java.math.BigDecimal x,
                                                 java.math.MathContext mathContext)
        Calculates the arc hyperbolic sine (inverse hyperbolic sine) of BigDecimal x.

        See: Wikipedia: Hyperbolic function

        Parameters:
        x - the BigDecimal to calculate the arc hyperbolic sine for
        mathContext - the MathContext used for the result
        Returns:
        the calculated arc hyperbolic sine BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • acosh

        public static java.math.BigDecimal acosh​(java.math.BigDecimal x,
                                                 java.math.MathContext mathContext)
        Calculates the arc hyperbolic cosine (inverse hyperbolic cosine) of BigDecimal x.

        See: Wikipedia: Hyperbolic function

        Parameters:
        x - the BigDecimal to calculate the arc hyperbolic cosine for
        mathContext - the MathContext used for the result
        Returns:
        the calculated arc hyperbolic cosine BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • atanh

        public static java.math.BigDecimal atanh​(java.math.BigDecimal x,
                                                 java.math.MathContext mathContext)
        Calculates the arc hyperbolic tangens (inverse hyperbolic tangens) of BigDecimal x.

        See: Wikipedia: Hyperbolic function

        Parameters:
        x - the BigDecimal to calculate the arc hyperbolic tangens for
        mathContext - the MathContext used for the result
        Returns:
        the calculated arc hyperbolic tangens BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • acoth

        public static java.math.BigDecimal acoth​(java.math.BigDecimal x,
                                                 java.math.MathContext mathContext)
        Calculates the arc hyperbolic cotangens (inverse hyperbolic cotangens) of BigDecimal x.

        See: Wikipedia: Hyperbolic function

        Parameters:
        x - the BigDecimal to calculate the arc hyperbolic cotangens for
        mathContext - the MathContext used for the result
        Returns:
        the calculated arc hyperbolic cotangens BigDecimal with the precision specified in the mathContext
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • toDegrees

        public static java.math.BigDecimal toDegrees​(java.math.BigDecimal x,
                                                     java.math.MathContext mathContext)
        Converts an angle measured in radians to an approximately equivalent angle measured in degrees. The conversion from radians to degrees is generally inexact, it uses the number PI with the precision specified in the mathContext.
        Parameters:
        x - An angle in radians.
        mathContext - the MathContext used for the result
        Returns:
        The angle in degrees.
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • toRadians

        public static java.math.BigDecimal toRadians​(java.math.BigDecimal x,
                                                     java.math.MathContext mathContext)
        /** Converts an angle measured in degrees to an approximately equivalent angle measured in radians. The conversion from degrees to radians is generally inexact, it uses the number PI with the precision specified in the mathContext.
        Parameters:
        x - An angle in degrees.
        mathContext - the MathContext used for the result
        Returns:
        The angle in radians.
        Throws:
        java.lang.UnsupportedOperationException - if the MathContext has unlimited precision
      • checkMathContext

        private static void checkMathContext​(java.math.MathContext mathContext)