Class BigUtil
public final class BigUtil extends Object
BigDecimal
and BigInteger
.
Also provides some functionality that is not available at all with these classes; for example, raising a
BigDecimal
to the power of another BigDecimal
.
Acquiring instances of BigDecimal
and BigInteger
is particularly easy with BigUtil
.
Simply call bd(arg)
with any reasonable argument type to get a BigDecimal
, and similarly
bi(arg)
to get a BigInteger
. No need to think about whether to use a constructor or
valueOf()
, no risk of accidentally building a BigDecimal
from a double the wrong way, and
because then names bd
and bi
are fairly specific, unlike valueOf
, it is practial
to statically import them and get instances extremely succintly. Additionally, you can use the static fields
BD0
, BD1
, BDM1
, BI0
, BI1
, BIM1
, BDPI
, and BDE
to acquire instances representing 0, 1, negative 1, pi, and e, and these can also be conveniently statically
imported.
Comparison methods are provided for comparing two BigDecimals
, two BigIntegers
, or one of each.
equal
, notEqual
, greater
, less
, greaterOrEqual
, and lessOrEqual
are all provided, so you can avoid the clutter of the (compareTo() <op> 0)
idiom. Additionally,
equal(BigDecimal, BigDecimal)
works as you would hope it would work -- two numbers that differ only in
trailing zeros after the decimal point are equal to one another. Convenience methods are provided for other
comparisons as well, namely isPos
, isNeg
, isZero
, and isOne
.
isInt(BigDecimal)
can be used to determine whether the value has a nonzero decimal component.
wholePart(BigDecimal)
and decimalPart(BigDecimal)
return the value's desired component, and
wholeAndDecimalParts(BigDecimal)
is similar but represents the decimal part as an integer.
fraction(BigDecimal)
and mixedNumber(BigDecimal)
return a fractional view of the value.
digits(BigInteger)
returns an array of the value's digits, and digits(BigDecimal)
returns two
arrays, one for the whole part and one for the decimal part. digitCount(BigInteger)
returns the length
of the value.
round(BigDecimal)
methods are provided, one for rounding to a BigInteger
value and another for
rounding to a different BigDecimal
value. Unlike BigDecimal.round(MathContext)
, BigUtil
's
round
method rounds to the given number of decimal places as you would expect, rather than reducing the
total number of significant figures to the given amount. It also removes any trailing decimal zeros and does not
cause the string representation to change to exponential notation.
add
, subtract
, multiply
, and divide
methods are provided, taking BigDecimal
,
BigInteger
, or a combination of the two as arguments. add
and multiply
can take a variable
number of arguments so that a chain of additions and multiplications can be performed with one call. divide
does not require the code clutter of specifying a number of digits to round to; if rounding is required, a very
high precision result will be returned. If rounding is not required, the result will not include trailing decimal
zeros.
pow
can raise any BigInteger
or BigDecimal
to the power of any other BigInteger
,
BigDecimal
, or long
. sqrt
and nthRoot
are also provided, as are exp
,
ln
, and log
, all of which can take BigDecimal
or BigInteger
arguments.
Any calculation whose result requires rounding; i.e., an irrational number or a repeating decimal, will be returned with an extremely high precision (at least 100 significant figures).
-
Field Summary
Fields Modifier and Type Field Description static BigDecimal
BD0
TheBigDecimal
0, with no decimal digitsstatic BigDecimal
BD1
TheBigDecimal
1, with no decimal digitsstatic BigDecimal
BDE
ABigDecimal
representation ofe
approximated to 110 decimal places.static BigDecimal
BDM1
TheBigDecimal
-1, with no decimal digitsstatic BigDecimal
BDPI
ABigDecimal
representation ofpi
approximated to 110 decimal places.static BigInteger
BI0
TheBigInteger
0static BigInteger
BI1
TheBigInteger
1static BigInteger
BIM1
TheBigInteger
-1 -
Method Summary
Modifier and Type Method Description static BigDecimal
add(BigDecimal a, BigDecimal b, BigDecimal... others)
Returns the sum of the given values.static BigDecimal
add(BigDecimal a, BigInteger b, BigInteger... others)
Returns the sum of the given values.static BigDecimal
add(BigInteger a, BigDecimal b, BigDecimal... others)
Returns the sum of the given values.static BigInteger
add(BigInteger a, BigInteger b, BigInteger... others)
Returns the sum of the given values.static BigDecimal
bd(double val)
Returns aBigDecimal
representation ofval
.static BigDecimal
bd(long val)
Returns aBigDecimal
representation ofval
.static BigDecimal
bd(String val)
Returns aBigDecimal
representation ofval
.static BigDecimal
bd(BigInteger val)
Returns aBigDecimal
representation ofval
.static BigInteger
bi(long val)
Returns aBigInteger
representation ofval
.static BigInteger
bi(String val)
Returns aBigInteger
representation ofval
.static BigDecimal
decimalPart(BigDecimal val)
Returns the decimal part ofval
(as a decimal, not scaled to an integer value).static int
digitCount(BigInteger val)
Returns the number of digits ofval
.static int[][]
digits(BigDecimal val)
Returns 2 arrays, the first of which contains the digits ofval
before the decimal point, and the second of which contains the digits after the decimal point.static int[]
digits(BigInteger val)
Returns the digits ofval
.static BigDecimal
divide(BigDecimal a, BigDecimal b)
Returnsa
divided byb
.static BigDecimal
divide(BigDecimal a, BigInteger b)
Returnsa
divided byb
.static BigDecimal
divide(BigInteger a, BigDecimal b)
Returnsa
divided byb
.static BigDecimal
divide(BigInteger a, BigInteger b)
Returnsa
divided byb
.static boolean
equal(BigDecimal a, BigDecimal b)
Returns true ifa
andb
are the same value, ignoring scale.static boolean
equal(BigDecimal a, BigInteger b)
Returns true ifa
andb
are the same value, ignoring the scale ofa
.static boolean
equal(BigInteger a, BigDecimal b)
Returns true ifa
andb
are the same value, ignoring the scale ofb
.static boolean
equal(BigInteger a, BigInteger b)
Returns true ifa
andb
are equal.static BigDecimal
exp(long power)
Returnse^power
.static BigDecimal
exp(BigDecimal power)
Returnse^power
.static BigDecimal
exp(BigInteger power)
Returnse^power
.static BigInteger[]
fraction(BigDecimal val)
Returns a fraction representation ofval
.static boolean
greater(BigDecimal a, BigDecimal b)
Returns true ifa
is greater thanb
.static boolean
greater(BigDecimal a, BigInteger b)
Returns true ifa
is greater thanb
.static boolean
greater(BigInteger a, BigDecimal b)
Returns true ifa
is greater thanb
.static boolean
greater(BigInteger a, BigInteger b)
Returns true ifa
is greater thanb
.static boolean
greaterOrEqual(BigDecimal a, BigDecimal b)
Returns true ifa
is greater than or equal tob
, ignoring scale.static boolean
greaterOrEqual(BigDecimal a, BigInteger b)
Returns true ifa
is greater than or equal tob
, ignoring the scale ofa
.static boolean
greaterOrEqual(BigInteger a, BigDecimal b)
Returns true ifa
is greater than or equal tob
, ignoring the scale ofb
.static boolean
greaterOrEqual(BigInteger a, BigInteger b)
Returns true ifa
is greater than or equal tob
.static BigDecimal
invert(BigDecimal val)
Returns1/val
.static BigDecimal
invert(BigInteger val)
Returns1/val
.static boolean
isInt(BigDecimal val)
Returns true ifval
is an integer, false otherwise.static boolean
isNeg(BigDecimal val)
Returns true ifval
is less than 0, false otherwise.static boolean
isNeg(BigInteger val)
Returns true ifval
is less than 0, false otherwise.static boolean
isOne(BigDecimal val)
Returns true ifval
is equal in magnitude to 1 (ignoring scale), false otherwise.static boolean
isOne(BigInteger val)
Returns true ifval
is equal to 1, false otherwise.static boolean
isPos(BigDecimal val)
Returns true ifval
is greater than 0, false otherwise.static boolean
isPos(BigInteger val)
Returns true ifval
is greater than 0, false otherwise.static boolean
isZero(BigDecimal val)
Returns true ifval
is equal in magnitude to 0 (ignoring scale), false otherwise.static boolean
isZero(BigInteger val)
Returns true ifval
is equal to 0, false otherwise.static boolean
less(BigDecimal a, BigDecimal b)
Returns true ifa
is less thanb
.static boolean
less(BigDecimal a, BigInteger b)
Returns true ifa
is less thanb
.static boolean
less(BigInteger a, BigDecimal b)
Returns true ifa
is less thanb
.static boolean
less(BigInteger a, BigInteger b)
Returns true ifa
is less thanb
.static boolean
lessOrEqual(BigDecimal a, BigDecimal b)
Returns true ifa
is less than or equal tob
, ignoring scale.static boolean
lessOrEqual(BigDecimal a, BigInteger b)
Returns true ifa
is less than or equal tob
, ignoring the scale ofa
.static boolean
lessOrEqual(BigInteger a, BigDecimal b)
Returns true ifa
is less than or equal tob
, ignoring the scale ofb
.static boolean
lessOrEqual(BigInteger a, BigInteger b)
Returns true ifa
is less than or equal tob
.static BigDecimal
ln(long val)
Returns the natural logarithm ofval
.static BigDecimal
ln(BigDecimal val)
Returns the natural logarithm ofval
.static BigDecimal
ln(BigInteger val)
Returns the natural logarithm ofval
.static BigDecimal
log(long base, long val)
Returns the log of the givenbase
ofval
.static BigDecimal
log(long base, BigDecimal val)
Returns the log of the givenbase
ofval
.static BigDecimal
log(long base, BigInteger val)
Returns the log of the givenbase
ofval
.static BigDecimal
log(BigDecimal base, long val)
Returns the log of the givenbase
ofval
.static BigDecimal
log(BigDecimal base, BigDecimal val)
Returns the log of the givenbase
ofval
.static BigDecimal
log(BigDecimal base, BigInteger val)
Returns the log of the givenbase
ofval
.static BigDecimal
log(BigInteger base, long val)
Returns the log of the givenbase
ofval
.static BigDecimal
log(BigInteger base, BigDecimal val)
Returns the log of the givenbase
ofval
.static BigDecimal
log(BigInteger base, BigInteger val)
Returns the log of the givenbase
ofval
.static BigInteger[]
mixedNumber(BigDecimal val)
Returns a mixed number representation ofval
, i.e.,a+b/c
, wherea
is the whole part ofval
,b
is the decimal part scaled as an integer, andc
is 10 raised to the power of the number of decimal digits.static BigDecimal
multiply(BigDecimal a, BigDecimal b, BigDecimal... others)
Returns the product of the given values.static BigDecimal
multiply(BigDecimal a, BigInteger b, BigInteger... others)
Returns the product of the given values.static BigDecimal
multiply(BigInteger a, BigDecimal b, BigDecimal... others)
Returns the product of the given values.static BigInteger
multiply(BigInteger a, BigInteger b, BigInteger... others)
Returns the product of the given values.static boolean
notEqual(BigDecimal a, BigDecimal b)
Returns true ifa
andb
are not the same value, ignoring scale.static boolean
notEqual(BigDecimal a, BigInteger b)
Returns true ifa
andb
are not the same value, ignoring the scale ofa
.static boolean
notEqual(BigInteger a, BigDecimal b)
Returns true ifa
andb
are not the same value, ignoring the scale ofb
.static boolean
notEqual(BigInteger a, BigInteger b)
Returns true ifa
andb
are not equal.static BigDecimal
nthRoot(BigDecimal base, long n)
Equivalent topow(BigDecimal, BigDecimal)
withinvert(n)
as the exponent, except thatn
must be greater than zero, and in the case thatbase
is negative,n
is odd, andinvert(n)
can't be expressed exactly as a decimal, this method will succeed whereaspow(base, invert(n))
will likely fail due to representing the denominator of the exponent as an even number rather than odd.static BigDecimal
nthRoot(BigDecimal base, BigInteger n)
Equivalent topow(BigDecimal, BigDecimal)
withinvert(n)
as the exponent, except thatn
must be greater than zero, and in the case thatbase
is negative,n
is odd, andinvert(n)
can't be expressed exactly as a decimal, this method will succeed whereaspow(base, invert(n))
will likely fail due to representing the denominator of the exponent as an even number rather than odd.static BigDecimal
nthRoot(BigInteger base, long n)
Equivalent topow(BigDecimal, BigDecimal)
withinvert(n)
as the exponent, except thatn
must be greater than zero, and in the case thatbase
is negative,n
is odd, andinvert(n)
can't be expressed exactly as a decimal, this method will succeed whereaspow(base, invert(n))
will likely fail due to representing the denominator of the exponent as an even number rather than odd.static BigDecimal
nthRoot(BigInteger base, BigInteger n)
Equivalent topow(BigDecimal, BigDecimal)
withinvert(n)
as the exponent, except thatn
must be greater than zero, and in the case thatbase
is negative,n
is odd, andinvert(n)
can't be expressed exactly as a decimal, this method will succeed whereaspow(base, invert(n))
will likely fail due to representing the denominator of the exponent as an even number rather than odd.static BigDecimal
pow(BigDecimal base, long power)
Returnsbase^power
.static BigDecimal
pow(BigDecimal base, BigDecimal power)
Returnsbase^power
.static BigDecimal
pow(BigDecimal base, BigInteger power)
Returnsbase^power
.static BigDecimal
pow(BigInteger base, long power)
Returnsbase^power
.static BigDecimal
pow(BigInteger base, BigDecimal power)
Returnsbase^power
.static BigDecimal
pow(BigInteger base, BigInteger power)
Returnsbase^power
.static BigInteger
round(BigDecimal val)
Returnsval
rounded to the nearestBigInteger
.static BigDecimal
round(BigDecimal val, int decimalPlaces)
Returnsval
rounded to the given number of decimal places.static BigDecimal
round(BigDecimal val, int decimalPlaces, RoundingMode roundingMode)
Returnsval
rounded to the given number of decimal places, using the givenRoundingMode
.static BigInteger
round(BigDecimal val, RoundingMode roundingMode)
static BigDecimal
sqrt(BigDecimal val)
Returns the square root of the given value.static BigDecimal
sqrt(BigInteger val)
Returns the square root of the given value.static BigDecimal
subtract(BigDecimal a, BigDecimal b)
Returnsa
minusb
.static BigDecimal
subtract(BigDecimal a, BigInteger b)
Returnsa
minusb
.static BigDecimal
subtract(BigInteger a, BigDecimal b)
Returnsa
minusb
.static BigInteger
subtract(BigInteger a, BigInteger b)
Returnsa
minusb
.static BigInteger[]
wholeAndDecimalParts(BigDecimal val)
Returns a 2-element array ofBigIntegers
that represents the whole part and the decimal part ofval
.static BigInteger
wholePart(BigDecimal val)
Returns the whole number part ofval
.
-
Field Details
-
BD0
TheBigDecimal
0, with no decimal digits -
BD1
TheBigDecimal
1, with no decimal digits -
BDM1
TheBigDecimal
-1, with no decimal digits -
BI0
TheBigInteger
0 -
BI1
TheBigInteger
1 -
BIM1
TheBigInteger
-1 -
BDPI
ABigDecimal
representation ofpi
approximated to 110 decimal places. -
BDE
ABigDecimal
representation ofe
approximated to 110 decimal places.
-
-
Method Details
-
bd
Returns aBigDecimal
representation ofval
. -
bd
Returns aBigDecimal
representation ofval
. -
bd
Returns aBigDecimal
representation ofval
. -
bd
Returns aBigDecimal
representation ofval
. -
bi
Returns aBigInteger
representation ofval
. -
bi
Returns aBigInteger
representation ofval
. -
isPos
Returns true ifval
is greater than 0, false otherwise. -
isPos
Returns true ifval
is greater than 0, false otherwise. -
isNeg
Returns true ifval
is less than 0, false otherwise. -
isNeg
Returns true ifval
is less than 0, false otherwise. -
isZero
Returns true ifval
is equal in magnitude to 0 (ignoring scale), false otherwise. -
isZero
Returns true ifval
is equal to 0, false otherwise. -
isOne
Returns true ifval
is equal in magnitude to 1 (ignoring scale), false otherwise. -
isOne
Returns true ifval
is equal to 1, false otherwise. -
equal
Returns true ifa
andb
are the same value, ignoring scale. -
equal
Returns true ifa
andb
are the same value, ignoring the scale ofa
. -
equal
Returns true ifa
andb
are the same value, ignoring the scale ofb
. -
equal
Returns true ifa
andb
are equal. -
notEqual
Returns true ifa
andb
are not the same value, ignoring scale. -
notEqual
Returns true ifa
andb
are not the same value, ignoring the scale ofa
. -
notEqual
Returns true ifa
andb
are not the same value, ignoring the scale ofb
. -
notEqual
Returns true ifa
andb
are not equal. -
greater
Returns true ifa
is greater thanb
. -
greater
Returns true ifa
is greater thanb
. -
greater
Returns true ifa
is greater thanb
. -
greater
Returns true ifa
is greater thanb
. -
less
Returns true ifa
is less thanb
. -
less
Returns true ifa
is less thanb
. -
less
Returns true ifa
is less thanb
. -
less
Returns true ifa
is less thanb
. -
greaterOrEqual
Returns true ifa
is greater than or equal tob
, ignoring scale. -
greaterOrEqual
Returns true ifa
is greater than or equal tob
, ignoring the scale ofa
. -
greaterOrEqual
Returns true ifa
is greater than or equal tob
, ignoring the scale ofb
. -
greaterOrEqual
Returns true ifa
is greater than or equal tob
. -
lessOrEqual
Returns true ifa
is less than or equal tob
, ignoring scale. -
lessOrEqual
Returns true ifa
is less than or equal tob
, ignoring the scale ofa
. -
lessOrEqual
Returns true ifa
is less than or equal tob
, ignoring the scale ofb
. -
lessOrEqual
Returns true ifa
is less than or equal tob
. -
isInt
Returns true ifval
is an integer, false otherwise.val
is considered an integer if it has no decimal digits or if every decimal digit is a 0.val
need not be able to fit into anint
to be considered an integer.If this method returns true,
val.toBigIntegerExact()
would succeed. If this method returns false, that would fail.Examples:
1000000000000000000000000000 → true 1000000000000000000000000000.000000 → true 1000000000000000000000000000.000001 → false 0 → true 0.0 → true -1000000000000000000000000000 → true -1000000000000000000000000000.000000 → true -1000000000000000000000000000.000001 → false
-
round
Returnsval
rounded to the nearestBigInteger
. UsesRoundingMode.HALF_UP
.Examples:
1.8 → 2 1.5 → 2 1.2 → 1 1.0 → 1 1 → 1 0.8 → 1 0.5 → 1 0.2 → 0 0.0 → 0 0 → 0 -0.2 → 0 -0.5 → -1 -0.8 → -1 -1 → -1 -1.0 → -1 -1.2 → -1 -1.5 → -2 -1.8 → -2
-
round
-
round
Returnsval
rounded to the given number of decimal places. UsesRoundingMode.HALF_UP
.decimalPlaces
must be at least zero. If it is zero, the result will be a whole number.The result will not have any trailing zeros in the decimal part.
Examples:
10 , 0 → 10 10 , 1 → 10 10.0 , 0 → 10 10.0 , 1 → 10 10.01 , 0 → 10 10.01 , 1 → 10 10.01 , 2 → 10.01 1.9803, 0 → 2 1.9803, 1 → 2 1.9803, 2 → 1.98 1.9805, 3 → 1.981 1.9803, 5 → 1.9803 0.00 , 4 → 0 -1.0 , 1 → -1 -1.55 , 0 → -2 -1.55 , 1 → -1.6 -1.55 , 2 → -1.55 -1.55 , 3 → -1.55
-
round
Returnsval
rounded to the given number of decimal places, using the givenRoundingMode
.decimalPlaces
must be at least zero. If it is zero, the result will be a whole number.The result will not have any trailing zeros in the decimal part.
-
wholePart
Returns the whole number part ofval
. The returned value will have the same sign sign asval
, unlessval
is a negative number between 0 and -1, in which case the returned value will be 0.Examples:
3.80 → 3 3.0 → 3 3 → 3 0.5 → 0 0 → 0 -0.5 → 0 -3.80 → -3 -3.0 → -3 -3 → -3
-
decimalPart
Returns the decimal part ofval
(as a decimal, not scaled to an integer value). The returned value will have the same sign sign asval
, unlessval
is an integer, in which case 0 will be returned. Any trailing zeros will be removed.To have the decimal part represented as an integer, use
wholeAndDecimalParts(BigDecimal)
.Examples:
3.80 → 0.8 3.0 → 0 3 → 0 0.5 → 0.5 0 → 0 -0.5 → -0.5 -3.80 → -0.8 -3.0 → 0 -3 → 0
-
wholeAndDecimalParts
Returns a 2-element array ofBigIntegers
that represents the whole part and the decimal part ofval
.The first element of the array is equivalent to
wholePart(BigDecimal)
. The second element is equivalent todecimalPart(BigDecimal)
, but scaled as an integer.Examples:
3.80 → [ 3, 8] 3.0 → [ 3, 0] 3 → [ 3, 0] 0.5 → [ 0, 5] 0 → [ 0, 0] -3.80 → [-3, -8] -3.0 → [-3, 0] -3 → [-3, 0] -0.5 → [ 0, -5]
-
mixedNumber
Returns a mixed number representation ofval
, i.e.,a+b/c
, wherea
is the whole part ofval
,b
is the decimal part scaled as an integer, andc
is 10 raised to the power of the number of decimal digits. Note that theb/c
fraction will not necessarily be reduced.The returned value is a 3-element array of
BigIntegers
, consisting of:wholeAndDecimalParts(BigDecimal)
[0]
wholeAndDecimalParts[1]
- 10 raised to the power of the number of decimal digits in
val
, excluding trailing zeros. (1 ifval
is an integer.)
Examples:
3.80 → [ 3, 8, 10] 3.0 → [ 3, 0, 1] 3 → [ 3, 0, 1] 0.5 → [ 0, 5, 10] 0 → [ 0, 0, 1] -3.80 → [-3, -8, 10] -3.0 → [-3, 0, 1] -3 → [-3, 0, 1] -0.5 → [ 0, -5, 10]
-
fraction
Returns a fraction representation ofval
.The returned value is a 2-element array of
BigIntegers
, consisting of:mixedNumber(BigDecimal)
[0] * mixedNumber[2] + mixedNumber[1]
- mixedNumber[2]
Examples:
3.80 → [ 38, 10] 3.0 → [ 3, 1] 3 → [ 3, 1] 0.5 → [ 5, 10] 0 → [ 0, 1] -3.80 → [-38, 10] -3.0 → [- 3, 1] -3 → [- 3, 1] -0.5 → [- 5, 10]
-
digits
Returns 2 arrays, the first of which contains the digits ofval
before the decimal point, and the second of which contains the digits after the decimal point. Ifval
is an integer, the second array will be empty. The second array will never have trailing zeros.val
may negative, but the returned arrays will never include any negative digits.Examples:
123.45600 → [ [1,2,3], [4,5,6] ] 3.0 → [ [3], [] ] 3 → [ [3], [] ] 0.5 → [ [0], [5] ] 0 → [ [0], [] ] - 3.0 → [ [3], [] ] -123.45600 → [ [1,2,3], [4,5,6] ]
-
digits
Returns the digits ofval
.val
may negative, but the returned array will never include any negative digits.Examples:
123 → [1,2,3] 3 → [3] 0 → [0] -3 → [3] -123 → [1,2,3]
-
digitCount
Returns the number of digits ofval
.val
may negative, but this method only considersabs(val)
. Ifval
is 0, 1 is returned.Examples:
123 → 3 3 → 1 0 → 1 -3 → 1 -123 → 3
-
add
Returns the sum of the given values. -
add
Returns the sum of the given values. -
add
Returns the sum of the given values. -
add
Returns the sum of the given values. -
subtract
Returnsa
minusb
. -
subtract
Returnsa
minusb
. -
subtract
Returnsa
minusb
. -
subtract
Returnsa
minusb
. -
multiply
Returns the product of the given values. -
multiply
Returns the product of the given values. -
multiply
Returns the product of the given values. -
multiply
Returns the product of the given values. -
divide
Returnsa
divided byb
. If rounding is necessary because the result is a non-terminating decimal, the result will be rounded to a large number of significant figures (at least 100). The result will not have trailing decimal zeros. -
divide
Returnsa
divided byb
. If rounding is necessary because the result is a non-terminating decimal, the result will be rounded to a large number of significant figures (at least 100). The result will not have trailing decimal zeros. -
divide
Returnsa
divided byb
. If rounding is necessary because the result is a non-terminating decimal, the result will be rounded to a large number of significant figures (at least 100). The result will not have trailing decimal zeros. -
divide
Returnsa
divided byb
. Note that even though the arguments areBigInteger
, the result is aBigDecimal
to allow decimal digits in the result. If rounding is necessary because the result is a non-terminating decimal, the result will be rounded to a large number of significant figures (at least 100). The result will not have trailing decimal zeros. -
invert
Returns1/val
. If rounding is necessary because the result is a non-terminating decimal, the result will be rounded to a large number of significant figures (at least 100). The result will not have trailing decimal zeros. -
invert
Returns1/val
. Note that even though the argument is aBigInteger
, the result is aBigDecimal
to allow decimal digits in the result. If rounding is necessary because the result is a non-terminating decimal, the result will be rounded to a large number of significant figures (at least 100). The result will not have trailing decimal zeros. -
pow
Returnsbase^power
.If
base
is 0,power
must be greater than 0. Will succeed without overflow ifabs(base)
is less than 1,000,000,000,000,000,000,000 andabs(power)
is less than 100,000,000. If one value is smaller than its limit and the other value is somewhat larger than its limit, the calculation might succeed. -
pow
Returnsbase^power
.If
base
is 0,power
must be greater than 0. Will succeed without overflow ifabs(base)
is less than 1,000,000,000,000,000,000,000 andabs(power)
is less than 100,000,000. If one value is smaller than its limit and the other value is somewhat larger than its limit, the calculation might succeed. -
pow
Returnsbase^power
.If
base
is negative,power
must either be an integer or a number that can be expressed as a fraction with an odd denominator.If
base
is 0,power
must be greater than 0. Will succeed without overflow ifabs(base)
is less than 1,000,000,000,000,000,000,000 andabs(power)
is less than 100,000,000. If one value is smaller than its limit and the other value is somewhat larger than its limit, the calculation might succeed. -
pow
Returnsbase^power
.If
base
is 0,power
must be greater than 0. Will succeed without overflow ifabs(base)
is less than 1,000,000,000,000,000,000,000 andabs(power)
is less than 100,000,000. If one value is smaller than its limit and the other value is somewhat larger than its limit, the calculation might succeed.Returns a
BigDecimal
since the result may not be an integer (whenpower
is negative). -
pow
Returnsbase^power
.If
base
is 0,power
must be greater than 0. Will succeed without overflow ifabs(base)
is less than 1,000,000,000,000,000,000,000 andabs(power)
is less than 100,000,000. If one value is smaller than its limit and the other value is somewhat larger than its limit, the calculation might succeed.Returns a
BigDecimal
since the result may not be an integer (whenpower
is negative). -
pow
Returnsbase^power
.If
base
is negative,power
must either be an integer or a number that can be expressed as a fraction with an odd denominator.If
base
is 0,power
must be greater than 0. Will succeed without overflow ifabs(base)
is less than 1,000,000,000,000,000,000,000 andabs(power)
is less than 100,000,000. If one value is smaller than its limit and the other value is somewhat larger than its limit, the calculation might succeed. -
nthRoot
Equivalent topow(BigDecimal, BigDecimal)
withinvert(n)
as the exponent, except thatn
must be greater than zero, and in the case thatbase
is negative,n
is odd, andinvert(n)
can't be expressed exactly as a decimal, this method will succeed whereaspow(base, invert(n))
will likely fail due to representing the denominator of the exponent as an even number rather than odd.Examples:
nthRoot(-1, 3)
→ -1pow(-1, 0.33333...)
→ArithmeticException
nthRoot(-8, 3)
→ -2pow(-8, 0.33333...)
→ArithmeticException
nthRoot(pow(-8, 5)), 3)
→ -32pow(-8, 1.6666...)
→ArithmeticException
nthRoot( 1, 3)
→ 1pow( 1, 0.33333...)
→ 1nthRoot( 8, 3)
→ 2pow( 8, 0.33333...)
→ 2 -
nthRoot
Equivalent topow(BigDecimal, BigDecimal)
withinvert(n)
as the exponent, except thatn
must be greater than zero, and in the case thatbase
is negative,n
is odd, andinvert(n)
can't be expressed exactly as a decimal, this method will succeed whereaspow(base, invert(n))
will likely fail due to representing the denominator of the exponent as an even number rather than odd.Examples:
nthRoot(-1, 3)
→ -1pow(-1, 0.33333...)
→ArithmeticException
nthRoot(-8, 3)
→ -2pow(-8, 0.33333...)
→ArithmeticException
nthRoot(pow(-8, 5)), 3)
→ -32pow(-8, 1.6666...)
→ArithmeticException
nthRoot( 1, 3)
→ 1pow( 1, 0.33333...)
→ 1nthRoot( 8, 3)
→ 2pow( 8, 0.33333...)
→ 2 -
nthRoot
Equivalent topow(BigDecimal, BigDecimal)
withinvert(n)
as the exponent, except thatn
must be greater than zero, and in the case thatbase
is negative,n
is odd, andinvert(n)
can't be expressed exactly as a decimal, this method will succeed whereaspow(base, invert(n))
will likely fail due to representing the denominator of the exponent as an even number rather than odd.Examples:
nthRoot(-1, 3)
→ -1pow(-1, 0.33333...)
→ArithmeticException
nthRoot(-8, 3)
→ -2pow(-8, 0.33333...)
→ArithmeticException
nthRoot(pow(-8, 5)), 3)
→ -32pow(-8, 1.6666...)
→ArithmeticException
nthRoot( 1, 3)
→ 1pow( 1, 0.33333...)
→ 1nthRoot( 8, 3)
→ 2pow( 8, 0.33333...)
→ 2 -
nthRoot
Equivalent topow(BigDecimal, BigDecimal)
withinvert(n)
as the exponent, except thatn
must be greater than zero, and in the case thatbase
is negative,n
is odd, andinvert(n)
can't be expressed exactly as a decimal, this method will succeed whereaspow(base, invert(n))
will likely fail due to representing the denominator of the exponent as an even number rather than odd.Examples:
nthRoot(-1, 3)
→ -1pow(-1, 0.33333...)
→ArithmeticException
nthRoot(-8, 3)
→ -2pow(-8, 0.33333...)
→ArithmeticException
nthRoot(pow(-8, 5)), 3)
→ -32pow(-8, 1.6666...)
→ArithmeticException
nthRoot( 1, 3)
→ 1pow( 1, 0.33333...)
→ 1nthRoot( 8, 3)
→ 2pow( 8, 0.33333...)
→ 2 -
sqrt
Returns the square root of the given value.val
must be non-negative. -
sqrt
Returns the square root of the given value.val
must be non-negative. -
exp
Returnse^power
.abs(power)
must be less or equal to 4,900,000,000. -
exp
Returnse^power
.abs(power)
must be less or equal to 4,900,000,000. -
exp
Returnse^power
.abs(power)
must be less or equal to 4,900,000,000. -
ln
Returns the natural logarithm ofval
.val
must be greater than 0. -
ln
Returns the natural logarithm ofval
.val
must be greater than 0. More specifically,val
must be greater than 10^-10,000,000 and less than 10^10,000,000. -
ln
Returns the natural logarithm ofval
.val
must be greater than 0. More specifically,val
must be greater than 10^-10,000,000 and less than 10^10,000,000. -
log
Returns the log of the givenbase
ofval
.base
must be greater than 0 and not equal to 1.val
must be greater than 0. -
log
Returns the log of the givenbase
ofval
.base
must be greater than 0 and not equal to 1.val
must be greater than 0. -
log
Returns the log of the givenbase
ofval
.base
must be greater than 0 and not equal to 1.val
must be greater than 0. -
log
Returns the log of the givenbase
ofval
.base
must be greater than 0 and not equal to 1.val
must be greater than 0. -
log
Returns the log of the givenbase
ofval
.base
must be greater than 0 and not equal to 1.val
must be greater than 0. -
log
Returns the log of the givenbase
ofval
.base
must be greater than 0 and not equal to 1.val
must be greater than 0. -
log
Returns the log of the givenbase
ofval
.base
must be greater than 0 and not equal to 1.val
must be greater than 0. -
log
Returns the log of the givenbase
ofval
.base
must be greater than 0 and not equal to 1.val
must be greater than 0. -
log
Returns the log of the givenbase
ofval
.base
must be greater than 0 and not equal to 1.val
must be greater than 0.
-