Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. package picalculator;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.MathContext;
  5. import java.util.ArrayList;
  6.  
  7. public class Vars {
  8.  
  9. public static final BigDecimal ZERO = new BigDecimal("0" );
  10. public static final BigDecimal ONE = new BigDecimal("1");
  11. public static final BigDecimal TWO = new BigDecimal("2");
  12. public static final BigDecimal FOUR = new BigDecimal("4");
  13.  
  14. public static BigDecimal LastPI = ZERO.plus();
  15. public static BigDecimal PI = new BigDecimal( "0.0", MathContext.DECIMAL128 );
  16. public static BigDecimal Count = new BigDecimal("0");
  17.  
  18. public static ArrayList<BigDecimal> PIAvgs = new ArrayList<>( 1000000 );
  19.  
  20. public static boolean shouldStop = false;
  21.  
  22. public static final Object piLock = new Object();
  23.  
  24. }
  25.  
  26. package picalculator;
  27.  
  28. import java.math.BigDecimal;
  29. import java.math.MathContext;
  30. import static picalculator.Vars.*;
  31.  
  32. public class BDFuncs {
  33.  
  34. // The magic happens here
  35. // This function calculates the next PI in the series, as well as the actual child series while creating other news (see updateAverage()).
  36. public static void nextCicle() {
  37. synchronized ( piLock ) {
  38. LastPI = PI.plus();
  39. if ( shouldSubstract() ) PI = PI.subtract( getCicleTerm() );
  40. else PI = PI.add( getCicleTerm() );
  41. updateAverage();
  42. Count = Count.add( ONE );
  43. }
  44. }
  45.  
  46. private static BigDecimal getCicleTerm() {
  47. return ONE.divide( Count.multiply( TWO ).add( ONE ), MathContext.DECIMAL128 );
  48. }
  49.  
  50. private static boolean shouldSubstract() {
  51. if ( Count.remainder( TWO ).compareTo( ONE ) == 0 ) return true;
  52. else return false;
  53. }
  54.  
  55. // This function works by calculating ONLY the averages at the last side of the tree.
  56. // It first calcles the last average of the first child series, and then using that
  57. // average and the last average of the same child series(if any), calculates its average
  58. // and sets it as the second child series' last child, repeating so until it reaches the
  59. // top of the tree, or in this case, while the index is more or equal than 0.
  60. private static void updateAverage() {
  61. PIAvgs.add( average( LastPI, PI ) );
  62. for ( int i = PIAvgs.size() - 2; i >= 0; i-- ) {
  63. PIAvgs.set( i, average( PIAvgs.get( i ) , PIAvgs.get( i + 1 ) ) );
  64. }
  65. }
  66.  
  67. private static BigDecimal average( BigDecimal bd1, BigDecimal bd2 ) {
  68. return bd1.add( bd2 ).divide( TWO );
  69. }
  70.  
  71. public static BigDecimal[] getData() {
  72. BigDecimal[] tempArr = new BigDecimal[3];
  73. synchronized ( piLock ) {
  74. tempArr[0] = PI.plus();
  75. tempArr[1] = ( PIAvgs.isEmpty() ? ZERO : PIAvgs.get(0) );
  76. tempArr[2] = Count.plus();
  77. }
  78. return tempArr;
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement