Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it? [closed]

Tags:

java

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".

like image 431
Geoffrey De Smet Avatar asked May 08 '13 13:05

Geoffrey De Smet


People also ask

What is the package of BigDecimal in Java?

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.

Can you add BigDecimal in Java?

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.

What is the use of BigDecimal in Java?

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.

What is the limit of BigDecimal in Java?

The largest value BigDecimal can represent requires 8 GB of memory.


2 Answers

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 

Update

The license information is now clear in the May 2015 update:

The full source code is made available under the LGPL v3.0.

like image 114
higuaro Avatar answered Oct 16 '22 15:10

higuaro


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. 
like image 34
rajesh Avatar answered Oct 16 '22 15:10

rajesh