Guest User

Untitled

a guest
May 20th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package client;
  2.  
  3. import compute.Task;
  4. import java.io.Serializable;
  5. import java.math.BigDecimal;
  6.  
  7. public class Pi implements Task<BigDecimal>, Serializable
  8. {
  9.  
  10.     private static final long serialVersionUID = 227L;
  11.  
  12.     /** constants used in pi computation */
  13.     private static final BigDecimal FOUR =
  14.         BigDecimal.valueOf(4);
  15.  
  16.     /** rounding mode to use during pi computation */
  17.     private static final int roundingMode =
  18.         BigDecimal.ROUND_HALF_EVEN;
  19.  
  20.     /** digits of precision after the decimal point */
  21.     private final int digits;
  22.  
  23.     /**
  24.      * Construct a task to calculate pi to the specified
  25.      * precision.
  26.      */
  27.         public Pi(int digits)
  28.     {
  29.         this.digits = digits;
  30.     }
  31.  
  32.     /**
  33.      * Calculate pi.
  34.      */
  35.         public BigDecimal execute()
  36.     {
  37.                 return computePi(digits);
  38.     }
  39.  
  40.     /**
  41.      * Compute the value of pi to the specified number of
  42.      * digits after the decimal point.  The value is
  43.      * computed using Machin's formula:
  44.      *
  45.      *          pi/4 = 4*arctan(1/5) - arctan(1/239)
  46.      *
  47.      * and a power series expansion of arctan(x) to
  48.      * sufficient precision.
  49.      */
  50.         public static BigDecimal computePi(int digits)
  51.     {
  52.                 int scale = digits + 5;
  53.                 BigDecimal arctan1_5 = arctan(5, scale);
  54.         BigDecimal arctan1_239 = arctan(239, scale);
  55.         BigDecimal pi = arctan1_5.multiply(FOUR).subtract(
  56.                                   arctan1_239).multiply(FOUR);
  57.         return pi.setScale(digits,
  58.                            BigDecimal.ROUND_HALF_UP);
  59.     }
  60.     /**
  61.      * Compute the value, in radians, of the arctangent of
  62.      * the inverse of the supplied integer to the specified
  63.      * number of digits after the decimal point.  The value
  64.      * is computed using the power series expansion for the
  65.      * arc tangent:
  66.      *
  67.      * arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 +
  68.      *     (x^9)/9 ...
  69.      */
  70.         public static BigDecimal arctan(int inverseX,
  71.                                                                         int scale)
  72.         {
  73.                 BigDecimal result, numer, term;
  74.         BigDecimal invX = BigDecimal.valueOf(inverseX);
  75.         BigDecimal invX2 =
  76.             BigDecimal.valueOf(inverseX * inverseX);
  77.  
  78.         numer = BigDecimal.ONE.divide(invX,
  79.                                       scale, roundingMode);
  80.  
  81.         result = numer;
  82.         int i = 1;
  83.         do
  84.         {
  85.             numer =
  86.                 numer.divide(invX2, scale, roundingMode);
  87.             int denom = 2 * i + 1;
  88.             term =
  89.                 numer.divide(BigDecimal.valueOf(denom),
  90.                              scale, roundingMode);
  91.             if ((i % 2) != 0)
  92.             {
  93.                 result = result.subtract(term);
  94.             }
  95.             else
  96.             {
  97.                 result = result.add(term);
  98.             }
  99.             i++;
  100.         } while (term.compareTo(BigDecimal.ZERO) != 0);
  101.         return result;
  102.     }
  103. }
Add Comment
Please, Sign In to add comment