Are there any libraries for Square Root over BigDecimal in Java?
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.
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.
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.
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.
Try Big Square Roots. It uses the Newton's method for approximating solutions such as square roots.
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