7

I am currently using the BigDecimal and it is giving me more decimals but not nearly enough for what I am trying to do. I need to be able to get all the way to the 10^6 digit. This is my current code

BigDecimal num = new BigDecimal(103993/33102.0);
    pw.println(num.toString());

and it outputs 3.14159265301190249175533608649857342243194580078125

where the number actually has a lot more decimals: http://www.wolframalpha.com/input/?i=103993%2F33102

FThompson
  • 28,352
  • 13
  • 60
  • 93
Will Jamieson
  • 918
  • 1
  • 16
  • 32

2 Answers2

17

You are loosing the precision when evaluating this:

103993/33102.0

as a double division. Actually, the following:

BigDecimal num = new BigDecimal(103993/33102.0);

is equivlent to:

double d = 103993/33102.0;
BigDecimal num = new BigDecimal(d);

instead, use:

int scale = 100;
BigDecimal num1 = new BigDecimal(103993);
BigDecimal num2 = new BigDecimal(33102);
System.out.println(num1.divide(num2, scale, RoundingMode.HALF_UP).toString());

OUTPUT:

3.1415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • in my eclipse console my output is: 3.1415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613 is that just cause eclipse dosent want to print the whole thing out – Will Jamieson Mar 01 '13 at 18:55
  • @WillJamieson You need to make `scale = 1000000`, but believe me it will take a long time. – Eng.Fouad Mar 01 '13 at 18:56
  • 1
    starting with scale = 10000 (ten thousands), it only prints "3." on my system. I wonder if it's eclipse or a Java bug... – Cyrille Ka Mar 01 '13 at 18:58
  • 1
    Strangely it doesn't help but it's ok. Works on command line. – Cyrille Ka Mar 01 '13 at 19:02
  • @CyrilleKarmann You are right, I change `scale` to `1000000` and after about 10 minutes, it prints `3.`. – Eng.Fouad Mar 01 '13 at 19:04
  • @CyrilleKarmann The maximum length that Eclipse can handle is `scale = 4095`. If I change it to `4096`, it prints `3.`. – Eng.Fouad Mar 01 '13 at 19:07
  • is there a quicker way to do this for getting 10,000 digits or is big decimal my only option? – Will Jamieson Mar 01 '13 at 19:30
  • @WillJamieson I am not sure if there are algorithms for achieving this with the primitive data types (i.e. the 64-bit double), but `BigDecimal` is designed for this purpose. – Eng.Fouad Mar 01 '13 at 19:33
3

The problem is how you are making your number. The 103993/33102.0 is evaluated as a double precision floating point expression (double) before the BigDecimal class ever gets involved. You should make separate BigDecimal objects and use BigDecimal.divide to get the number you want.

Since you want exact results, I'd probably pass both of your numbers in as integers when starting.

Even after doing that, I doubt you'll be able to get out to the 10^6 digit. You might need to implement your own division algorithm to get out to that level as any sane implementation of arbitrary precision math is going to stop long before that point (at least by default).

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Sean McSomething
  • 6,376
  • 2
  • 23
  • 28
  • 1
    I'm pretty sure `BigDecimal` has as much precision as you want it to have (you may just wait a little longer if you want more precision, you can specify `scale` appropriately in `divide`). – Bernhard Barker Mar 01 '13 at 18:59