Package de.tilman_neumann.jml.base
Class BigDecimalMath
- java.lang.Object
-
- de.tilman_neumann.jml.base.BigDecimalMath
-
public class BigDecimalMath extends Object
Basic BigDecimal arithmetics.- Author:
- Tilman Neumann
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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.static BigDecimal
add(BigDecimal a, BigInteger b)
static BigInteger
ceilInt(BigDecimal x)
Returns ceil(x) as a big integer.static int
compare(BigDecimal a, BigInteger b)
static BigDecimal
divide(BigDecimal a, BigDecimal b, Precision resultPrecision)
static BigDecimal
divide(BigDecimal a, BigDecimal b, Scale resultScale)
Division with guaranteed precision.static BigDecimal
divide(BigDecimal a, BigInteger b, Precision resultPrecision)
static BigDecimal
divide(BigDecimal a, BigInteger b, Scale resultScale)
Division by an integer.static BigInteger
floorInt(BigDecimal x)
Returns floor(x) as a big integer.static BigDecimal
frac(BigDecimal x)
Returns the fractional part of x, with the same scale than x.static BigDecimal
multiply(BigDecimal a, long b)
Computes the product a*b without precision loss.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.static BigDecimal
multiply(BigDecimal a, BigRational b, Scale resultScale)
static BigDecimal
multiply(BigDecimal a, BigInteger b)
Multiplication without precision loss.static BigInteger
roundInt(BigDecimal x)
Round x to the nearest integer.static BigDecimal
subtract(BigDecimal a, BigInteger b)
Computes the difference of a and b.
-
-
-
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
-
add
public static BigDecimal add(BigDecimal a, BigInteger b)
-
subtract
public static BigDecimal subtract(BigDecimal a, BigInteger b)
Computes the difference of a and b.- Parameters:
a
-b
-- Returns:
- a-b
-
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, BigRational b, Scale resultScale)
-
multiply
public static BigDecimal multiply(BigDecimal a, BigInteger b)
Multiplication without precision loss.- Parameters:
a
- big floatb
- 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
- dividendb
- divisorresultScale
- result accuracy in decimal digits after the floating point- Returns:
- a / b
-
divide
public static BigDecimal divide(BigDecimal a, BigDecimal b, Precision resultPrecision)
-
divide
public static BigDecimal divide(BigDecimal a, BigInteger b, Scale resultScale)
Division by an integer.- Parameters:
a
- dividendb
- divisorresultScale
- result accuracy in decimal digits after the floating point- Returns:
- quotient with wanted precision.
-
divide
public static BigDecimal divide(BigDecimal a, BigInteger b, Precision resultPrecision)
-
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.
-
-