Class BigDecimalMath

java.lang.Object
de.tilman_neumann.jml.base.BigDecimalMath

public class BigDecimalMath
extends java.lang.Object
Basic BigDecimal arithmetics.
  • Method Summary

    Modifier and Type Method Description
    static java.math.BigDecimal add​(java.math.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.
    static java.math.BigDecimal add​(java.math.BigDecimal a, java.math.BigInteger b)  
    static java.math.BigInteger ceilInt​(java.math.BigDecimal x)
    Returns ceil(x) as a big integer.
    static int compare​(java.math.BigDecimal a, java.math.BigInteger b)  
    static java.math.BigDecimal divide​(java.math.BigDecimal a, java.math.BigDecimal b, Precision resultPrecision)  
    static java.math.BigDecimal divide​(java.math.BigDecimal a, java.math.BigDecimal b, Scale resultScale)
    Division with guaranteed precision.
    static java.math.BigDecimal divide​(java.math.BigDecimal a, java.math.BigInteger b, Precision resultPrecision)  
    static java.math.BigDecimal divide​(java.math.BigDecimal a, java.math.BigInteger b, Scale resultScale)
    Division by an integer.
    static java.math.BigInteger floorInt​(java.math.BigDecimal x)
    Returns floor(x) as a big integer.
    static java.math.BigDecimal frac​(java.math.BigDecimal x)
    Returns the fractional part of x, with the same scale than x.
    static java.math.BigDecimal multiply​(java.math.BigDecimal a, long b)
    Computes the product a*b without precision loss.
    static java.math.BigDecimal multiply​(java.math.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.
    static java.math.BigDecimal multiply​(java.math.BigDecimal a, BigRational b, Scale resultScale)  
    static java.math.BigDecimal multiply​(java.math.BigDecimal a, java.math.BigInteger b)
    Multiplication without precision loss.
    static java.math.BigInteger roundInt​(java.math.BigDecimal x)
    Round x to the nearest integer.
    static java.math.BigDecimal subtract​(java.math.BigDecimal a, java.math.BigInteger b)
    Computes the difference of a and b.

    Methods inherited from class java.lang.Object

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

    • add

      public static java.math.BigDecimal add​(java.math.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
    • add

      public static java.math.BigDecimal add​(java.math.BigDecimal a, java.math.BigInteger b)
    • subtract

      public static java.math.BigDecimal subtract​(java.math.BigDecimal a, java.math.BigInteger b)
      Computes the difference of a and b.
      Parameters:
      a -
      b -
      Returns:
      a-b
    • multiply

      public static java.math.BigDecimal multiply​(java.math.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 java.math.BigDecimal multiply​(java.math.BigDecimal a, BigRational b, Scale resultScale)
    • multiply

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

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

      public static java.math.BigDecimal divide​(java.math.BigDecimal a, java.math.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 java.math.BigDecimal divide​(java.math.BigDecimal a, java.math.BigDecimal b, Precision resultPrecision)
    • divide

      public static java.math.BigDecimal divide​(java.math.BigDecimal a, java.math.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.
    • divide

      public static java.math.BigDecimal divide​(java.math.BigDecimal a, java.math.BigInteger b, Precision resultPrecision)
    • frac

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

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

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

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

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