Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there libraries for Square Root over BigDecimal? [closed]

Tags:

java

math

Are there any libraries for Square Root over BigDecimal in Java?

like image 262
Daniel C. Sobral Avatar asked Sep 06 '09 05:09

Daniel C. Sobral


People also ask

How do you find the square root of BigDecimal numbers?

math. BigDecimal. sqrt(MathContext mc) is an inbuilt function added in Java SE 9 & JDK 9 which returns BigDecimal value of square root of a BigDecimal on which sqrt() method is applied with rounding according to the context settings.

What is MathContext in Java?

The java. math. MathContext class provides immutable objects which encapsulate the context settings and describes certain rules for numerical operators, such as those implemented by the BigDecimal class.

Where can I use BigDecimal?

The BigDecimal class provides operations on double numbers for arithmetic, scale handling, rounding, comparison, format conversion and hashing. It can handle very large and very small floating point numbers with great precision but compensating with the time complexity a bit.


2 Answers

JScience v4.3.1 has a Real class which seems to be the equivalent of BigDecimal and that might help you. An example of usage:

// The Square Root of Two, to 30 digits // According to "The Square Root of Two, to 5 million digits." // Source: http://www.gutenberg.org/files/129/129.txt System.out.println("1.41421356237309504880168872420");  // Using JScience with 50 digits precision Real.setExactPrecision(50); System.out.println(Real.valueOf(2).sqrt());  // Using default java implementation System.out.println(Math.sqrt(2));  > 1.41421356237309504880168872420 > 1.414213562373095048801689 > 1.4142135623730951 

Edit: Updated the code and the links to reflect the current version at the time (v4.3.1). Based on @ile an @Tomasz comments, thanks.

like image 153
Redder Avatar answered Oct 05 '22 11:10

Redder


Try Big Square Roots. It uses the Newton's method for approximating solutions such as square roots.

like image 34
cletus Avatar answered Oct 05 '22 12:10

cletus