java - Using BigDecimal to Print Pi: Code is inefficient and doesn't work -


I am playing with BigDekalk today, and ended up with the code below. There is no problem according to my compiler, but after walking I get a few exceptions. Any help is appreciated, thanks a lot: -)

  import java.math. *; // The method used to calculate Ramanujan Pi public class main {// a1-4) is the constant constant used in the calculation of the side is the personal static last big decimilium A1 = new budicidal (9810); Private static final abiotic A2 = new budicidal (1103); Private Static Finals BigDecell A3 = New BigDial (26390.0); Private Stable Last BigDec A4 = New BigDial (396.0); // con1 2sqrt (2) is the exact reflexion of the private static final BigDQmail con1 = BigDecimal.valueOf (2 * Math.pow (2, -1)); With the increase in the size of // var loops, the PI personal static final leads to more precise production of BigDecular Loops = new BigDec (2); // Asum = 1 / pi later private static BigDecimal asum in the program; // fouri = 4 ... this private static BigDecimal charve = new BigDecimal (4) may not be required; // Factorial :: N * N-1 * N-2 ... * 1 Public Stable BigDecal Factorial (BigDial N) {If (Great (NT, BigDicell .Jero)) Bigadechel. Return n.multiply (factual (n.subtract (BigDecimal.ONE))); } // calc0-4 In the main calculation, for the stability, the public stable BigDec is present for the increase in Calc0 () {return con1.divide (a1, loops.intValue (* 16, BigDec.ROUND_HALF_DOWN); } Public Static BigDec Mail Mail 1 (BigDecimal i) {BigDecal Rate; Rate = (White (Ch. Most likely (i))); Return writ; } Public Static BigDiemail Mail 2 (BigDecimal i) {Return (a2.add (a3.multiply (i))); } Public stable BigDiskim Calse 3 (BigDecimal i) {BigDicale Rated = New BigDial (Math Pow (Factorial (I). Dual (), 4)); Return writ; } Public static BigDiskim Calcis4 (BigDecimal i) {BigDicell Retet = New Begidikle (MathPau (A 4.doubleValue (), (i.doubleValue () * 4))); Return writ; } // compares two BigDecimals and if I & lt; = A public static boolean grt (Bigidiki i, biodical A) {BigDial B = New BigDesque (a.compareTo (i)); If (B.F.ANAL (BigDicl. Zorro)) Beaieges (BigDic.On)) comes true; return false; } Public static zero main (string [] args) {// Main calculation - for (Bigidiki i = New BigDiskim (0); Great (I, Loop); i.add (i.ONE)) {asum = asum added (Calc0 (). Multiplied (calc1 (i)). Multiply (calc4 (i)); Multiply (calc2 (i)); split (calc3 (i), loops.intValue () * 16, bigidil ROUND_HALF_DOWN.); } // print Pi System.out.println (asum.pow (-1)); }  

}

here asum Is empty

  private static BigDekal Assam; // to start  

then

 for  (Bigidiki i = New BigDescimal (0); Great (i, loop); i.add (I. One)) {asum = asum.add (// remaining code); // You will get NullPointerException}  

solution

get started asum

  Private static BigDecimal asum = New BigDecimal (0);  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -