• 05-07-2008, 08:45 PM
michcio
hello I wonder if there is a way to easy store a huge amount decimals. I have done a program that returns PI with many decimals that are right...But double can only store up to 14 decimals...how could I show my PI(System.out.println:D) ?? please help!!!

here is my short code

Code:

```    public static void main(String[] args) { double summa = 0; for(double i=0; i<100; i++)             summa += Math.pow((-1),i)/(2*i+1) * (4/(Math.pow(5,(2*i+1)))-(1/Math.pow(239, (2*i+1)))); summa *= 4; System.out.println(summa);                             } }```
it's a really short program that only add 100 of elemnts and then I got 14 deciomals right...but please help how do I do so I can see more then 14 decimals??????:(
• 05-07-2008, 11:06 PM
rjuyal
BigD!
• 05-08-2008, 12:48 AM
michcio
ah nice I didn't know there was a special class for this :D...can BigDecimal have as many Decimals I want?...can you explain a bit how I should use it :o
• 05-08-2008, 08:31 AM
rjuyal
Just like you working with double

But don't use +, - , *, / , instead use its owb methods:

http://java.sun.com/j2se/1.5.0/docs/...igDecimal.html
• 05-08-2008, 08:56 PM
michcio
wow nice now I can finally write my PI :D ty very much :D
• 05-08-2008, 09:00 PM
rjuyal
```public static void main(String[] args) throws IOException{       [COLOR="DarkOrange"][SIZE="4"][B]//reading PI's decimals so I can compare them with thoes I count[/B][/SIZE]       BufferedReader br = new BufferedReader                           (new FileReader("PI1.txt"));         String PI = "";         int k = 0;         while(true){             String fil = br.readLine();             if(fil==null)                 break;             PI += fil;         }         br.close();[/COLOR]       [COLOR="Green"] [SIZE="4"][B]//describing MathContext and some variables[/B][/SIZE]         MathContext mode = new MathContext(1000000);         BigDecimal a0 = new BigDecimal(1);         BigDecimal b0 = new BigDecimal(1).divide(sqrt(new BigDecimal(2),mode),mode);         BigDecimal t0 = new BigDecimal(0.25);         BigDecimal p0 = a0;         int iterationer = 16;         boolean fortsätt = true;         int totalt = 0;[/COLOR]         while(fortsätt){       [COLOR="Red"]         [SIZE="4"][B]//the intersesting for-loop [/B][/SIZE]         for(int i=1; i<=iterationer; i++){             BigDecimal a1 = (a0.add(b0,mode)).divide(new BigDecimal(2),mode);             BigDecimal b1 = sqrt(a0.multiply(b0,mode),mode);             BigDecimal t1 = t0.subtract(p0.multiply(a0.subtract(a1).pow(2),mode),mode);             BigDecimal p1 = p0.multiply(new BigDecimal(2),mode);             a0 = a1;             b0 = b1;             t0 = t1;             p0 = p1;             System.out.println((i+totalt) + " varv av " + (iterationer+totalt));         }[/COLOR]         BigDecimal summa = a0.add(b0,mode).pow(2,mode).divide(t0.multiply(new BigDecimal(4),mode),mode);         System.out.println("Antal rätt decimaler = " + (antalsamma((summa + ""),PI)-2));         if(JOptionPane.showConfirmDialog(null,"Fortsätta?","Pi",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){             fortsätt = true;             totalt += iterationer;             iterationer = Integer.parseInt(JOptionPane.showInputDialog("Hur många iterationer till?") );   }         else             fortsätt = false;   } } [COLOR="Silver"] [SIZE="4"][B]  // some of my decimals to the left, number of loops, to the right the correct                      //number of decimals[/B][/SIZE]   //varv        rätt decimaler     // 9              1 391     // 10            2 787     // 11            5 581     // 12            11 170     // 13            22 347     // 14            44 700     // 15            89 409     // 16            178 824     // 17            >200 000[/COLOR]       [COLOR="Teal"][SIZE="4"][B]//a method to compare my PI with the one I have read in the beginning of   //this program[/B][/SIZE]   public static int antalsamma(String a, String b){         int längd = Math.min(a.length(),b.length());         int i=0;         for(;i<längd;i++){           if(a.charAt(i)!= b.charAt(i))               break;         }       return i;     }[/COLOR]   private static final BigDecimal EIGHT = new BigDecimal(8);   private static final BigDecimal TEN = new BigDecimal(10);   private static final BigDecimal THREE = new BigDecimal(3);   private static final BigDecimal FIF = new BigDecimal(15); [COLOR="Blue"]  [SIZE="4"][B]//the sqrt method[/B][/SIZE] public static BigDecimal sqrt(BigDecimal S, MathContext mc){     MathContext mode = new MathContext(mc.getPrecision()+2);     BigDecimal y = new BigDecimal(0);     BigDecimal x0 = new BigDecimal(0);     BigDecimal x1 = new BigDecimal(1/Math.sqrt(S.doubleValue()));     while(antalsamma(x0.toPlainString(),x1.toPlainString())<mc.getPrecision()){       x0 = x1;       y = S.multiply(x0.multiply(x0,mode),mode);       x1 = x0.divide(EIGHT,mode).multiply(FIF.subtract(y.multiply(TEN.subtract(THREE.multiply(y,mode)),mode)),mode).abs();     }     return x1.multiply(S,mc);   }[/COLOR] }```