Java's BigDecimal.pow(int)
method only accepts an integer parameter, no BigDecimal
parameter.
Is there a library, like Apache's commons-lang, that supports BigDecimal.pow(BigDecimal)
? It should be able to do calculate "1.21".pow("0.5")
to return "1.1"
.
A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.
add(BigDecimal val) is used to calculate the Arithmetic sum of two BigDecimals. This method is used to find arithmetic addition of large numbers of range much greater than the range of largest data type double of Java without compromising with the precision of the result.
The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal . The BigDecimal class gives its user complete control over rounding behavior.
The largest value BigDecimal can represent requires 8 GB of memory.
There is a Math.BigDecimal implementation of core mathematical functions with source code available from the Cornell University Library here (also you can download the library as a tar.gz). Here is a sample of the library use:
import org.nevec.rjm.*; import java.math.BigDecimal; public class test { public static void main(String... args) { BigDecimal a = new BigDecimal("1.21"); BigDecimal b = new BigDecimal("0.5"); System.out.println(BigDecimalMath.pow(a, b).toString()); } }
Prints out:
1.1
The license information is now clear in the May 2015 update:
The full source code is made available under the LGPL v3.0.
Havent used, but saw suanshu. An open source version in github (https://github.com/nmdev2020/SuanShu).
BigDecimalUtils has a pow() which suits your needs
public static java.math.BigDecimal pow(java.math.BigDecimal a, java.math.BigDecimal b) Compute a to the power of b.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With