Results 1 to 8 of 8
  1. #1
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Exclamation get more decimals?!?! please help!

    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

    Java 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??????:(

  2. #2
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default BigD!

    how about BigDecimal!!! :)
    i am the future

  3. #3
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    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

  4. #4
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    Just like you working with double

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


    See the link, This might be helpful for you:

    http://java.sun.com/j2se/1.5.0/docs/...igDecimal.html
    i am the future

  5. #5
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    wow nice now I can finally write my PI :D ty very much :D

  6. #6
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    How about closing the thread as Solved :D
    i am the future

  7. #7
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    yeah how do I do that?

  8. #8
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    ok so I have worked a lot with BigDecimals now but I find them very slow...I have found a easy method to take sqrt of BigDeciamals (I need this method while describing decimals of PI) and a fast way to describe PI's decimals...though when I set the MathContext to 10^6 decimals and my computer is on for 24 h the program hasn't done the first for-loop yet:O look at my code:

    Java Code:
    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]
    }
    so my question now is...Is there a faster way to count with decimals?! I have seen programs which are abel to count PI with 10^6 decimals in just a few seconds... how can this be possible ... please HELP!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •