Class BigDecimalMath


  • public class BigDecimalMath
    extends Object
    Basic BigDecimal arithmetics.
    Author:
    Tilman Neumann
    • Method Detail

      • add

        public static BigDecimal add​(BigDecimal a,
                                     BigRational b,
                                     Scale resultScale)
        Computes the sum of a and b accurate to the given resultScale.
        Scale is the natural accuracy measure for additions because for each argument, each piece of it (bit, digit, ...) makes its own independent contribution to the result scale.
        Parameters:
        a -
        b -
        resultScale -
        Returns:
        sum
      • multiply

        public static BigDecimal multiply​(BigDecimal a,
                                          BigRational b,
                                          Precision resultPrecision)
        Computes the product of a and b.
        Precision is the natural accuracy measure for multiplications because for each argument, each piece of it (bit, digit, ...) makes its own independent contribution to the result precision.
        Parameters:
        a -
        b -
        resultPrecision -
        Returns:
        product
      • multiply

        public static BigDecimal multiply​(BigDecimal a,
                                          BigInteger b)
        Multiplication without precision loss.
        Parameters:
        a - big float
        b - big integer
        Returns:
        a*b
      • multiply

        public static BigDecimal multiply​(BigDecimal a,
                                          long b)
        Computes the product a*b without precision loss.
        Parameters:
        a -
        b -
        Returns:
        a*b
      • divide

        public static BigDecimal divide​(BigDecimal a,
                                        BigDecimal b,
                                        Scale resultScale)
        Division with guaranteed precision.
        Parameters:
        a - dividend
        b - divisor
        resultScale - result accuracy in decimal digits after the floating point
        Returns:
        a / b
      • divide

        public static BigDecimal divide​(BigDecimal a,
                                        BigInteger b,
                                        Scale resultScale)
        Division by an integer.
        Parameters:
        a - dividend
        b - divisor
        resultScale - result accuracy in decimal digits after the floating point
        Returns:
        quotient with wanted precision.
      • frac

        public static BigDecimal frac​(BigDecimal x)
        Returns the fractional part of x, with the same scale than x.
        Parameters:
        x -
        Returns:
        frac(x)
      • roundInt

        public static BigInteger roundInt​(BigDecimal x)
        Round x to the nearest integer.
        Parameters:
        x -
        Returns:
        round(x)
      • ceilInt

        public static BigInteger ceilInt​(BigDecimal x)
        Returns ceil(x) as a big integer.
        Parameters:
        x -
        Returns:
        ceil(x)
      • floorInt

        public static BigInteger floorInt​(BigDecimal x)
        Returns floor(x) as a big integer.
        Parameters:
        x -
        Returns:
        floor(x)
      • compare

        public static int compare​(BigDecimal a,
                                  BigInteger b)
        Parameters:
        a -
        b -
        Returns:
        <0/0/>0 if a is smaller/equal/greater than b.