0

I want to compute numbers with arbitrarily long decimal values (suppose I want to take a 100 decimal digits for a particular number).

//Take the square root of 2 for 100 digits in total.
public class Main {
    public static void main(String[] args) {
        BigDecimal root2 = new BigDecimal(Math.pow(2, 0.5));
        root2 = root2.setScale(99, BigDecimal.ROUND_HALF_UP);
        System.out.println(root2);
    }
}

And this program outputs:

1.414213562373095145474621858738828450441360473632812500000000000000000000000000000000000000000000000

Having a beginner background in the storage of bytes, I realize the issue here is either the output format, or the fact that I'm still not getting the precision I'm looking for since it doesn't use the memory I'm expecting it to. Any suggestions? Is it possible to get precision this high? I'm not against turning to Mathematica.

For clarification: I'm trying to get the full precision of 100 digits (the zeros do not belong here).

  • You currently have 100 digits. How many do you want? – Elliott Frisch Dec 19 '16 at 23:12
  • 1
    It's not that it's the wrong length, it's the fact that `sqrt(2)` is irrational and shouldn't have trailing zeros like that. It doesn't store the whole number I'm looking for. –  Dec 19 '16 at 23:14
  • See http://stackoverflow.com/questions/13649703/square-root-of-bigdecimal-in-java for a possible answer. – bithead61 Dec 19 '16 at 23:16
  • 3
    Where do you expect the added precision to come from? The incoming value is double-precision float, and it only has about 16 decimal digits of precision. – Hot Licks Dec 19 '16 at 23:16
  • 1
    But `Math.pow(2, 0.5)` is returning a `double`, so you have limited precision. If you want to do true square roots with `BigDecimal`, see [square-root-of-bigdecimal-in-java](http://stackoverflow.com/questions/13649703/square-root-of-bigdecimal-in-java) – azurefrog Dec 19 '16 at 23:16
  • Possible duplicate of [Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it?](http://stackoverflow.com/questions/16441769/javas-bigdecimal-powerbigdecimal-exponent-is-there-a-java-library-that-does) – James McNee Dec 19 '16 at 23:22
  • @JamesMacca I figured this wasn't a duplicate as OP isn't aware why their solution isn't working, and so they don't know they need a library. – WW. Dec 19 '16 at 23:23
  • Have you considered one of the wrappers/implementations of GMP ? – ivanivan Dec 19 '16 at 23:24

1 Answers1

4

Here is an explanation why what you are doing is not working.

Math.pow() returns a double. So your code is calculating the value using the precision provided by double, and then creating a BigDecimal from that double. It's too late to set the precision on the big decimal since it only has the double as input.

So you can not use Math.pow() for your purpose. Instead you need to find another library that does arbitrary precision calculations.

Refer to this question for more: Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it?

Community
  • 1
  • 1
WW.
  • 23,793
  • 13
  • 94
  • 121